在`module-info.java`和`build.gradle.kts`中都需要指定模块依赖吗?

huangapple go评论89阅读模式
英文:

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

applicationutilitieslist 是项目,在每个项目内部都有一个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 都需要列出依赖关系。

例如,applicationbuild.gradle.kts 包含:

plugins {
    application
}

dependencies {
    implementation(project(":utilities"))
}

..

为什么 applicationbuild.gradle.kts 必须包含 implementation(project(":utilities"))

因为 application 内部的 module-info.java 已经指定了该模块依赖于什么。

这看起来不像是重复吗?

是否意味着依赖必须同时在 module-info.javabuild.gradle.kts 中指定?

英文:

This is my multi-project Gradle setup:

.
└── gradle-module-project
 &#160;&#160; ├── application
 &#160;&#160; ├── build.gradle.kts
 &#160;&#160; ├── gradlew
 &#160;&#160; ├── list
 &#160;&#160; ├── settings.gradle.kts
 &#160;&#160; └── 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&lt;JavaPlugin&gt;().configureEach {
        configure&lt;JavaPluginExtension&gt; {
            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(&quot;:utilities&quot;))
}

..

Why does build.gradle.kts for application have to include implementation(project(&quot;:utilities&quot;))?

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(&#39;:easytext.analysis&#39;)
}

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.

huangapple
  • 本文由 发表于 2020年7月30日 07:33:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63163996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定