Niels Doucet
02/20/2024, 4:33 PMNiels Doucet
02/20/2024, 4:34 PMval writeBuildProperties by tasks.registering(WriteProperties::class) {
property("commit", versioning.info.commit)
property("version", project.version.toString())
destinationFile = layout.buildDirectory.file("build-info.properties")
}
tasks.processResources {
from(writeBuildProperties) {
rename { "META-INF/$it" }
}
}
This seems nice and terse, but it doesn't really tell gradle that the properties file should be considered part of the sourceSets.main.resources
Niels Doucet
02/20/2024, 4:37 PMval writeBuildProperties by tasks.registering(WriteProperties::class) {
property("commit", versioning.info.commit)
property("version", project.version.toString())
destinationFile = layout.buildDirectory.file("build-info/META-INF/build-info.properties")
}
sourceSets.main {
resources {
srcDir(writeBuildProperties.get().outputs.files.singleFile.parentFile.parentFile)
}
}
This option is clear about the sourceset, but it requires that ugly parent
parent
thing to appease the srcDir method ๐คNiels Doucet
02/20/2024, 4:40 PMThomas Broyer
02/20/2024, 4:59 PMlayout.buildDirectory.dir("build-info")
or something like that (I've never really used the project layout as I haven't wrapped my head around that directory property API yet) so the srcDir
is easier to configure (except now you need to explicitly configure a builtBy(writeBuildProperties)
)ephemient
02/20/2024, 4:59 PMsrcDir(writeBuildProperties.flatMap { it.destinationFile.getAsFile().map { it.parentFile.parentFile } })
Niels Doucet
02/20/2024, 5:24 PMsrcDir
to take a single file output and put it in a folder of choice, but perhaps that's a bit too specific as a use case ๐คท
I think I'll go with the builtBy
approach as the "least ugly" solution:
val buildInfoDir = layout.buildDirectory.dir("build-info")
val writeBuildProperties by tasks.registering(WriteProperties::class) {
property("commit", versioning.info.commit)
property("version", project.version.toString())
destinationFile = buildInfoDir.map { it.file("META-INF/build-info.properties") }
}
sourceSets.main {
resources {
srcDir(files(buildInfoDir) { builtBy(writeBuildProperties) })
}
}
Vampire
02/20/2024, 5:46 PMSync
, which `from`s the write properties task into the respective subdirectory where it is needed and you use the sync task as srcDir
2. do not use a WriteProperties
task, but just have a ...properties
file with placeholders in src/main/resources
and use expand
on processResources
to fill in the placeholders
I usually use the latter unless there are strong reasons not to.
This is also more compatible with running from IDE if you cannot or don't want to use Gradle delegation.Niels Doucet
02/21/2024, 8:12 AMThomas Broyer
02/21/2024, 8:17 AMNiels Doucet
02/21/2024, 8:23 AMtasks.processResources {
filesMatching("META-INF/edge-build-info.properties") {
expand(
"commit" to versioning.info.commit,
"version" to project.version.toString()
)
}
}
Thanks again for the suggestions and ideas ๐Vampire
02/21/2024, 8:37 AM....properties.template
or whatever and add a rename
,
or use a more simplistic alternative like a line-based replacement from the build script, or using ReplaceTokens
which also just does a text-replacement.Vampire
02/21/2024, 8:37 AM