问题描述
我尝试运行一个使用Java 9模块的非常简单的gradle项目,但收到以下错误.
I try to run a very simple gradle project which uses java 9 modules, but i receive the following error.
/home/vadim/IdeaProjects/test_modules/src/main/java/module-info.java:2: error: module not found: HdrHistogram
requires HdrHistogram;
^
这里是 https://github.com/vad0/test_modules .主类基本上什么都不做.
Here is it https://github.com/vad0/test_modules.The main class does basically nothing.
package app;
import org.HdrHistogram.Histogram;
public class RunHdr {
public static void main(String[] args) {
final Histogram histogram = new Histogram(5);
System.out.println(histogram);
}
}
它仅使用一种依赖性:HdrHistogram.根据官方gradle教程 https://docs,我在build.gradle中包含了此魔术命令. gradle.org/current/samples/sample_java_modules_multi_project.html .
It uses only one dependency: HdrHistogram. I included this magic command in build.gradle according to official gradle tutorial https://docs.gradle.org/current/samples/sample_java_modules_multi_project.html.
java {
modularity.inferModulePath = true
}
整个build.gradle看起来像这样.
The whole build.gradle looks like this.
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
java {
modularity.inferModulePath = true
}
dependencies {
compile group: 'org.hdrhistogram', name: 'HdrHistogram', version: '2.1.12'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
module.info看起来像这样
module.info looks like this
module test.modules.main {
requires HdrHistogram;
}
我已经阅读了许多有关Jigsaw的教程以及与之相关的一大堆stackoverflow问题,但是仍然无法使这个简单的示例生效.我该如何解决?
I have already read a number of tutorials on Jigsaw and a whole bunch of stackoverflow questions related to it, but still can't make this simple example work. How do i fix it?
谢谢
推荐答案
不幸的是,gradle
并未将每个jar都视为一个模块(简单来说).如果要了解确切是如何gradle
构建module-path
(而不是class-path
)的,则可能要从此处,特别是在 isModuleJar 方法.很容易理解(尽管花了我近两天的时间来设置gradle和调试问题),您要使用的依赖项:gradle表示它不是模块(没错,但是我也不知道它是正确的).要使其非常正确,请gradle
会将您的依赖项添加到CLASSPATH ,但在下一行:它将 not 将您的依赖项添加到module-path
,因为如果isModuleJar
中的过滤器失败.
Unfortunately, gradle
does not treat every jar as a module (in simple words). If you want to find out how exactly is gradle
building the module-path
(as opposed to class-path
), you probably want to start from here, specifically at the isModuleJar method. It's pretty easy to understand (though it took me almost two days to set-up gradle and debug the problem out) that the dependency that you are trying to use : gradle says that it is not a module (it isn't wrong, but I am not sure it is correct either). To make it very correct, gradle
will add your dependency to the CLASSPATH, but in the very next line: it will not add your dependency to the module-path
, because if fails the filter in isModuleJar
.
我不知道这是否是错误,或者是否是故意的,但是解决方案很简单:
I do not know if this is a bug or not, or may be this is on purpose, but the solution is easy:
plugins.withType(JavaPlugin).configureEach {
java {
modularity.inferModulePath = true
}
tasks.withType(JavaCompile) {
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
]
classpath = files()
}
}
您将其故意添加到路径中.我将其标记为缺陷,让我们看看他们怎么说.
you add it to the path, on purpose. I will flag this as a defect and let's see what they have to say.
编辑
更好的是,使用由gradle提交者编写的插件:
Even better, use a plugin that is written by a gradle commiter:
plugins {
id 'java'
id 'de.jjohannes.extra-java-module-info' version "0.1"
}
最简单的选择是:
extraJavaModuleInfo {
automaticModule("HdrHistogram-2.1.12.jar", "HdrHistogram")
}
这篇关于找不到gradle拼图模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!