如何向测试modulepath添加元素?Surefire插件相关技术问询
向Maven Surefire测试模块路径添加元素的方法
方法一:通过测试依赖自动纳入
如果要添加的是标准Maven管理的Java模块,直接将其声明为test scope依赖即可,Surefire会自动将这类依赖加入测试模块路径:
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>target-module</artifactId> <version>1.0.0</version> <scope>test</scope> </dependency> </dependencies>
方法二:手动指定额外模块路径(Surefire 3.0.0+)
对于本地Jar包、非Maven管理的模块这类自定义元素,可以用Surefire的additionalModulepathElements参数手动配置,示例如下:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> <configuration> <additionalModulepathElements> <additionalModulepathElement>${project.basedir}/libs/custom-module.jar</additionalModulepathElement> </additionalModulepathElements> </configuration> </plugin> </plugins> </build>
注意事项
- 必须使用Surefire 3.0.0及以上版本,旧版本不支持该模块路径配置参数。
- 要添加的元素必须是合法的Java模块(包含
module-info.java),否则无法被识别为模块路径的一部分。
内容的提问来源于stack exchange,提问作者Gili
相关产品推荐
相关产品推荐

