How to prevent wrapper from generating gradlew.bat? / How to run task after wrapper task?

The gradle wrapper usually generates (besides from other files) the scripts gradlew and gradlew.bat. Because this project is done exclusively under Linux, there is no need for gradlew.bat. Therefore, I would like the gradle wrapper not to generate it in the first place.

Because was not able to achieve this, I tried to delete the file after each call to the wrapper task. To this purpose I created a task wrapperWithoutWin that depends on the wrapper task and deletes gradlew.bat. This task should now be called instead of the wrapper task.

Unfortunately, wrapperWithoutWin does delete an existing gradlew.bat file, but this is generated afterwards by the wrapper task. My understanding was that the dependency task (in this case the wrapper task) should be executed before the depending task (wrapperWithoutWin). Somehow this does not work as expected.

My two questions are:

  1. How can one prevent the wrapper from generating gradlew.bat?

  2. And (because I find this an interesting question as well, even if the first one is solved) how can one run tasks after the wrapper task?

     task wrapper(type: Wrapper) {
         gradleVersion = "4.2.1"
     }
    
     task wrapperWithoutWin(dependsOn: "wrapper") {
         println file("${projectDir}/gradlew.bat").exists() 
         delete "${projectDir}/gradlew.bat"
         println file("${projectDir}/gradlew.bat").exists()
     }

Your wrapperWithoutWin task is running its steps when the task is configured. Not, when the task is executed. Try this instead:

task wrapperWithoutWin(dependsOn: "wrapper") {
    doLast {
        println file("${projectDir}/gradlew.bat").exists()
        delete "${projectDir}/gradlew.bat"
        println file("${projectDir}/gradlew.bat").exists()
    }
}

Now, when you run gradle wrapperWithoutWin it should work.

For more information about the difference between task configuration and execution, see this helpful explanation.

1 Like

Thank you very much (also for the very helpful link)!

In that case, the following modification of the wrapper task should be sufficient:

task wrapper(type: Wrapper) {
    gradleVersion = "4.2.1"
    doLast {
	delete "${projectDir}/gradlew.bat"
    }
}