我有两个 Gradle 项目,其目录结构如下:
/baseDir
/first
/first/build.gradle
/second
/second/build.gradle
/second/settings.gradle
settings.gradle 看起来像这样:
includeFlat "first"
当我执行gradle build
时,第二个项目是在第一个(主)项目之后编译的。我在第一个项目的build.gradle中尝试过:
dependencies {
compile project(':first')
}
在执行gradle build
时,我收到一个错误,即在第一个项目中声明的依赖项无法在第二个项目中找到。
我需要的是以下内容。当我在第二个项目上进行 gradle build 时,我希望首先构建第一个项目,并将 first.jar
用作第二个项目中的依赖项。
我该怎么做?
编辑:当我在第二个项目的 settings.gradle 中设置以下内容时:
includeFlat "first", "second"
然后我在执行graddle clean
时收到以下错误:
FAILURE: Build failed with an exception.
* What went wrong:
Could not select the default project for this build. Multiple projects in this build have project directory '/Users/mg/Documents/Grails/GGTS3.6.3-SR1/second': [:, :second]
> Multiple projects in this build have project directory '/Users/mg/Documents/Grails/GGTS3.6.3-SR1/second': [:, :second]
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
编辑:我创建了一个示例项目表单演示:https://github.com/confile/Gradle-MultiProject-Test
最佳答案
你可以让构建任务依赖于另一个项目的构建任务:
build{
dependsOn ':first:build'
}
尝试此项目设置:
root
settings.gradle
gradle.build
first
gradle.build
second
gradle.build
并将其放入根目录的 settings.gradle 中:
include 'first'
include 'second'
关于java - 如何确保包含的子项目在 Gradle 中的主项目之前运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28384700/