有一个明确的解决方案,可以使用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的情况下打包资源(使用更轻量级的插件)?
发布于 2010-02-12 06:18:38
只需使用jar:test-jar并将结果JAR声明为依赖项(有关更多详细信息,请参阅this guide )。虽然我不理解在这个jar中包含资源和类的问题,但您始终可以排除所有.class文件:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>**/*.class</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>并使用它:
<project>
...
<dependencies>
<dependency>
<groupId>com.myco.app</groupId>
<artifactId>foo</artifactId>
<version>1.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>发布于 2018-11-30 22:57:48
被接受的答案帮助了我,但如果你也需要相同项目的常规jar,它就不太准确了。它将从两个jars中删除*.class文件。
下面的设置转换为类似以下内容:
中删除
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*.class</exclude>
</excludes>
</configuration>
</execution>
</executions>
</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
复制相似问题