75inchpianist 75inchpianist - 1 month ago 10
Groovy Question

Gradle Zip Task Up-To-Date even after clean build

I have the following task definitions.

task generateFile {
def file = new File("$buildDir/setclasspath.sh")
outputs.file(file)
doLast {
file.text = "sample"
}
}


and

task createDistro(type: Zip, dependsOn: ['jar', 'generateFile']) << {
from generateFile {
fileMode = 0755
into 'bin'
}
}


Problem is when I run a
gradle clean build
the generateFile task runs, but the zipFile task does not.

Why is this the case? Shouldn't the clean make the input of the Zip task trigger a new run?

sm4 sm4
Answer

You need to first understand the phases of Gradle. Mainly remember that there are CONFIG and EXECUTION phases. This was your previous problem: Gradle clean erasing my file prior to ZIP task execution

You made the same mistake in the createDistro. When you use the << you are reducing the task to the doLast only and that runs in EXECUTION phase. That means your Zip task has not been configured with the from part. So again the correct definitions should be:

apply plugin: 'java'

task generateFile {
  def file = new File("$buildDir/setclasspath.sh")
  outputs.file(file)
  doLast {
    file.createNewFile() // you need to actually create the file first
    file.text = "sample"
   }
}

task createDistro(type: Zip, dependsOn: ['jar', 'generateFile']) {    
// this is the configuration of Zip task, do not run it in doLast or '<<'
 from generateFile {
   fileMode = 0755
   into 'bin'
 }
}

Moreover as @RaGe mentioned in comments, the task won't run. You need to plug it in the task tree.

But I think you are still trying to fix a problem that you might not need to fix. From what you are doing, you might be actually better off using the gradle distribution plugin.