igr
igr

Reputation: 10634

Gradle task that depends on other task output?

I have a custom task that depends on installDist - not only for execution, but also on installDist output:

project.task('run', type: JavaExec, dependsOn: 'installDist', overwrite: true) {
    def libdir = new File("$project.tasks.installDist.destinationDir/lib")
    ...

It happens that when running the first time, the libdir does not catch the destination dir of installDist, because of how the Gradle works. Since I need to use libdir for my task, how can I wait for the installDist to finish, and then to run my task?

I know I can explicitly run installDist before my task, but I want just to run my task after clean up.

Upvotes: 2

Views: 2892

Answers (2)

igr
igr

Reputation: 10634

After @Ben Greens answer, I figured:

project.task('run', type: JavaExec, dependsOn: 'installDist', overwrite: true) {
    doFirst {
        def libdir = new File("$project.tasks.installDist.destinationDir/lib")
...

so this happens before my task is executed, but after the installDist is executed.

Upvotes: 5

Ben Green
Ben Green

Reputation: 4111

The task lifecycle is a little confusing. Gradle reads through the whole file and tries to configure the tasks before actually executing them. Take a look at the build.gradle in Example 20.1 on https://docs.gradle.org/current/userguide/build_lifecycle.html for more info on the lifecycle.

To avoid this, you could try putting the relevent task info into a doLast block like so:

task run() {
    dependsOn 'installDist'
    doLast {
        javaexec {
            main = project.mainClassName
            classpath = project.configurations.standaloneRuntime
            def libdir = new File("$project.tasks.installDist.destinationDir/lib")
        }    
    }
}

Upvotes: 2

Related Questions