arturro
arturro

Reputation: 51

Gradle task dependsOn does not work

I want to setup simple dependency between tasks.

My build.gradle

apply plugin: 'java'

sourceCompatibility = 1.8
version = '1.0'

repositories {
    mavenCentral()
}

task('Second', dependsOn: 'First') {
    println "Second"
}

task('First') {
    println "First"
}

As a result I expect First and than Second. But I've got:

$ gradle build
Second
First
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build UP-TO-DATE

What am I doing wrong? Thanks for help.

Upvotes: 5

Views: 11037

Answers (1)

Opal
Opal

Reputation: 84786

Still the same error.. You're at configuration phase. Add actions to task and everything will be fine. This will work:

apply plugin: 'java'

sourceCompatibility = 1.8
version = '1.0'

repositories {
    mavenCentral()
}

task('Second', dependsOn: 'First') << {
    println "Second"
}

task('First') << {
    println "First"
}

This is a configuration phase:

task t1 {
   println "t1"
}

Code in configuration phase is executed before action. Adding action is done with << (leftShift) operator.

task t1 << {
   println "t1"
}

Here are the docs.

<< is just overridden operator do doLast method. Without it it will be:

apply plugin: 'java'

sourceCompatibility = 1.8
version = '1.0'

repositories {
    mavenCentral()
}

task('Second', dependsOn: 'First') {
   doLast {
      println "Second"
   }
}

task('First') {
   doLast {
      println "First"
   }
}

I also encourage you to read this blog post.

Upvotes: 13

Related Questions