0

I'm trying to write a gradle build with 3 modules (A,B,C). A depends on B and B depends on C (A->B->C). All modules lie in the same parent directory.

All off the modules can either be present in the workspace or not, this is why my settingsB.gradle looks like this:

if(new File("../C").exists()){
  include 'C'
  project(":C").projectDir = file("../C")
}

within the dependencies I do the same check:

    dependencies {
      if(new File("../C").exists()) {
        compile project(':C')
      }else {
        compile "com.mycompany.C:0.0.14"
      }

This setup works perfectly, but not with the toplevel module A. If I add B to A, in the same way like above, I get an error while building project A:

Build file 'C:\projects\A\build.gradle' line: 44 A problem occurred evaluating project ':B'. Project with path ':C' could not be found in project ':B'.

The only way to solve this is to add module C to the settings.gradle of project A.

Is there any other way to solve the error ?

Thank you in advance

Ludi
  • 433
  • 2
  • 16
  • It might be better to use [source dependencies](https://blog.gradle.org/introducing-source-dependencies) for this. – Bjørn Vester Oct 21 '20 at 13:20
  • interessing idea, I will try it out,thank's – Ludi Oct 21 '20 at 13:26
  • your comment leed to using a compsite build, thank you ! – Ludi Oct 21 '20 at 13:32
  • Btw, I believe the reason the first approach with `include` doesn't work is because it defines a normal multi-project build, and these can only have a single settings file. So the settings files from the included projects are ignored. Composite builds use them though. – Bjørn Vester Oct 21 '20 at 14:38
  • that makes sense :) – Ludi Oct 22 '20 at 06:11

1 Answers1

0

The solution is a compostie build (https://docs.gradle.org/current/userguide/composite_builds.html)

settingsA.gradle

includeBuild('../B') {
  dependencySubstitution {
    substitute module('com.mycompany:B') with project(':')
  }
}

settingsB.gradle

includeBuild('../C') {
  dependencySubstitution {
    substitute module('com.mycompany:C') with project(':')
  }
}
Ludi
  • 433
  • 2
  • 16