Friday, 4 March 2011

Reuse your test code in maven projects!

Here is a quick tip if you are using maven.
If you create some test utility or simply want to reuse some test class in a different project you don't need to duplicate the code or put the code in the main section, just do this:

In the project you have code to share add this section to you pom.xml
<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>
  </plugin>
 </plugins>
</build>

and in the using project you want to use the test utilities add this

<project>
...
 <dependencies>
  <dependency>
   <groupId>com.myco.app</groupId>
   <artifactId>foo</artifactId>
   <version>1.0-SNAPSHOT</version>
   <classifier>tests</classifier>
   <scope>test</scope>
  </dependency>
 </dependencies>
...
</project>

1 comment:

webamoeba said...

Great tip! Thanks :)