Why do only some tasks on dependent projects trigger same named tasks on depended-on projects


(Thor Kummer) #1

Sorry about this lengthy question, it really is a simple as the title suggests, but I wanted to provide an example and rational it.

According to the tutorial a running a task on a dependent project will first trigger the same task on the depended-on project. For my java projects this works for compileJava but not for clean or my own tasks.

I tested with the following project

Structure:

pro/
  settings.gradle
  build.gradle
  a/
  b/

settings.gradle:

include 'a', 'b'

build.gradle:

configure(subprojects) {
    apply plugin: 'java'
    task mytask { }
}
    configure(project(':b')) {
    dependencies {
        compile project(':a')
    }
}

gradle b:compileJava

gives

Executing command: "b:compileJava"
:a:compileJava UP-TO-DATE
:a:processResources UP-TO-DATE
:a:classes UP-TO-DATE
:a:jar UP-TO-DATE
:b:compileJava UP-TO-DATE
  BUILD SUCCESSFUL
  Total time: 3.089 secs
  Completed Successfully

I.e, the same named task on a is called before the requested task on b. (Actually I expected only the compileJava but not processResources, classes and jar, since compileJava doesn’t depend on them. (Is this behaviour in the documentation?)).

but

gradle b:clean

gives

Executing command: "b:clean"
:b:clean
  BUILD SUCCESSFUL
  Total time: 2.892 secs
  Completed Successfully

That is, a:clean is not called first as I expected from the tutorial. Same with ‘mytask’:

Executing command: "b:mytask"
:b:mytask UP-TO-DATE
  BUILD SUCCESSFUL
  Total time: 2.936 secs
  Completed Successfully

Do I need to explicitly set cross project dependencies between same named tasks on dependent projects or is there a simpler way to achieve this.

My actual usecase is a deploy task that is supposed to trigger compile…deploy (if needed) on depended-on tasks before deploying the requested project. E.g.

gradle b:deploy

should trigger deploy on a first and all it’s requisite tasks.


(Thor Kummer) #2

Replacing the ‘compile project(’:a’) with just ‘project(’:a’) doesn’t change this, but replacing the dependencies { } call with dependsOn(’:a’) does produce the behaviour I’m after. They are apparently not equivalent as I had assumed.


(Peter Niederwieser) #3

According to the tutorial a running a task on a dependent project will first trigger the same task on the depended-on project.

Where do you have that from?


(Thor Kummer) #4

The user guide, section 38.6.1.2. Declaring dependencies says:

Provided you have two Java projects where one depends on the other. If you trigger a compile for the dependent project you don't want that all tasks of the other project get executed. Therefore a dependsOn creates dependencies between tasks with equal names

As I said, I had a assumed that dependsOn calls and the dependencies script blocks had the same effect, but that seems not to be the case.


(Peter Niederwieser) #5

They aren’t the same. Project.dependsOn() is an advanced and rarely needed feature. Typically you just declare the compile/runtime dependencies between projects (in the dependencies {} section), and Gradle will figure out the task dependencies automatically.