我的Java项目结构如下:
- root-directory (has no .gradle file)
- project_1
- build.gradle
- settings.gradle
- project_2
- build.gradle
- settings.gradle
- project_3
- build.gradle
- settings.gradle
project_2
依赖于project_1
,此依赖关系定义如下:
- project_2/build.gradle
implementation(project(":project_1"))
- project_2/settings.gradle
include(":project_1")
project(":project_1").projectDir = new File("../project_1")
而且,project_3
依赖于project_2
,此依赖关系定义如下:
- project_3/build.gradle
implementation(project(":project_2"))
- project_3/settings.gradle
include(":project_2")
project(":project_2").projectDir = new File("../project_2")
现在,当我通过在project_3
目录中运行>> ./gradlew build
终端命令来构建./project_3/
时,会发生以下错误:
A problem occurred evaluating project ':project_2'.
> Project with path ':project_1' could not be found in project ':project_2'.
我希望能够从其自己的目录构建每个项目。我该如何解决这个问题?
更新:我尝试通过添加以下内容将root-directory
转换为项目根目录:
- root-directory/settings.gradle
rootProject.name = "root"
include(":project_1", ":project_2", ":project_3")
- root-directory/build.gradle
// Nothing in this file
然后从所有子项目的project(":project_#").projectDir = new File("../project_#")
文件中删除settings.gradle
行。但是即使如此,每个项目目录中的build命令也无法使用。
答案 0 :(得分:1)
请勿在模块级include(":project_1/2/3")
中定义build gradle
,而应使用根项目的settings.gradle
来定义此内容...
include ":project_1", ":project_2", ":project_3"
rootProject.name = "SomeApp"
,然后您可以在模块级build.gradle
中引用这些子项目:
dependencies {
api project(':project_1')
implementation project(':project_2')
}
答案 1 :(得分:0)
项目依赖项需要一个项目路径,而不仅仅是项目名称:
dependencies {
implementation project(":project_1")
}
在项目名称前注意:
:它将项目名称转换为相对于根项目的项目路径。
答案 2 :(得分:0)
就我而言,我有一些不是 Java 项目的子项目。我必须这样做
allprojects {
apply plugin: 'java'
}
在 build.gradle 中