简体   繁体   中英

Gradle task doesnt remove file if other taks is run with dependency on the first one

I am new to Gradle and I am obviously missing something. I have 2 tasks in gradle:

task myCopy(type: Copy) {
    println 'copy1'
    from 'src/main/java/android/app/cfg/TestingConfigCopy.java'
    into 'src/main/java/android/app/cfg/temp'
}

task myDelete(dependsOn: 'myCopy', type: Delete) {
    println 'delete1'
    delete 'src/main/java/android/app/cfg/TestingConfigCopy.java'
}

When I remove dependency and run them 1 by 1, file gets copied and then the old one deleted but when I use dependency and run the myDelete task, the file gets copied but doesnt get deleted. I feel I am missing some basic behaviour of Gradle. Those tasks are located at the end of my build.gradle file inside /app directory of the android project.

The code in your myDelete task is executing during the task's configuration phase. If you want the code to execute during the execution phase you must put the code in the task's doLast clause.

There are two ways to do this. You could write:

task myDelete(dependsOn: 'myCopy', type: Delete) {
    doLast {
       println 'delete1'
       delete 'src/main/java/android/app/cfg/TestingConfigCopy.java'
    }
}

or you can use the << notation:

task myDelete(dependsOn: 'myCopy', type: Delete) << {
    println 'delete1'
    delete 'src/main/java/android/app/cfg/TestingConfigCopy.java'
}

See gradle's build lifecycle documentation for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM