如何在 Gradle 中包含多模块项目?

How do I include a multi-module project in Gradle?

我正在尝试将功能添加到多模块 Gradle Java 项目中(如果您好奇的话,它是 spring-集成)。我已经将该项目 fork 到我的本地机器上,并正在创建一个单独的 Gradle 项目,该项目引用这个本地克隆的项目进行开发。

在我的新项目中,settings.gradle 文件如下所示:

include ":springint"
project(":springint").projectDir = file("/users/me/git/spring-integration")

rootProject.name = 'sprinttest'

我从我的 build.gradle 中引用了它:

dependencies {
    compile project(":springint")
...

问题是它似乎只考虑 /git/spring-integration 目录的 build.gradle 而不是它的 settings.gradle。这是一个问题,因为有相当多的子模块是通过其 settings.gradle 文件引用的,而当我 运行 我的本地项目时,这些子模块没有被拾取。我得到的错误是:

  • What went wrong: A problem occurred evaluating project ':springint'. Project with path 'spring-integration-test-support' could not be found in project ':springint'.

如果您查看 spring-integration 的 settings.gradle,您会发现它动态地包含所有这些子项目:

rootProject.name = 'spring-integration'

rootDir.eachDir { dir ->
    if (dir.name.startsWith('spring-integration-')) {
        include ":${dir.name}"
    }
}

我认为 gradle 会在构建时自动合并此依赖项的 settings.gradle 但事实并非如此,因为这就是为什么,例如 spring-integrationtest-support 未找到。我是不是遗漏了什么,或者我应该 "recreate" 将此项目设置中的相同逻辑应用到我自己的设置中?

您只能定义一个 settings.gradle 文件。

https://docs.gradle.org/current/userguide/userguide_single.html#sec:defining_a_multiproject_build

To define a multi-project build, you need to create a settings file. The settings file lives in the root directory of the source tree, and specifies which projects to include in the build

如果要在子项目中包含子项目,请使用以下语法:

include "a-subproject:an-inner-subproject"

它看起来像这样:

include "springint"
project(":springint").projectDir = file("/users/me/git/spring-integration")

rootProject.name = 'sprinttest'

project(":springint").projectDir.eachDir { dir ->
    if (dir.name.startsWith('spring-integration-')) {
        include "springint:${dir.name}"
    }
}