Maven surefire and JDK 11

11

我正在尝试让Maven Surefire在JDK 11下运行,但是我一直收到以下错误:

  1. 如果我将reuseForks设置为true:

  Error occurred in starting fork, check output in log
  Process Exit Code: 1
  at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:670)
  at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:283)
  at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:246)
如果我将它设置为false:
Execution default-test of goal org.apache.maven.plugins:maven-surefire-   plugin:3.0.0-M1:test
failed: java.lang.ClassNotFoundException: org.apache.maven.plugin.surefire.StartupReportConfiguration

我找到了 这个这个 链接,它们描述了同样的问题,但都没有解决方法。

为了复现此错误,我创建了这个git仓库


你尝试过使用旧版本的surefire插件吗,例如2.21.0? - gjoranv
@gjoranv 是的,我有过同样的问题,在2.21.0上。 - Gnas
由于某种原因,“module-info”类正在干扰测试。我已经将其删除,测试正常工作。可能插件还没有准备好用于模块化项目?此外,模块本身存在问题(可能是由于错误的openjfx-monocle依赖关系)。 - José Pereda
2个回答

15

使用模块化项目进行测试时,似乎需要将forkCount设置为0

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M1</version>
    <configuration>
        <forkCount>0</forkCount> <!-- changed this to 0 -->
        <reuseForks>false</reuseForks>
        <!-- <threadCount>1</threadCount> --> <!-- shall be used with 'parallel' -->
        <printSummary>true</printSummary>
        <!-- <skipTests>false</skipTests> --> <!-- defaults to false -->

        <!-- run test in headless mode -->
        <systemPropertyVariables>
            <glass.platform>Monocle</glass.platform>
            <monocle.platform>Headless</monocle.platform>
            <prism.order>d3d</prism.order>
        </systemPropertyVariables>

        <argLine>
            --add-exports javafx.graphics/com.sun.javafx.application=ALL-UNNAMED
            --add-exports javafx.graphics/com.sun.glass.ui=ALL-UNNAMED
        </argLine>
    </configuration>
</plugin>

引用此文章

当存在module-info.java并启用了分叉进程时,Surefire会创建一个混合类路径,其中包含模块和未命名模块,导致模块可见性问题并防止应用程序启动。


注意:禁用forkCountreuseForks配置参数会导致抛出org.apache.maven.surefire.booter.SurefireBooterForkException,类似于SUREFIRE-1528中报告的异常。

如果这可以帮助Maven社区的开发人员,同一运行的执行转储如下:

# Created at 2018-11-23T09:31:53.631
Corrupted STDOUT by directly writing to native stream in forked JVM 1. Stream 'Error occurred during initialization of boot layer'.
java.lang.IllegalArgumentException: Stream stdin corrupted. Expected comma after third character in command 'Error occurred during initialization of boot layer'.
    at org.apache.maven.plugin.surefire.booterclient.output.ForkClient$OperationalData.<init>(ForkClient.java:507)
    at org.apache.maven.plugin.surefire.booterclient.output.ForkClient.processLine(ForkClient.java:210)
    at org.apache.maven.plugin.surefire.booterclient.output.ForkClient.consumeLine(ForkClient.java:177)
    at org.apache.maven.plugin.surefire.booterclient.output.ThreadedStreamConsumer$Pumper.run(ThreadedStreamConsumer.java:88)
    at java.base/java.lang.Thread.run(Thread.java:834)

这是源代码库中的一行代码。


1
尽管我理解这将导致使用类路径而不是模块路径运行测试。 - Naman
这意味着并行的Maven执行是不可能的吗?因为-T1CforkCount 0不能结合使用。 - Dormouse
1
发现了https://winterbe.com/posts/2018/08/29/migrate-maven-projects-to-java-11-jigsaw/,该链接解决了多线程问题。 - Dormouse
@Dormouse,实现帖子中的建议如何解决多线程问题?我没有看到任何关于线程的参考。 - Eric
正是 --illegal-access=permit 这个参数,被 Cesar 巧妙地转化为了一个答案,而我似乎太懒了。 - Dormouse

6
我在这里找到了解决方案:

https://winterbe.com/posts/2018/08/29/migrate-maven-projects-to-java-11-jigsaw/

我必须添加。
<argLine>
    --illegal-access=permit
</argLine>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.0</version>
    <configuration>
        <argLine>
            --illegal-access=permit
        </argLine>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.22.0</version>
    <configuration>
        <argLine>
            --illegal-access=permit
        </argLine>
    </configuration>
</plugin>


1
这在我的spring-boot项目中没有起作用。我不得不说在2.21.0版本。不确定为什么。否则我会得到一个编译错误:关于反射API的一些内容。 - djangofan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接