This message was deleted.
# kotlin-dsl
s
This message was deleted.
1
f
I was curious to see if ChatGPT could do this, but it couldn't get it quite right.
Try this instead:
Copy code
@Suppress("UnstableApiUsage")
tasks.named<ProcessResources>("processResources") {
    from("src/main/resources")
    include("application.properties")
    properties
        .filter { it.value != null }
        .forEach { prop ->
            expand(
                prop.key to prop.value,
                "project.${prop.key}" to prop.value,
            )
        }
}
And the ChatGPT answer for the curious:
Here's the equivalent Kotlin DSL version of the Gradle script:
```import org.apache.tools.ant.filters.ReplaceTokens
tasks.processResources {
withCopySpec {
from("src/main/resources")
include("application.properties")
project.properties.forEach { prop ->
if (prop.value != null) {
filter(ReplaceTokens::class.java, mapOf(prop.key to prop.value))
filter(ReplaceTokens::class.java, mapOf("project.${prop.key}" to prop.value))
}
}
}
}```
Note that the Kotlin DSL version uses
tasks.processResources
instead of
processResources
, and
withCopySpec
instead of
with copySpec
. In addition, the
findAll()
method has been replaced by
forEach
, and the map passed to
filter()
has been modified to use Kotlin's map syntax. Finally, the
ReplaceTokens
filter is referred to using
ReplaceTokens::class.java
.
a
Maybe something like Franck proposed, just a note that
expand
works a bit differently than filter. It uses Groovy templating system. And using
.
in property names might not work as expected. So with filter maybe something like that (modified a bit so filter is set just once):
Copy code
tasks.processResources {
    // Not needed if some java plugin is applied, since JavaBasePlugin sets 'from(...)'
    // from("src/main/resources")
    include("application.properties")
    val tokens = project.properties
        .filter { it.value != null }
        .flatMap { prop -> listOf(prop.key to prop.value.toString(), "project.${prop.key}" to prop.value.toString()) }
        .toMap()
    filter<ReplaceTokens>("tokens" to tokens)
}
Or if you want to use “Maven style” for property token, e.g. ${property} instead of @property@, something like this should work:
Copy code
filter<ReplaceTokens>("tokens" to tokens, "beginToken" to "\${", "endToken" to "}")
s
I specifically wanted ant style tokens like @property@ because maven style tokens clash with spring tokens which are both ${property}
@Anze Sodja example worked but not all the files were copied from src/main/resources to the build directory. i added include("*") and it worked. but does that mean the all files are filtered?
Copy code
tasks.processResources {
    // Not needed if some java plugin is applied, since JavaBasePlugin sets 'from(...)'
//    from("src/main/resources")
    include("*")
    include("application.properties")
    val tokens = project.properties
        .filter { it.value != null }
        .flatMap { prop -> listOf(prop.key to prop.value.toString(), "project.${prop.key}" to prop.value.toString()) }
        .toMap()
    filter<org.apache.tools.ant.filters.ReplaceTokens>("tokens" to tokens)
}
and @Franck Rasolo suggestion didn't compile withCopySpec could is unresolved
v
You could use the same
with(copySpec { ... })
hack as in your original code, but it is ugly and hacky even in Groovy DSL. :-D Use the code of @Anze Sodja, but use
filesMatching
instead of
include
.
Copy code
tasks.processResources {
    filesMatching("application.properties") {
        filter...
    }
}
🙌 1
f
@Shalom Ben-Zvi Kazaz that was the suggestion from ChatGPT, not mine 😉
👍 1
s
@Vampire Thank you , that did it and now its perfect.
👌 1
c
The same problem had me stumped recently. None of the described solutions seemed to work, so I did it manually...
Copy code
val rep = Regex("\\$\\{((?:\\w|\\.)+)\\}")
    filesMatching("**") {
        filter { line ->
            line.replace(rep) { match ->
                properties[match.groupValues[1]]?.toString() ?: match.value
            }
        }
    }