Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle dependency to project in buildscript

Tags:

gradle

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.

like image 494
judoole Avatar asked Jun 11 '13 17:06

judoole


People also ask

How do I add a dependency project in Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build. gradle file. This declares a dependency on an Android library module named "mylibrary" (this name must match the library name defined with an include: in your settings.

What is the use of Buildscript in Gradle?

buildscript: This block is used to configure the repositories and dependencies for Gradle. dependencies: This block in buildscript is used to configure dependencies that the Gradle needs to build during the project.

How do I import Gradle dependencies into eclipse?

Import an existing Gradle project You can also import existing Gradle projects into Eclipse. Select the File Import… ​ Gradle Gradle Project menu entry for this. After pressing the Next > button, you need to specify the root directory of your Gradle project.


1 Answers

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.

like image 78
judoole Avatar answered Sep 23 '22 08:09

judoole