有一个明确的解决方案,可以使用test-jar
插件的maven-jar-plugin
目标在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
的情况下打包资源(使用更多'轻量级'插件)?
答案 0 :(得分:36)
只需使用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>
答案 1 :(得分:1)
公认的答案对我有所帮助,但是如果您还需要同一个项目的常规jar,它就不太准确。它将从两个jar中删除* .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>
答案 2 :(得分:0)
已经有一个目标是从maven构建test jar。
假设您需要更灵活的东西,可以使用jar插件打包测试资源,并使用类似这样的主程序包目标来运行该目标。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>test-resources</classifier>
<includes>
<include>**/*.whatever-you-want</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
当运行jar目标时,捆绑的任何内容都将添加到project-name-version-test-resources.jar中。
然后,您可以通过上面使用的相同依赖项将其包含在项目中。
答案 3 :(得分:0)
使用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