英文:
Is it a requirement to specify module dependencies in both module-info.java and build.gradle.kts
问题
这是我的多项目Gradle设置:
.
└── gradle-module-project
├── application
├── build.gradle.kts
├── gradlew
├── list
├── settings.gradle.kts
└── utilities
application
、utilities
和 list
是项目,在每个项目内部都有一个Java模块。
换句话说,每个项目都有自己的 src/main/java/module-info.java
文件和 build.gradle.kts
文件。
application
模块依赖于 utilities
模块,而 utilities
模块依赖于 list
模块。
根目录下的 build.gradle.kts
包含以下内容:
subprojects {
..
plugins.withType<JavaPlugin>().configureEach {
configure<JavaPluginExtension> {
modularity.inferModulePath.set(true)
}
}
..
}
我觉得奇怪的是,在每个子项目内,build.gradle.kts
都需要列出依赖关系。
例如,application
的 build.gradle.kts
包含:
plugins {
application
}
dependencies {
implementation(project(":utilities"))
}
..
为什么 application
的 build.gradle.kts
必须包含 implementation(project(":utilities"))
?
因为 application
内部的 module-info.java
已经指定了该模块依赖于什么。
这看起来不像是重复吗?
是否意味着依赖必须同时在 module-info.java
和 build.gradle.kts
中指定?
英文:
This is my multi-project Gradle setup:
.
└── gradle-module-project
   ├── application
   ├── build.gradle.kts
   ├── gradlew
   ├── list
   ├── settings.gradle.kts
   └── utilities
application
, utilties
and list
are projects, and inside each one there is a Java Module.
That is, each project has its own src/main/java/module-info.java
file and build.gradle.kts
file.
application
module depends on utilities
module, and utilities
module depends on list
module.
build.gradle.kts
at the root contains the following:
subprojects {
..
plugins.withType<JavaPlugin>().configureEach {
configure<JavaPluginExtension> {
modularity.inferModulePath.set(true)
}
}
..
}
What I find strange is that inside each sub-project, the build.gradle.kts
is required to list the dependencies.
For example, application
-> build.gradle.kts
contains:
plugins {
application
}
dependencies {
implementation(project(":utilities"))
}
..
Why does build.gradle.kts
for application
have to include implementation(project(":utilities"))
?
When the module-info.java
inside application
already specifies what that module depends on.
Doesn't this seem like duplication?
Is it intended that the dependency must be specified in both module-info.java
and build.gradle.kts
?
答案1
得分: 1
当使用Gradle与Java模块时,必须两次指定依赖项:一次是为了模块系统的好处,另一次是为了构建工具的好处。
我一直在跟随Sandor Mak和Paul Bakker的Java 9模块化中的Easy Text演示应用程序,这是一个多模块项目。一个模块的module-info.java
如下:
module easytext.cli {
requires easytext.analysis;
}
当我在Gradle中包装它时,在我的build.gradle
文件中,我还需要:
dependencies {
implementation project(':easytext.analysis')
}
也许只是时间的问题,工具会跟上,我们就不必两次做了。
还要查看Paul Bakker的JavaOne演示使用Java构建JPMS模块,他在其中详细介绍了使用Gradle构建多模块Java/Groovy/Kotlin项目的细节。
英文:
When using Java Modules with Gradle, dependencies have to be specified twice: once for the benefit of the module system and again for the benefit of the build tool.
I've been following along with the Easy Text demo application in Java 9 Modularity by Sandor Mak and Paul Bakker, which is a multi-module project. The module-info.java
for one module is:
module easytext.cli {
requires easytext.analysis;
}
When I wrap it in Gradle, in my build.gradle
file I also need:
dependencies {
implementation project(':easytext.analysis')
}
Perhaps it's just a matter of time before the tooling catches up and we won't have to do both.
Also check out Paul Bakker's JavaOne presentation Building JPMS Modules with Java in which he goes through the fine-detail of building multiple-module Java/Groovy/Kotlin projects with Gradle.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论