How to execute a copy task after the java build task?


(Rodrigo de Alvarenga Mattos) #1
apply plugin: 'java'
  // This code works with ant exec but never works with gradle exec task. Why?
  task mavenClean(dependsOn: clean) {
    ant.exec(outputproperty:"cmdOut", executable: 'c:\Program Files\Maven\bin\mvn.bat') {
        arg(line: 'clean')
    }
}
    // This code works with ant exec but never works with gradle exec task. Why?
  task mavenBuild(dependsOn: build) {
    ant.exec(outputproperty:"cmdOut", executable: 'c:\Program Files\Maven\bin\mvn.bat') {
        arg(line: 'package')
    }
}
    // The code inside this copy task never executes except for the println
// The copy never works but the message is printed. Why?
  task copyEndpointLib(type: Copy, dependsOn: mavenBuild) {
    println 'copy endpoint'
    from './vendor/target/endpoints-client-libs/myendpoint'
    into '../client/src/main/java/com/mypackage'
    include "*.*"
}

What is the problem with this build script?

Still do not understand how Gradle works


(Peter Niederwieser) #2

The problem with this script is that you are calling ‘ant.exec’ in the configuration phase, rather than the execution phase. Both occurrences (and also the ‘println’ in the third task) need to be wrapped inside ‘doLast { … }’. For more information on the build lifecycle, see the Gradle User Guide.

PS: Instead of ‘ant.exec’, it would be more idiomatic to use a task of type ‘Exec’. (And then you don’t need ‘doLast’ since you are just configuring the ‘Exec’ task, rather than defining its behavior.)


(Rodrigo de Alvarenga Mattos) #3

I really do not understand how to solve this problem using doLast { … } without a sample. Could you write a sample code using my copyEndpointLib after the java assemble task integrated with the java plugin task execution in a doLast { … } statement?

Thanks Peter

But now my build script works using

“javaPluginTask”.dependsOn “MyCustomTask”

apply plugin: 'java'
  task mavenClean(type:Exec) {
    commandLine 'cmd', '/c', 'mvn clean'
}
  task mavenBuild(type:Exec) {
    commandLine 'cmd', '/c', 'mvn package'
}
  task copyEndpointLib(type: Copy) {
    from './vendor/target/endpoints-client-libs/myendpoint'
    into '../client/src/main/java/com/mypackage'
}
  clean.dependsOn mavenClean
compileJava.dependsOn mavenBuild
assemble.dependsOn copyEndpointLib

(Peter Niederwieser) #4

I just highlighted a fundamental problem with your first build script. I’m not exactly sure what you are trying to achieve and why.


(Rodrigo de Alvarenga Mattos) #5

I was trying to copy the automatic generated source code from an appengine maven project to another project that is the client library of another android project.

And the problem is execute the copy inside the java plugin build lifecycle.

When I use

task myCopyTask(type:Copy, dependsOn: assemble) nothing happens

But when I use

assemble.dependsOn myCopyTask it works

Thanks anyway!!!


(Rodrigo de Alvarenga Mattos) #6

Peter

How would you rewrite this code using doLast {…} ?

A sample where copyMyLib task executes before java assemble task A sample where copyMyLib task executes after java assemble task

apply plugin: 'java'
  task copyMyLib(type: Copy) {
    from 'mysource'
    into 'mytarget'
}
  assemble.dependsOn copyMyLib

(Peter Niederwieser) #7

‘assemble.dependsOn myCopyTask’ only makes sure that the latter comes before the former, but not that it comes before or after other tasks such as ‘compileJava’ etc.


(Peter Niederwieser) #8

There is no good way to have the ‘copyMyLib’ task run after ‘assemble’, without explicitly invoking the ‘copyLib’ task itself or a task that depends on it. (You’d have to do it in some other way, e.g. ‘assemble.doLast { copy { … } }’). What you are probably missing is another task dependency (besides ‘assemble.dependsOn copyMyLib’) from ‘copyMyLib’ on the task that produces ‘mysource’. If things nevertheless work as expected, then only by coincidence.


(Rodrigo de Alvarenga Mattos) #9

So the right thing to do is:

copyMyLib.dependsOn mavenBuild
compileJava.dependsOn copyMyLib

Now I’m starting to understand how it works.


(Peter Niederwieser) #10

If ‘mavenBuild’ generates ‘‘mysource’’, this looks good. Additionally you’ll have to let ‘compileJava’ (or, better, ‘sourceSets.main.java’) know about this source directory.