简体   繁体   English

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

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

I'm trying to add a feature into a multi-module Gradle Java project (it's spring-integration if you're curious). 我正在尝试将功能添加到多模块Gradle Java项目中(如果您好奇的话,它是Spring集成的)。 I've forked the project onto my local machine and am creating a separate Gradle project that references this locally cloned project for development. 我已将该项目分叉到本地计算机上,并且正在创建一个单独的Gradle项目,该项目引用此本地克隆的项目进行开发。

In my new project the settings.gradle file looks like this: 在我的新项目中,settings.gradle文件如下所示:

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

rootProject.name = 'sprinttest'

and I reference it from my build.gradle with: 我从build.gradle中引用了它:

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

The problem is that it seems to only consider the build.gradle of that /git/spring-integration directory and not its settings.gradle. 问题在于,它似乎仅考虑该/ git / spring-integration目录的build.gradle,而不考虑其settings.gradle。 That's an issue because there quite a few submodules that are referenced via its settings.gradle file that don't get picked up when I run my local project. 这是一个问题,因为有很多子模块通过它的settings.gradle文件引用,当我运行本地项目时这些子模块没有被拾取。 The error I get is: 我得到的错误是:

  • What went wrong: A problem occurred evaluating project ':springint'. 出了什么问题:评估项目':springint'时发生问题。 Project with path 'spring-integration-test-support' could not be found in project ':springint'. 在项目':springint'中找不到路径为'spring-integration-test-support'的项目。

If you look at spring-integration's settings.gradle you see that it includes all these subprojects dynamically: 如果查看spring-integration的settings.gradle您会看到它动态包含所有这些子项目:

rootProject.name = 'spring-integration'

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

I would think that gradle would automatically incorporate the settings.gradle of this dependency at build time but it's not, as this is why, for example, spring-integrationtest-support is not found. 我认为gradle会在构建时自动合并此依赖项的settings.gradle,但事实并非如此,因为这就是为什么例如找不到spring-integrationtest-support的原因。 Am I missing something, or should I "recreate" the same logic from this project's settings into my own? 我是否缺少某些东西,还是应该从该项目的设置中“重新创建”相同的逻辑到自己的逻辑中?

You can only define one and only one settings.gradle file. 您只能定义一个,也只能定义一个settings.gradle文件。

https://docs.gradle.org/current/userguide/userguide_single.html#sec:defining_a_multiproject_build 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 设置文件位于源树的根目录中 ,并指定要包含在构建中的项目

If you want to include a subproject within a subproject, use this syntax : 如果要在子项目中包含子项目,请使用以下语法:

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

It can look like that : 它看起来像这样:

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}"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM