How can I use a variable value in a copy task (configuration phase) which is initially determined during a task in execution phase?

Hi everybody,

I’m facing a problem in my build script and I’m not sure how to handle it:

I have three tasks:

The first task reads a file and reads a build number from it, increments it and saves it for other tasks to use it The second task builds a zip file and uses this variable to name the zip The third task copies the zip and some other stuff to a new destination

ext.release = '1.9'
ext.patch = '1'
ext.destDir = 'build'
// dummy value, actual value will be read from build.properties
ext.build = 'xxx'
ext.tag = 'MyApp_' + release + '.' + patch + '_'
    task incrementBuildNumber() {
    doLast{
    // read line, parse buildnumber, set next number
    def path = 'build.properties'
    def list = []
    File propertyFile = file(path)
        // find old number by going through each line
    propertyFile.eachLine {line ->
      // save previous number, set new number
      if(line.startsWith("release.buildnumber=")) {
        // set build, 22 is dummy because it does not matter
         // where it comes from here
        globeBuild = '22'
        // set new value
        line = "release.buildnumber="+ tag + '22'
      }
      // save in list
      list += line
    }
    // delete old, create new, put content in file
    propertyFile.delete()
    propertyFile.createNewFile()
    // write new properties back in file from list
    list.each{ item -> propertyFile << item+"\n" }
  }
}
    task packageDM(type:Zip, dependsOn:['incrementBuildNumber']) {
  //Should be MyApp_1.9.1_021 but uses MyApp_1.9.1_xxx
  baseName = tag + build
  from('src/main/db') {
    include '**/*'
     into('DB/prod')
  }
  from('.') {
    include 'build.properties'
  }
}
    task copyPackage(type:Copy, dependsOn:['packageDM']) {
  // move file to new destination
  def dir = destDir
  from '.'
  into dir
  include tag + build + '.zip'
      // delete original file
  doLast{
    file(tag + build + '.zip').delete()
  }
}

I would like to parse the number and use it for each other tasks. But the zip and copy task use the variable in the configuration phase but the increment task is called in the execution phase (zip is always called ‘MyApp_1.9.1_xxx’ instead of the real build number from the file) Is it possible to change that, because i don’t want to pass parameters from outside.

Thanks in advance Stefan

You’ll have to defer all usages of the computed value(s) to the execution phase. For example, you could do ‘packageDM.doFirst { baseName = … }’. A potential drawback is that the up-to-date check won’t see the correct value, but there isn’t currently a better solution.

Hi Peter,

Thanks for your reply! Moving parts of the task logic to a doFirst block did not work, it still uses the wrong number. When i put the whole task löogic in a doFirst it won’t execute at all (up-to-date)

But maybe my basic approach is wrong… Maybe i move the variables to a config file

If it still uses the wrong number, chances are there is something wrong with the build script. I wouldn’t use ‘build’ as the name for an extra property because it might collide with the task name.

Yeah, i just changed that to shorten my code :slight_smile: Normally it is called globeBuild…