Is there a configuration-cache compatible way to a...
# community-support
n
Is there a configuration-cache compatible way to access
project.version
during a task action?
m
Make it a task input
n
I suppose it's not a lazy property, so reading it eagerly shouldn't be a risk (if it would change, you've got bigger problems). Sometimes it's the most obvious solution. Thanks 🙂
👍 1
e
if you do something like
Copy code
abstract class MyTask : DefaultTask() {
    @get:Input
    val version: String = project.version.toString()
}
tasks.register<MyTask>("myTask")
or
Copy code
abstract class MyTask : DefaultTask() {
    @get:Input
    abstract var version: Property<String>
}
tasks.register<MyTask>("myTask") {
     version.set(project.version.toString())
}
then it'll be captured when the task is first configured, and if you do something like
Copy code
abstract class MyTask : DefaultTask() {
    @get:Input
    abstract val version: Property<String>
}
tasks.register<MyTask>("myTask") {
    version.set(project.provider { project.version.toString() })
}
then it'll be captured at the end of configuration
t
wouldn't it need to be a
Property<String>
that is set from
project.providers.gradleProperty("version")
? I guess that assumes you have a single version for a whole build...
actually I am not sure how that would get resolved. but using providers would be the first thing I try
v
project.providers.gradleProperty("version")
would only work if the version is actually coming from a Gradle property like when you have it in
gradle.properties
, but even then the version could be changed in the build script or by a plugin. Safest currently should be the last variant in the comment before yours, having a
Provider<String>
in the task like you should always have and setting value or convention to a provider that reads it from
project
. Without configuration cache it will then be read when the inputs for the task are fingerprinted, with configuration cache at the time the configuration cache entry is calculated. Hopefully with Gradle 10 and the great propertyization it will maybe be a
Provider<String>
in
Project
already.
👍 2
👌 1
j
I suppose it's not a lazy property
Technically it is any object, so it can be lazy, a lot of semver plugins do that, so you should avoid calling it eagerly.
1
n
Our usage is as part of a settings plugin to handle our version management. So we're in the case where I think it can be considered safe to do so 🙂
And we do indeed overwrite the version property with a ValueSource object.