有一个明确的解决方案,可以使用maven-jar-plugin插件的test-jar目标在maven项目之间共享公共测试代码(参见here)。
我需要对测试资源做类似的事情,特别是在测试期间,我希望项目A的测试资源在项目B的类路径中可用。
对于项目A,需要声明:
<!-- Package and attach test resources to the list of artifacts: -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<jar destfile="${project.build.directory}/test-resources.jar">
<fileset dir="${project.basedir}/test-resources" />
</jar>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/test-resources.jar</file>
<type>jar</type>
<classifier>test-resources</classifier>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>在项目B中,它将是正常的依赖关系:
<dependency>
<groupId>myproject.groupId</groupId>
<artifactId>myartifact</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>test-resources</classifier>
<scope>test</scope>
</dependency>问:它应该在所有情况下都有效吗?有没有可能在没有maven-antrun-plugin的情况下打包资源(使用更轻量级的插件)?
发布于 2019-08-30 15:48:20
使用maven-dependency-plugin,我们可以将需要的资源放在正确的目录下,只需要修改依赖项目上的pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>generate-test-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>dependeeGroupId</groupId>
<artifactId>dependeeArtifactId</artifactId>
<version>dependeeVersion</version>
<type>test-jar</type>
<outputDirectory>${project.build.directory}/test-classes</outputDirectory>
<includes>resourceNeeded.txt</includes>
<overWrite>true</overWrite>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>type用于获取测试资源
outputDirectory用于将可用的资源放入测试中
这里的文档:https://maven.apache.org/plugins/maven-dependency-plugin/unpack-mojo.html
https://stackoverflow.com/questions/2247199
复制相似问题