英文:
unnamed module @0x7cd3a5 error while running tests for java application(with module system enabled) using gradle
问题
以下是翻译好的部分:
我正在尝试使用Gradle运行启用模块系统的Java应用程序的测试,并遇到以下错误。
java.lang.IllegalAccessError:类org.junit.platform.launcher.core.LauncherFactory(在无名模块@0x7cd3a5中)无法访问类org.junit.platform.commons.util.Preconditions(在模块org.junit.platform.commons中),因为模块org.junit.platform.commons不会将org.junit.platform.commons.util导出到无名模块@0x7cd3a5中
错误提示:模块org.junit.platform.commons不会将org.junit.platform.commons.util导出。
这是我的构建文件的样子:
import org.springframework.boot.gradle.plugin.SpringBootPlugin
plugins {
id 'java'
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'org.beryx.jlink' version '2.21.0'
id 'org.javamodularity.moduleplugin' version '1.6.0'
}
repositories {
gradlePluginPortal()
jcenter()
}
sourceCompatibility = JavaVersion.VERSION_14
targetCompatibility = JavaVersion.VERSION_14
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
// ... (省略部分代码)
test {
useJUnitPlatform()
}
// ... (省略部分代码)
dependencies {
// ... (省略部分代码)
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
exclude group: 'com.vaadin.external.google', module: 'android-json'
}
}
// ... (省略部分代码)
prepareMergedJarsDir.doLast {
// ... (省略部分代码)
}
jlink {
// ... (省略部分代码)
mergedModule {
// ... (省略部分代码)
}
launcher {
// ... (省略部分代码)
}
}
tasks.jlink.doLast {
copy {
from "src/main/resources"
into "$imageDir.asFile/bin/config"
}
copy {
from "$buildDir/classes/java/main/com/sudhir/registration"
into "$imageDir.asFile/bin/config/com/sudhir/registration/for-spring-classpath-scanner"
}
}
然而,在IntelliJ中,我能够运行特定的测试。我认为这是因为IntelliJ使用了类路径而不是模块路径。
我正在使用gradle-module-system插件进行构建、测试和运行。示例代码可以在这里找到。
有人能否协助我解决这个问题。我对使用Java模块系统非常陌生。
<details>
<summary>英文:</summary>
I am trying to run the test for java applications (with module system enabled) using Gradle and getting the following error.
java.lang.IllegalAccessError: class org.junit.platform.launcher.core.LauncherFactory (in unnamed module @0x7cd3a5) cannot access class org.junit.platform.commons.util.Preconditions (in module org.junit.platform.commons) because module org.junit.platform.commons does not export org.junit.platform.commons.util to unnamed module @0x7cd3a5
Error says **module org.junit.platform.commons does not export org.junit.platform.commons.util**
This is how my build file looks like:
import org.springframework.boot.gradle.plugin.SpringBootPlugin
plugins {
id 'java'
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id "org.beryx.jlink" version "2.21.0"
id "org.javamodularity.moduleplugin" version "1.6.0"
}
repositories {
gradlePluginPortal()
jcenter()
}
sourceCompatibility = JavaVersion.VERSION_14
targetCompatibility = JavaVersion.VERSION_14
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
jar {
enabled = true;
}
application{
mainModule='com.sudhir.registration'
}
test {
useJUnitPlatform()
}
configurations {
springFactoriesHolder { transitive = false }
}
dependencyManagement {
imports {
mavenBom SpringBootPlugin.BOM_COORDINATES
}
}
dependencies {
springFactoriesHolder 'org.springframework.boot:spring-boot-actuator-autoconfigure'
springFactoriesHolder 'org.springframework.boot:spring-boot-autoconfigure'
springFactoriesHolder 'org.springframework.boot:spring-boot'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
exclude group: 'com.vaadin.external.google', module: 'android-json'
}
}
mainClassName = "com.sudhir.registration.ApiApplication"
prepareMergedJarsDir.doLast {
// extract and merge META-INF/spring.factories from springFactoriesHolder
def factories = configurations.springFactoriesHolder.files.collect {
def props = new Properties()
props.load(zipTree(it).matching { include 'META-INF/spring.factories' }.singleFile.newInputStream())
props
}
def mergedProps = new Properties()
factories.each { props ->
props.each { key, value ->
def oldVal = mergedProps[key]
mergedProps[key] = oldVal ? "$oldVal,$value" : value
}
}
def content = mergedProps.collect { key, value ->
def v = (value as String).replace(',', ',\\n')
"$key=$v"
}.join('\n\n')
mkdir("$jlinkBasePath/META-INF")
new File("$jlinkBasePath/META-INF/spring.factories").text = content
// insert META-INF/spring.factories into the main jar
ant.zip(update: "true", destfile: jar.archivePath, keepcompression: true) {
fileset(dir: "$jlinkBasePath", includes: 'META-INF/**')
}
}
jlink {
imageZip = file("$buildDir/image-zip/registration-service-image.zip")
options = ['--strip-java-debug-attributes', '--compress', '2', '--no-header-files', '--no-man-pages']
forceMerge 'jaxb-api', 'byte-buddy', 'classgraph'
mergedModule {
uses 'ch.qos.logback.classic.spi.Configurator'
excludeRequires 'com.fasterxml.jackson.module.paramnames'
excludeProvides implementation: 'com.sun.xml.bind.v2.ContextFactory'
excludeProvides servicePattern: 'javax.enterprise.inject.*'
excludeProvides service: 'org.apache.logging.log4j.spi.Provider'
excludeProvides servicePattern: 'reactor.blockhound.integration.*'
}
launcher {
name = 'run'
jvmArgs = [
'--add-reads', 'registration.service.merged.module=com.sudhir.registration',
'-cp', 'config/',
]
}
}
tasks.jlink.doLast {
copy {
from "src/main/resources"
into "$imageDir.asFile/bin/config"
}
copy {
from "$buildDir/classes/java/main/com/sudhir/registration"
into "$imageDir.asFile/bin/config/com/sudhir/registration/for-spring-classpath-scanner"
}
}
However, I am able to run specific tests in IntelliJ. I think it's because Intellij is using classpath instead of module path.
I am using [gradle-module-system][1] plugin to build, test and run. sample code can be found [here][2].
Can someone please assist me how to deal with this issue. I am very new to using java module system.
[1]: https://github.com/java9-modularity/gradle-modules-plugin
[2]: https://gitlab.yadavsudhir.com/sudhir/registration-service
</details>
# 答案1
**得分**: 0
不太确定这个问题的原因。这些讨论可能有助于理解:[这个][1],[这个][2],[这个][3]或者[这个][4]。
但是以下是在类似情况下对我有帮助的:
```groovy
test {
moduleOptions {
runOnClasspath = true
}
useJUnitPlatform()
}
(将 moduleOptions
设置添加到测试任务)
专注分享java语言的经验与见解,让所有开发者获益!
评论