我有一个具有以下结构的 Java 项目的多模块设置。
mainApp
|--> core-module
| |--> src
| |--> build.gradle
| |--> gradle.properties
|
|--> lib-module
| |--> src
| |--> build.gradle
| |--> gradle.properties
|--> lib-another-module
| |--> src
| |--> build.gradle
| |--> gradle.properties
|--> settings.gradle
|--> build.gradle
在我的settings.gradle
我已经指定了。rootProject.name = 'mainApp'
include 'core-module'
include 'lib-module'
include 'lib-another-module'
在我的mainApp/build.gradle
文件,我写了一个助手为helper-tasks.gradle
并将该文件导入我的$rootDir/build.gradle
文件为apply from: "$rootDir/helpers/helper-tasks.gradle"
task('bundleJar', type: Jar)
是 subprojects { subproject -> }
的一部分关闭,我会调用 ./gradlew bundleJar
.这里期望它应该遍历每个子项目并为我的最终包收集类在
helper-tasks.gradle
文件我写了 gradle tasks
如下task('bundleJar', type: Jar) {
manifest {
attributes 'Manifest-Version': "1.0"
attributes 'Built-By': USER_NAME
attributes 'Created-By': "Java Version: " + JAVA_VERSION
attributes 'Implementation-Version': BUNDLE_VERSION + " Build " + GIT_COMMIT_HASH
attributes 'Build-Timestamp': BUILD_TIME
}
baseName('lib-module')
from project('lib-module').sourceSets.main.output.classesDirs
}
这里project('lib-module').sourceSets.main.output.classesDirs
不获取子模块项目的相对路径$rootDir/lib-module/build/classes/java/main
但仍指"$rootDir/build/classes/java/main
.为什么?以及如何将我的设置提供给帮助文件?
最佳答案
有一个工作示例 here on GitHub .
考虑 build.gradle
在根:
subprojects {
apply plugin: 'java-library'
apply from: "${rootProject.projectDir}/helpers/helper-tasks.gradle"
}
和 helpers/helper-tasks.gradle
:task('bundleJar', type: Jar) {
manifest {
attributes 'Manifest-Version': "1.0"
attributes 'Built-By': USER_NAME
attributes 'Created-By': "Java Version: " + JAVA_VERSION
attributes 'Implementation-Version': BUNDLE_VERSION + " Build " + GIT_COMMIT_HASH
attributes 'Build-Timestamp': BUILD_TIME
}
baseName("${project.name}")
from project.sourceSets.main.output.classesDirs
}
请注意,文件名现在与项目名称匹配。在示例中,gradle lib-module:bundleJar
将生成 ./lib-module/build/libs/lib-module.jar
与适当的类。同样,
gradle lib-another-module:bundleJar
将生成 ./lib-another-module/build/libs/lib-another-module.jar
(与适当的类)。
关于java - Gradle Multi-Module groovy 'Project' 对象未在帮助文件中进行评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64555185/