1

I have 3 separate Gradle projects - x depends on y depending on z.

x/settings.gradle

include ':y'
project(':y').projectDir = new File('../y')

x/build.gradle

apply plugin: 'java'

y/settings.gradle

include ':z'
project(':z').projectDir = new File('../z')

y/build.gradle

apply plugin: 'java'
dependencies {
    compile project(':z')
}

When I run gradlew build in x directory, I get error

* Where:
Build file 'C:\Temp\idea\y\build.gradle' line: 4

* What went wrong:
A problem occurred evaluating project ':y'.
  Project with path ':z' could not be found in project ':y'.

But running gradlew build in y directory is successful.

How should I configure x and y to avoid y's dependencies affect x?

Alexey
  • 2,980
  • 4
  • 30
  • 53

2 Answers2

0

This is not how multi project builds work in Gradle.

In short, you cannot have multiple settings.gradle, only one at the root of the parent project.

In that settings file, you will define the different modules:

include ':x', ':y', ':z'

Note that the above implicitly assumes the 3 projects are located in folders named x, y and Z as siblings of the settings.gradle file.

The Gradle wrapper should be also defined at that root level and nowhere else.

And without changing the build.gradle file in each project, invoking ./gradlew build in that root folder should produce what you expect.

Have a look at the Gradle documentation on multi project builds for more details.

Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43
0

I have the same project dependency setup x -> y -> z and had to use the following build.gradle and settings.gradle to get each project to build on its own and run own tests:

z build.gradle has no internal project dependencies or settings.gradle

y build.gradle has

dependencies {
  implementation project(':z')

y settings.gradle has

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

x build.gradle has

dependencies {
  implementation project(':y')
  implementation project(':z')

x settings.gradle has

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

This is the only way, so far, that I could get project y to build independently and run tests, and then get project x to build independently and run tests. Otherwise, without the z in the x settings.gradle I was getting:

Project with path ':z' could not be found in root project 'x'.

and without the z in the x build.gradle getting:

package does not exist

and

cannot find symbol

This and this question helped as I also added to x and y build.gradle files

bootJar {
  enabled = false
}

jar {
  enabled = true
}
rupweb
  • 3,052
  • 1
  • 30
  • 57