您可以在配置 maven-jar-plugin 并运行mvn install的隔离工件中发布您的测试:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
然后从要执行测试的其他项目中导入依赖项:
<dependency>
<groupId>project.with.tests.groupid</groupId>
<artifactId>projectwithtestsartifatid</artifactId>
<version>1.0</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
无论如何,surefire 或 failsafe 插件只会在target/test-classes文件夹中寻找测试,而不是在 classpath 的 jar 中。在surefire中存在一个未分配的固定版本(至少目前是这样)。
一种解决方法是使用依赖插件来解压target/test-classes中的测试类,但这可能会污染版本:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>resource-dependencies</id>
<phase>process-test-classes</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeTypes>test-jar</includeTypes>
<outputDirectory>${project.build.directory}/test-classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>