英文:
Gradle Multi-Module groovy 'Project' object not getting evaluated in helper files
问题
我有一个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"
。为什么会这样?我如何在帮助文件中提供设置?
英文:
I have a multi-module setup for a Java project with following structure.
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
And in my settings.gradle
I've specified.
rootProject.name = 'mainApp'
include 'core-module'
include 'lib-module'
include 'lib-another-module'
in my mainApp/build.gradle
file, I've written a helper as helper-tasks.gradle
and imported that file in my $rootDir/build.gradle
file as
apply from: "$rootDir/helpers/helper-tasks.gradle"
task('bundleJar', type: Jar)
is a part of subprojects { subproject -> }
closure and I'll be just calling ./gradlew bundleJar
. Here expectations are it should iterate through each sub-project and collect the classes for my final bundle
in helper-tasks.gradle
file I wrote a gradle tasks
as below
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
}
Here project('lib-module').sourceSets.main.output.classesDirs
doesn't pick-up relative path of sub-module project $rootDir/lib-module/build/classes/java/main
but still referring to "$rootDir/build/classes/java/main
. Why? and how can I provide my settings to helper files?
答案1
得分: 1
这里有一个在GitHub上的工作示例(链接:https://github.com/codetojoy/easter_eggs_for_gradle/tree/master/egg_StackOverflow_64555185)。
请查看根目录下的 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
文件。
英文:
There is a working example here on GitHub.
Consider the build.gradle
at the root:
subprojects {
apply plugin: 'java-library'
apply from: "${rootProject.projectDir}/helpers/helper-tasks.gradle"
}
and 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
}
Note that the name of the file now matches the project name. In the example, gradle lib-module:bundleJar
will generate ./lib-module/build/libs/lib-module.jar
with appropriate classes.
Similarly, gradle lib-another-module:bundleJar
will generate ./lib-another-module/build/libs/lib-another-module.jar
(with appropriate classes).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论