Gradle cannot find a project in a dependency chain

4.8k views Asked by At

I have 3 Java projects located next to each other in the folder structure: Proj1, Proj2 and Proj3. Proj1 depends on Proj2 and it depends on Proj3.

Here are their settings.gradle files:

rootProject.name = 'Proj3'

rootProject.name = 'Proj2'

include ':Proj3'
project(':Proj3').projectDir = new File(settingsDir, '../Proj3')

rootProject.name = 'Proj1'

include ':Proj2'
project(':Proj2').projectDir = new File(settingsDir, '../Proj2')

and their gradle.build files:

For 2

apply plugin: 'java'

dependencies {
    compile project(':Proj3')
}

For 1

apply plugin: 'java'

dependencies {
    compile project(':Proj2')
}

The 2nd and 3rd projects are built fine by Gradle but when I try to build the 1st it complains that

A problem occurred evaluating project ':Proj2:'. Project with path ':Proj3' could not be found in project ':Proj2'.

and points to the line compile project(':Proj3') (in the build of Proj2). Stack trace starts with

org.gradle.tooling.BuildException: Could not fetch model of type 'EclipseProject' using Gradle distribution 'https://services.gradle.org/distributions/gradle-3.1-bin.zip'.

Don't know why the 2nd project can find the 3rd fine when it's built and suddenly not when the 1st is built. Why would the 1st care about how the 2nd searches for the 3rd after its already included in the "Project and External Dependencies" folder.

How do I make it work?

2

There are 2 answers

4
Stanislav On BEST ANSWER

I think, you've maid a wrong project configuration. You can have only one settings.gradle file for one multimodule project and this single file should include all modules.

So the structure might be something like this:

RootProject/
  build.gradle
  settings.gradle
  Proj1/
    build.gradle
  Proj2/
    build.gradle
  Proj3/
    build.gradle

And settings.gradle should contains atleast follows:

include 'Proj1', 'Proj2', 'Proj3'

In that case, it seems, you don't need to have some extra settings to change module name or it's directory. You can read about it in the official user guide.

1
Mark On

I found how to do it without needing to publish anything or changing the directory structure.

Project 3 does not need a settings.gradle or dependencies in it build.gradle.

Project 2 needs settings.gradle:

include ':Proj3'
project(':Proj3').projectDir = new File(settingsDir, '../Proj3')

and build,gradle:

dependencies {
    compile project(':Proj3')
}

Project 1 needs settings.gradle:

include ':Proj3', ':Proj2'
project(':Proj3').projectDir = new File(settingsDir, '../Proj3')
project(':Proj2').projectDir = new File(settingsDir, '../Proj2')

and build,gradle:

dependencies {
    compile project(':Proj2')
}

And that's the key! Project 1 while only depending on Project 2 needs to include and specify project 3 also in its settings. Then all works fine.