Set a copy task to run after compileJava

Sorry for what must sound like a trivial question, but this still escapes me.

I want to copy the result of java compile (the class files) to another directory (copy, not move).

However, if I create a Copy task, all I can do in specifying dependencies is say

compileJava.dependsOn myCopyTask

which is not what i want, I want something like:

myCopyTask.dependsOn compileJava

or more to the point (but not gradle syntax)

compileJava.doAfter myCopyTask

However, there is no ‘doAfter’.

So, how do I do it?

I have tried this kind of syntax, but it doesn’t seem to run.

compileJava {
 doLast {
  copy {
         from sourceSets.main.runtimeClasspath
         into "$buildDir/classes-copy"
         include '*.class'
       }
      }
}

I realize I could probably create a copy task on which the ‘processResources’ task depends, which would achieve the same thing ( I think ), except, I want to make sure that ‘myCopyTask’ runs if and only if the ‘compileJava’ task runs…

(not depending on if the processResources task runs).

thanks for your help and patience.

sean

I think you have to include leading directories in your ‘include’ spec. This seems to work for me:

compileJava << {

copy {

from sourceSets.main.runtimeClasspath

into “$buildDir/classes-copy”

include “**/*.class”

}

}

ah yes! Thanks, that was the problem here.

Is there any other ‘correct’ way to have the code running in the order I need?

(just so I know)

i.e., if I was instead creating a copy task, as shown in the examples in the documentation:

task('copy', type: Copy) {
    from(file('srcDir'))
    into(buildDir)
}

How would I get it running after the ‘compileJava’ task (and only running if compileJava task ran (not if it was skipped or is up-to-date)) ?

Just depending the copy task on ‘compileJava’ works as you intend for the “up-to-date” part. I’m not sure about the “skipped”. At least it seems to work on a quick test.

I think the correct approach is:

  • Have ‘compileJava’ unaltered. * Depend the copy task on ‘compileJava’. * Depend any task using the copied files on the copy task.

This should give you a clean solution.

The problem I perceive with this solution (but perhaps I am wrong)

is that, I would intuitively want:

  1. files copied, if compileJava is run (i.e., not skipped, and not ‘UP-TO-DATE’.

  2. files not copied, if compileJava is NOT run (i.e., skipped, or ‘UP-TO-DATE’.

However, if I have some other task "depend’ing on ‘myCopyTask’,

then suddenly I have the unwanted situation where compileJava may be run,

but my copy task not (i.e., if task B dependsOn myCopyTask, and task B is UP-TO-DATE’.

Task B cannot be up-to-date, when compileJava is run (and not up-to-date). Since compileJava is outdated, the copy task is outdated, task B is outdated.

If you don’t run task B, then why should you have to run the copy task?

I think this solution works in all scenarios.