Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share test resources between maven projects

Tags:

maven-2

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).

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.

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> 

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> 

Question: Should it work in all cases? Is it possible to pack resources without maven-antrun-plugin (using more 'lightweight' plugin)?

like image 205
dma_k Avatar asked Feb 11 '10 19:02

dma_k


People also ask

Can two Maven modules depend on each other?

Because modules within a multi-module build can depend on each other, it is important that the reactor sorts all the projects in a way that guarantees any project is built before it is required. The following relationships are honoured when sorting projects: a project dependency on another module in the build.

What is test dependency in Maven?

A test -scoped dependency is a dependency that is available on the classpath only during test compilation and test execution. If your project has war or ear packaging, a test -scoped dependency would not be included in the project's output archive.


2 Answers

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> 

And to use it:

<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> 
like image 185
Pascal Thivent Avatar answered Sep 21 '22 15:09

Pascal Thivent


Accepted answer helped me, but it's not quite accurate in case you need regular jar of same project as well. It will delete *.class files from both jars.
Settings below translates to something like:

  • create me 2 jars, 1 regular, 1 test;
  • remove *.class files, but only from test jar

    <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> 
like image 26
miracle_the_V Avatar answered Sep 20 '22 15:09

miracle_the_V