I recently started working with gradle. I found out pretty quickly that you can easily tell gradle to automatically detect the current version with the following snippet:
def getVersionName = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--dirty=-SNAPSHOT'
standardOutput = stdout
}
return stdout.toString().trim()
}
version = getVersionName()
The I found out that you can substitute that version number into your resources like this:
processResources {
expand(version: version)
}
And in the files you want to have the version number something like this (should work with any variable)
version: ${version}
The substitution is working great and working as expected.
However I ran into a problem when the version number changed and no resources did that the resoures do not get reprocessed and therefore the version number does not get updated in those files.
I somewhat fixed this by telling the task to run every time like this:
processResources {
expand(version: version)
outputs.upToDateWhen { false }
}
This is working but I feel like this is a pretty dirty hack.
What I would like to have instead is a logic that would rerun the task whenever the resource files change (like it already does) or when the version number (or any variable or variables that I want care about for that matter) change.
If anyone is interested here is the link to the actual file: https://gitlab.crazyblock-network.net/BrainStone/MplManager/blob/master/build.gradle
And this is the repo: https://gitlab.crazyblock-network.net/BrainStone/MplManager
This should fix the problem:
def tokens = [
'version': version
]
processResources {
inputs.properties(tokens)
expand(tokens)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With