how do I use the project version inside a task so ...
# community-support
b
how do I use the project version inside a task so that the task gets updated once the project version changes?
1
j
Add it as an input of your task.
Copy code
task.yourInput.set(provider { project.version })
b
so simply accessing project.version inside a task is a bad idea, correct?
since the task won't be rerun when you update your version
j
yes, not because the version, but the project instance
b
thanks!
is there a way to default to that though when declaring your task?
j
Anything cannot be easily injected with
@Inject
probably should be passed as an input but avoiding global things like project and so on
b
Copy code
@get:Input
abstract val version: Property<String> = provider { project.version }
kinda like that
j
Gradle allows you injecting easily a ObjectFactory or a ProviderFactory. But they do not allow you injecting Project, that is a guide about what to avoid.
You shouldn't use project inside the task implementation
b
ok, gotcha
thank you
j
A good way to see possible issues is, create a test kit test with configuration cache enabled
You will get issues about serialization and so on, to catch what should be passed as input from outside or referenced directly inside (which is usually almost nothing)
b
I guess I could also just auto configure tasks with
Copy code
inputs.property("version", project.version)
v
You could set the convention of your property in the constructor of the task. But usually you shouldn't. It is best practice that tasks and extensions are as opinionless as possible to maximize reusability, and that plugins that register tasks than add opinion by setting the conventions on the properties and wiring extension properties to task properties and so on.