在maven项目之间共享testing资源

有一个明确的解决scheme,使用maven-jar-plugin插件的test-jar目标(参见这里 )共享maven项目之间的通用testing代码。

我需要用testing资源来做类似的事情,特别是我希望testing期间项目B的类path中可以使用项目A的testing资源。

对于项目,需要声明:

 <!-- 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 (使用更多的“轻量级”插件)?

只需使用jar:test-jar并将生成的JAR声明为依赖项(有关更多详细信息,请参阅本指南 )。 虽然我不明白在这个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> 

目前已经有一个从maven构buildtestingjar的目标。

假设你需要一些更灵活的东西,你可以使用jar插件打包你的testing资源,并用主包目标来执行这个目标。

  <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中。

然后,您可以通过上面使用的相同依赖项将其包含在项目中。