Ivan CLOVIS Canet
07/13/2025, 8:24 AM@get:InputFile
abstract val file: RegularFileProperty
then Gradle tracks its state, which doesn't make sense since the file doesn't exist yet.
I've seen
@get:Input
abstract val file: Property<String>
used, which does work, but that's not nice to use when configuring a task.Ivan CLOVIS Canet
07/13/2025, 8:38 AMval createVersion by tasks.registering {
description = "Store the self version into the resources"
val file = project.layout.projectDirectory.file("src/main/kotlin/Self.kt").asFile
doLast {
file.writeText("""
package dev.opensavvy.conventions.versions
const val OPENSAVVY_CONVENTIONS_VERSION = "$version"
""".trimIndent())
}
inputs.property("version", version)
outputs.file(file)
}
and the best way I've found to rewrite it is
val createVersion by tasks.registering(EmbedVersionTask::class) {
sources.set(project.layout.projectDirectory.file("src/").toString())
this.version = project.version.toString()
}
abstract class EmbedVersionTask : DefaultTask() {
@get:Input
abstract val sources: Property<String>
@get:Internal
val writePath: Provider<String>
get() = sources.map { "$it/main/kotlin/Self.kt" }
@get:Input
abstract val version: Property<String>
init {
description = "Store the self version into the resources"
outputs.file(writePath)
}
@TaskAction
fun embedVersion() {
File(writePath.get()).writeText("""
package dev.opensavvy.conventions.versions
const val OPENSAVVY_CONVENTIONS_VERSION = "${version.get()}"
""".trimIndent())
}
}
but there must be a better way, right?Philip W
07/13/2025, 9:13 AMThomas Broyer
07/13/2025, 11:07 AM@OutputFile
(just like you configured outputs.file()
in the original code)Thomas Broyer
07/13/2025, 11:13 AM@get:OutputFile
abstract val file: RegularFileProperty
then something like
val createVersion by tasks.registering(EmbedVersionTask::class) {
this.version = project.provider { project.version.toString() }
this.file = project.layout.projectDirectory.file("src/main/kotlin/Self.kt")
}
(I agree if this is automatically run during a build, it should output to the build directory; it's only acceptable to output to source trees if you run the task on its own and commit the generated file)Martin
07/13/2025, 12:46 PMthe user should be able to configure where it is placed.I stopped giving users the options to configure output locations. This is an anti pattern IMO as a misconfiguration could lead to overlapping outputs. +1 to @Philip W and @Thomas Broyer suggestion to output in the
build
directoryMartin
07/13/2025, 12:47 PMIvan CLOVIS Canet
07/13/2025, 3:11 PMIvan CLOVIS Canet
07/13/2025, 3:14 PMMartin
07/13/2025, 3:33 PM