Inheritance of tasks and dependency on subprojects' tasks

(Steve) #1

Hey there,

I am building a project and have the following issue: I have two subprojects which are very similar, they share all the same tasks task1, task2, task3; only one task is a little different, taskDiff. They both have the task, and it does very similar things, but is a little different in both subprojects
My idea was to create a group which contains both subprojects, and task1, task2, task3 are defined in that group. That way, both subprojects can be called with those tasks.
The problem is, taskDiff needs to be defined in the subprojects. But the tasks of the group depend on that task. When I do a

task1.dependsOn taskDiff

I get an error of course, because gradle doesn’t know taskDiff at that level.

Can you give me some hint on how to proceed here?

I hope I could make myself clear here, I am quite new to gradle.

Thanks a lot!

(Chris Doré) #2

Use task1.dependsOn 'taskDiff'. Using a string will cause it to be lazily evaluated after everything is configured.

(Steve) #3

Oh interesting insight, thank you!

I solved it in the meantime already, because I realized how this inheritance actually works.


    task task1 {

    task taskDiff {
        // some things that should always be done in this task


and then

configure(project(’:my-docker-subproject’)) {

    task otherTask {

    taskDiff {
        //here I am extending the inherited task, e.g. with doFirst{}
    task1.dependsOn taskDiff


So I could extend taskDiff in the subproject (note, that you can’t write ‘task taskDiff {}’ but have to write ‘taskDiff{}’ and then with things like ‘doFirst’ you can combine the inherited taskSteps with the new ones.

Hope that helps someone =)