judoole
judoole

Reputation: 1422

Gradle dependency to project in buildscript

Trying to get my head around if it is possible to use Tasks from other projects in Gradle. Let's say ProjectB is a project with src/main/groovy containing com.MyTask, having parent ProjectA

In build.gradle in ProjectC, also having parent ProjectA:

buildscript {
    dependencies{
        project(':ProjectB')
    }
}

That seems to be legit, because introdusing a typo in "project(:'ProjectB')" fails hard. What also fails is introducing this line:

import com.MyTask

Is project-references not valid in buildscript closure? Also tried moving com.MyTask to buildSrc/src/main/groovy with the same amount of success.

Upvotes: 10

Views: 3451

Answers (2)

judoole
judoole

Reputation: 1422

The solution which worked for me was to make "com.MyTask" available both at configurationtime and in sources. ProjectA(the parent) got this added to buildSrc/build.gradle's sourceSets:

sourceSets{
    main{
        groovy{
            srcDir 'ProjectB/src/main/groovy'
        }
    }
}

Now ProjectC and all other projects can use MyTask. At the same time it is bundled with the final jar of ProjectB.

The issue has also been discussed thoroughly between between Adam Murdoch, Luke Daley and Steve Ebersole: http://gradle.1045684.n5.nabble.com/buildSrc-as-a-regular-project-td5677255.html

Edit: It was smarter manipulating parent buildSrc than the standalone project. That way IntelliJ is happy-go-lucky.

Upvotes: 5

Rylander
Rylander

Reputation: 20129

From Gradle documentation 15.4. Adding dependencies to a task:

Example 15.13. Adding dependency on task from another project

build.gradle

project('projectA') {
    task taskX(dependsOn: ':projectB:taskY') << {
        println 'taskX'
    }
}

project('projectB') {
    task taskY << {
        println 'taskY'
    }
}

Upvotes: 0

Related Questions