2

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.

Srneczek
  • 2,143
  • 1
  • 22
  • 26
  • that seems to me a little bit confusing. I've tested it locally and the file was deleted even with ask dependency. could you please provide the output and may be run it with debug flag? – Stanislav Nov 01 '15 at 11:01

1 Answers1

0

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.

ditkin
  • 6,774
  • 1
  • 35
  • 37
  • thx for quick answer but the file still stays there - I will check the lifecycle doc – Srneczek Oct 31 '15 at 13:52
  • your solution won't work, since `delete` is just a part of the task's configuration and should be be placed into the configuration closure, but the file will be deleted during execution. in your examples file will be never deleted at all. – Stanislav Nov 01 '15 at 10:59
  • So what should I do to delete the file? All I want is to replace few lines in my Config.java file before the code gets compiled. All I was able to find is to parse file through filter during copying it. As soon as I have to copy it I had to save it somewhere - thats why I went for solution: copy to temp location while replacing lines > delete original file > copy duplicated file back to original place > delete temp file. Or is there better solution? – Srneczek Nov 01 '15 at 11:18