在Maven项目之间共享测试资源 [英] Share test resources between maven projects
问题描述
有一个明确的解决方案,可以使用maven-jar-plugin
插件的test-jar
目标在maven项目之间共享通用测试代码(请参阅
There is a clear solution for sharing the common test code between maven projects using test-jar
goal of maven-jar-plugin
plugin (see here).
我需要对测试资源做类似的事情,特别是我希望在测试过程中项目A的测试资源在项目B的类路径中可用.
I need to do the similar thing with test resources, in particular, I want test resources of project A be available in the classpath of project B during testing.
对于项目A,需要声明:
For project A one need to declare:
<!-- 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中,它将是正常依赖项:
And in project B it will be normal dependency:
<dependency>
<groupId>myproject.groupId</groupId>
<artifactId>myartifact</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>test-resources</classifier>
<scope>test</scope>
</dependency>
问题:它应该在所有情况下都有效吗?是否可以在没有maven-antrun-plugin
的情况下打包资源(使用更多的轻量级"插件)?
Question: Should it work in all cases? Is it possible to pack resources without maven-antrun-plugin
(using more 'lightweight' plugin)?
推荐答案
只需使用 jar:test-jar
并将声明的JAR声明为依赖项(请参阅
Just use jar:test-jar
and declare the resulting JAR as a dependency (refer to this guide for more details). And while I don't understand the problem of having resources and classes in this jar, you can always exclude all .class
files:
<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>
这篇关于在Maven项目之间共享测试资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!