Slackbot
02/18/2023, 6:55 PMFranck Rasolo
02/18/2023, 7:25 PMFranck Rasolo
02/18/2023, 7:25 PM@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,
)
}
}
Franck Rasolo
02/18/2023, 7:33 PMHere'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 usesinstead oftasks.processResources
, andprocessResources
instead ofwithCopySpec
. In addition, thewith copySpec
method has been replaced byfindAll()
, and the map passed toforEach
has been modified to use Kotlin's map syntax. Finally, thefilter()
filter is referred to usingReplaceTokens
.ReplaceTokens::class.java
Anze Sodja
02/18/2023, 8:12 PMexpand
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):
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:
filter<ReplaceTokens>("tokens" to tokens, "beginToken" to "\${", "endToken" to "}")
Shalom Ben-Zvi Kazaz
02/19/2023, 2:46 AMShalom Ben-Zvi Kazaz
02/19/2023, 2:48 AMtasks.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)
}
Shalom Ben-Zvi Kazaz
02/19/2023, 2:50 AMVampire
02/19/2023, 11:33 AMwith(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
.
tasks.processResources {
filesMatching("application.properties") {
filter...
}
}
Franck Rasolo
02/19/2023, 12:53 PMShalom Ben-Zvi Kazaz
02/19/2023, 1:26 PMClaude Brisson
02/28/2023, 12:54 PMval rep = Regex("\\$\\{((?:\\w|\\.)+)\\}")
filesMatching("**") {
filter { line ->
line.replace(rep) { match ->
properties[match.groupValues[1]]?.toString() ?: match.value
}
}
}