Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rerun processResoures when variable changes

Tags:

gradle

build

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

like image 383
BrainStone Avatar asked Jun 30 '16 05:06

BrainStone


1 Answers

This should fix the problem:

def tokens = [
    'version': version
]

processResources {
    inputs.properties(tokens)
    expand(tokens)
}
like image 128
JB Nizet Avatar answered Sep 29 '22 12:09

JB Nizet