unnamed module @0x7cd3a5 error while running tests for java application(with module system enabled) using gradle

huangapple 未分类评论43阅读模式
英文:

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 &#39;org.springframework.boot:spring-boot-starter-web&#39;

testImplementation(&#39;org.springframework.boot:spring-boot-starter-test&#39;) {
    exclude group: &#39;org.junit.vintage&#39;, module: &#39;junit-vintage-engine&#39;
    exclude group: &#39;com.vaadin.external.google&#39;, module: &#39;android-json&#39;
}

}

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: &quot;true&quot;, destfile: jar.archivePath, keepcompression: true) {
    fileset(dir: &quot;$jlinkBasePath&quot;, includes: &#39;META-INF/**&#39;)
}

}

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 &#39;ch.qos.logback.classic.spi.Configurator&#39;

    excludeRequires &#39;com.fasterxml.jackson.module.paramnames&#39;
    excludeProvides implementation: &#39;com.sun.xml.bind.v2.ContextFactory&#39;
    excludeProvides servicePattern: &#39;javax.enterprise.inject.*&#39;
    excludeProvides service: &#39;org.apache.logging.log4j.spi.Provider&#39;
    excludeProvides servicePattern: &#39;reactor.blockhound.integration.*&#39;
}
launcher {
    name = &#39;run&#39;
    jvmArgs = [
            &#39;--add-reads&#39;, &#39;registration.service.merged.module=com.sudhir.registration&#39;,
            &#39;-cp&#39;, &#39;config/&#39;,
    ]
}

}

tasks.jlink.doLast {
copy {
from "src/main/resources"
into "$imageDir.asFile/bin/config"
}

copy {
    from &quot;$buildDir/classes/java/main/com/sudhir/registration&quot;
    into &quot;$imageDir.asFile/bin/config/com/sudhir/registration/for-spring-classpath-scanner&quot;
}

}



However, I am able to run specific tests in IntelliJ. I think it&#39;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 设置添加到测试任务)

英文:

Not quite sure on the reason of this problem. These threads may help to understand it: this, this, this or this

But this is what helped me in a similar case:

test {
    moduleOptions {
        runOnClasspath = true
    }
    useJUnitPlatform()
}

(Adding moduleOptions setting to the test task)

huangapple
  • 本文由 发表于 2020年7月27日 00:43:41
  • 转载请务必保留本文链接:https://java.coder-hub.com/63102983.html
匿名

发表评论

匿名网友

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

确定