Slackbot
06/20/2023, 10:49 AMVampire
06/20/2023, 11:36 AMval generateManifest by tasks.registering {
val outputFile = layout.buildDirectory.file("MANIFEST-generated.MF")
outputs.file(outputFile).withPropertyName("generatedManifest")
doLast {
outputFile.get().asFile.writeText(
"""
Foo: bar
""".trimIndent()
)
}
}
tasks.jar {
dependsOn(generateManifest)
manifest {
from(generateManifest.map { files(it).singleFile })
}
}
Or as proper task class version:
abstract class GenerateManifest : DefaultTask() {
@get:OutputFile
abstract val generatedManifest: RegularFileProperty
@TaskAction
fun taskAction() {
generatedManifest.get().asFile.writeText(
"""
Foo: bar
""".trimIndent()
)
}
}
val generateManifest by tasks.registering(GenerateManifest::class) {
generatedManifest.set(layout.buildDirectory.file("MANIFEST-generated.MF"))
}
tasks.jar {
dependsOn(generateManifest)
manifest {
from(generateManifest.flatMap { it.generatedManifest })
}
}
Unfortunatley, it needs an explicit dependsOn as the implicit task dependency is not respected in this case. I just reported that as https://github.com/gradle/gradle/issues/25435tomas-mrkvicka
06/20/2023, 12:47 PMmanifest {
from(generateManifest.flatMap { it.generatedManifest })
}
Is nice. I didn't know about it.
Thank you for your help!
Side note: my GenerateManifest task actually uses a Build Service's properties (which can not atc as an task input at the monent) so I can not reuse the task's output. So UP-TO-DATE check doesn't bother me right now.Vampire
06/20/2023, 1:22 PMwhich can not atc as an task input at the monentwho says that?
Vampire
06/20/2023, 1:23 PMabstract class RandomService : BuildService<BuildServiceParameters.None> {
val randomValue get() = Random.nextInt()
}
abstract class RandomTask : DefaultTask() {
@get:ServiceReference
abstract val randomService: Property<RandomService>
@get:Input
abstract val input: Property<Int>
init {
input.set(randomService.map { it.randomValue })
outputs.upToDateWhen { true }
}
@TaskAction
fun taskAction() {
println(input.get())
}
}
gradle.sharedServices.registerIfAbsent("random", RandomService::class) { }
val random by tasks.registering(RandomTask::class)
The task random it out-of-date because the input changes. And if you change the build service to always return the same value, the task is properly up-to-date.tomas-mrkvicka
06/22/2023, 6:21 AMwho says that?Well, the documentation says: Note that using a service with any other annotation is currently not supported. For example, it is currently not possible to mark a service as an input to a task. But your approach:
init {
input.set(randomService.map { it.randomValue })
outputs.upToDateWhen { true }
}
looks promising. I didn't know that I can do it like that. I will try that. Thank you!!