How to replace a token in resource file using Kotlin DSL
Asked Answered
E

2

5

What would be the equivament of the following in Kotlin DSL (build.gradle.kts)?

processResources {
    from(sourceSets.main.resources.srcDirs) {
        filter ReplaceTokens, tokens: [version: version]
    }
}
Elegance answered 16/9, 2020 at 12:6 Comment(2)
Does this answer your question? How to configure the processResources task in a Gradle Kotlin buildForeleg
No question you linked asks how to set processResources task and I'm asking how to rewrite a specific processResources function to KotlinElegance
E
12

It's actually super easy:

tasks.processResources {
    expand("version" to project.version)
}

And then just put ${version} in resource and it will be replaced with your project version

Elegance answered 16/9, 2020 at 14:28 Comment(1)
Thank you so much for this answer. It works and helped me with my issue as well. :)Buckwheat
A
0

As of Gradle 8.4, the you can use a filter. This is useful if you have a large number of tokens to replace

tasks.processResources {
    filter<ReplaceTokens>("tokens" to mapOf("version" to project.version))
}

If you have the tokens as a Properties instance (or an existing Map), that can be used as well:

val properties: Properties = . . .
tasks.processResources {
    filter<ReplaceTokens>("tokens" to properties)
}
Aquacade answered 7/6 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.