Maven编译,并为使用surefire-plugin对JPMS模块进行单元测试提供了作用域。

问题描述 投票:0回答:1

试图测试我的maven项目(一个父pom和几个模块(jpms模块)),我得到了恼人的 Error occurred in starting fork:

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M4:test (default-test) on project com.foo.plugin: There are test failures.
...
Error occurred in starting fork, check output in log
Process Exit Code: 1
org.apache.maven.surefire.booter.SurefireBooterForkException: The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
...
Error occurred in starting fork, check output in log
Process Exit Code: 1
    at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:690)
    at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:285)
    at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:248)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1217)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:1063)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:889)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginM

例如,我找到了一个解决办法。此处 建议设置 <forkCount>0</forkCount> 中的surefire-plugin配置。但是,这样的解决方案不允许在模块路径上运行测试,所以我采用了这样的 surefire 发出.

Surefire的开发人员(感谢他们)发现,原因是在我使用的依赖的maven范围内。在我的项目中,我有。

         <dependency>
            <groupId>jakarta.ws.rs</groupId>
            <artifactId>jakarta.ws.rs-api</artifactId>
            <version>2.1.6</version>
            <scope>provided</scope> <!-- NOTE IT -->
        </dependency>
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>2.3.2</version>
            <scope>provided</scope> <!-- NOTE IT -->
        </dependency>

他们说这是必须的。

          <dependency>
            <groupId>jakarta.ws.rs</groupId>
            <artifactId>jakarta.ws.rs-api</artifactId>
            <version>2.1.6</version>
            <scope>compile</scope> <!-- NOTE IT -->
        </dependency>
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>2.3.2</version>
            <scope>compile</scope> <!-- NOTE IT -->
        </dependency>

当我改变了范围后,我的项目在surefire的测试中没有出现问题。这两个依赖项 jakarta.ws.rs-apijakarta.xml.bind-api 是JPMS模块,是 required 由我的JPMS模块。

那么问题来了,我的代码中是否存在问题(provided 是我想为我的JPMS模块运行测试时出了问题)还是问题出在surefire插件上?

java maven maven-surefire-plugin java-module
1个回答
1
投票

注意:你可以自由删除 <scope>compile</scope> 因为在你的POM中没有声明作用域,意味着Maven将使用默认值 compile 反正。

提供的范围是项目架构方面。

它用于应用服务器包含WS API的情况(即使是不同的arfifact文件名)。这样我们就把它标记为 provided 使得WAR文件变小,而且WS API构件不会被包含在内。

如果您也在构建无服务器应用程序,并且您正在构建您自己的容器,那么就不需要使用 provided 是需要声明的。在那里,工件将出现在Fat JAR中,这是你所期望的。

所以你可以看到,标记为provided的依赖关系与classpaths是一致的。它出现在编译器的classpath上,但不会出现在运行时。

在这个范围内还有一个功能 provided. 如果你在POM中提供的范围内有这样的依赖关系,那么继承时就不会在依赖的子POM中看到该依赖关系。

更多信息请参考Maven文档。

http:/maven.apache.orgguidesintroductionintroduction-to-dependency-mechanism.html。

这个作用域只在编译和测试的classpath上可用,而不是转义的。

© www.soinside.com 2019 - 2024. All rights reserved.