75inchpianist 75inchpianist - 1 month ago 8
Groovy Question

Gradle clean erasing my file prior to ZIP task execution

I have the following simple task in my build:

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

outputs.file(file)
}

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


When I run
gradle clean build
I see the following output:

Cannot call TaskOutputs.file(Object) on task ':generateFile' after task has started execution. Check the configuration of task ':generateFile' as you may have misused '<<' at task declaration


How do I declare the task file creation outputs as an input to the zip task while also ensuring they happen in the execution phase?

If I leave off the << then the clean task wipes the generated file before the ZIP can use it. If I keep them, I get the above error.

sm4 sm4
Answer

It's the opposite as what is being suggested in the comments. You are trying to set the outputs in execution phase. The correct way to do what you are probably trying to do is for example:

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