Gradle子子项目的构建配置

问题描述

我正在尝试编写具有3个模块(A,B,C)的gradle构建。 A取决于B,B取决于C(A-> B-> C)。所有模块都位于同一父目录中。

所有模块都可以存在于工作区中,这就是为什么我的设置 B .gradle如下所示:

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

在依赖项中,我执行相同的检查:

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

此设置可以完美运行,但不适用于顶级模块A。如果我将B添加到A,则与上述相同,在构建项目A时会出错:

构建文件'C:\ projects \ A \ build.gradle'行:44 评估项目':B'时发生问题。 在项目':B'中找不到路径为':C'的项目。

解决此问题的唯一方法是将模块C添加到项目A的settings.gradle。

还有其他解决方法吗?

提前谢谢

解决方法

解决方案是堆肥(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(':')
  }
}