我开始使用JPMS模块对Java 11 Gradle项目进行模块化。为了测试目的,我创建了一个名为TestGradleModular
的示例项目。模块配置看起来不错,可以从eclipse中找到模块,我可以在我的应用程序中使用它们。
当我在eclipse中启动应用程序时,JVM引发异常:
Error occurred during initialization of boot layer
java.lang.module.FindException: Module org.apache.logging.log4j not found, required by TestGradleModular
我查看了项目的运行配置,发现eclipse包含了我在类路径上使用的模块,而不是启动命令行上的模块路径:
C:\Program Files\OpenJDK\jdk-11.0.6+10\bin\javaw.exe -Dfile.encoding=UTF-8 -p "C:\Users\user\workspaces\test\TestGradleModular\bin\main" -classpath "C:\Users\user\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-core\2.11.2\6c2fb3f5b7cd27504726aef1b674b542a0c9cf53\log4j-core-2.11.2.jar;C:\Users\user\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-api\2.11.2\f5e9a2ffca496057d6891a3de65128efc636e26e\log4j-api-2.11.2.jar" -m TestGradleModular/TestGradleModular.TestMe
TestGradleModular
项目包含以下文件:
gradle.build:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'
sourceCompatibility = 11
targetCompatibility = 11
repositories {
mavenCentral()
mavenLocal()
}
plugins.withType(JavaPlugin).configureEach {
java {
modularity.inferModulePath = true
}
}
ext.isConGradle = {entry ->
entry.kind == 'con' && entry.path == 'org.eclipse.buildship.core.gradleclasspathcontainer'
}
tasks.withType(JavaCompile) {
doFirst {
options.compilerArgs += [
'--module-path', classpath.asPath
]
classpath = files()
options.fork = true
options.encoding = compileEncoding
options.debug = compileDebugging
if(compileDebugging){
options.debugOptions = new DebugOptions()
options.debugOptions.setDebugLevel(compilerDebuggingInformation)
}
}
}
eclipse {
project {
natures 'org.eclipse.buildship.core.gradleprojectnature'
}
classpath {
//Necessary otherwise the library is not raised to the module level.
containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
file {
whenMerged {
entries.findAll { isConGradle(it) }.each {
it.entryAttributes['module'] = 'true'
}
}
}
}
}
dependencies{
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.2'
}
src/main/java/module-info.java:
module TestGradleModular{
requires org.apache.logging.log4j;
}
src/main/java/TestGradleModular/TestMe.java:
package TestGradleModular;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class TestMe{
private static final Logger LOGGER = LogManager.getLogger();
public static void main(String[] args){
LOGGER.error("d'oh!");
}
}
Eclipse Java构建路径>库
Modulepath
|> JRE System Library [JavaSE-11]
|> Project and External Dependencies
|> Access rules: No rules defined
|> External annotations: (None)
|> Native library location: No
|> Is modular
|> log4j-core-2.11.2.jar - C:\Users\user\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-core\2.11.2\6c2fb3f5b7cd27504726aef1b674b542a0c9cf53\log4j-core-2.11.2.jar
|> Is modular - non modifiable
|> Access rules: (no restrictions) - non modifiable
|> Visible only for the test sources: No - non modifiable
|> log4j-api-2.11.2.jar - C:\Users\user\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-api\2.11.2\f5e9a2ffca496057d6891a3de65128efc636e26e\log4j-api-2.11.2.jar
|> Is modular - non modifiable
|> Access rules: (no restrictions) - non modifiable
|> Visible only for the test sources: No - non modifiable
Classpath
这是蚀错误还是Gradle配置错误?
我在以下位置进行了测试
Gradle Version 6.4.1
eclipse 2019-12 (4.14.0) x64 with Buildship 3.1.3
eclipse 2020-06 (4.16.0) x64 with Buildship 3.1.4
AdoptOpenJDK 11.0.6+10 x64
编辑:
我在Buildship的问题跟踪器中打开了一个问题: https://github.com/eclipse/buildship/issues/1002