36

I have an identical problem to this Gradle Multi-Module Project Setup but I have to use project compile dependencies to build and cannot use the library(jar) dependencies solution that was given as a solution in the above question.

Root
|__ P1
|   |_ PP1
|   |_ PP2
|
|__ P2
   |_PP3
   |_PP4

PP1, PP2, PP3 and PP4 are sub-projects and each have their own build.gradle files; P1 and P2 also have build.gradle and settings.gradle files.

How can I declare PP1 as a compile dependency in PP3's build.gradle file?

apply plugin: 'java' 
dependencies {
    compile('P1:PP1') //does not work
    compile group: 'P1', name: 'PP1', version: '0.1' // jar library dependency not an option

    compile('{ant_target}')? //follow up question - an ant target as a dependency
}

I'm running Gradle v1.2

Community
  • 1
  • 1
meja
  • 479
  • 1
  • 6
  • 11
  • 1
    Possible duplicate of [Is it possible to set up a gradle project with more than 2 levels?](http://stackoverflow.com/questions/15299004/is-it-possible-to-set-up-a-gradle-project-with-more-than-2-levels) – Amanuel Nega Nov 22 '16 at 15:31

1 Answers1

55

A build can only have a single settings.gradle file. Assuming settings.gradle is in the root directory and includes projects like so:

include "P1:PP1"

You can add a compile dependency like so:

dependencies {
    compile(project(":P1:PP1"))
}

For more information, see the "Multi-Project Builds" chapter in the Gradle User Guide.

darrenp
  • 4,265
  • 2
  • 26
  • 22
Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • Thanks! On a related question, is it possible to have an Ant target declared as a dependency in the build.gradle dependencies closure? Scenario: P2's build.gradle file imports P2's build.xml; "myAntTask" is a target in build.xml; the project settings.gradle has `include 'P1:PP1', 'P2'`; I can execute P2's "myAntTask" ant target using Gradle. Sticking with my compile dependency requirement, how can I declare "myAntTask" as a compile dependency in P1's build.gradle? I'm slowly converting my Ant built project to Gradle while also adding new Gradle projects – meja Nov 29 '12 at 23:27
  • Sounds like you are confusing task dependencies and artifact dependencies. – Peter Niederwieser Nov 29 '12 at 23:58
  • Didn't answer the question "How can I declare PP1 as a compile dependency in PP3's build.gradle file?". This solution only shows P1 and PP1. – Johann Feb 21 '18 at 12:43
  • Settings file 'C:\Workspace\MyDependentProject\settings.gradle' line: 24 A problem occurred evaluating settings 'MyDependentProject'. Project with path 'DependencyProject' could not be found. – User Jul 26 '18 at 10:45
  • 1
    late to the party, but for anyone new coming along: Gradle 4.10 now supports nested (composite) builds, so we can now have a build within a build within a build etc, each with their own settings file – Andy Dingfelder Aug 20 '18 at 22:26
  • Hey @AndyDingfelder, when should we prefer a multi projects having multiple settings.gradle over simple multi module build with only one settings.gradle? What is the idea behind multi project build, any usecase? – abitcode Mar 28 '20 at 10:45
  • 1
    @AndyDingfelder really? it seems still only single `settings.gradle` is allowed? – Vojtěch Jan 02 '21 at 08:29