Demo Joomla! 1.5

Visit the Joomla! 1.5 demo site to see my extensions live running

Demo Joomla! 2.5

Visit the Joomla! 2.5 demo site to see my extensions live running

Support

Do not submit a bug report if you need technical support or have questions.

Forums

Post your suggestions ask for help in the community forums

Wiki

Visit the Wiki extensive and up to date documentation at your fingertips.

Contact Me

Missing images/links, any comments, suggestions, need help? Contact me

Skype

Need desperately help?
Skype Me™! But dont abuse of it!

Maven reusing test classes across multi modules projects

User Rating:  / 0
PoorBest 

maven-logo-2
Maven clearly differentiate "productive code"  in each module src/main/* from unit test code in src/test/*.
In a typical multi-module projects like the one below:

componentA
|
|-moduleA
|         /src/main/java
|         /src/main/resources
|         /src/test/java
|         /src/test/resources
|-moduleB
|         /src/main/java
|         /src/main/resources
|         /src/test/java
|         /src/test/resources

Note1: Eclipse do not support multi module project as only one level of code sharing is allowed. So we have
3 eclipse projects/maven projects with a pom.xml:

  • componentA with a packaging pom, and 2 module aggregated
  • moduleA, parent is ../componentA/pom.xml
  • moduleB   parent is ../componentA/pom.xml

Note2: eclipse has only one Class Loader, meaning that code in /src/main/java  and /src/test/java  is also exported
as dependencies between modules, while in Maven, code is not shared!

So as default by adding a dependencies in moduleB/pom.xml to moduleA, you'll only inherit moduleA src/main/java
and src/main/resources

<dependency>
<groupId>com.waltercedric.maven</groupId>
<groupId>modulesA</groupId>
</dependency>

But what if you have some test API classes in modulesA? trying to add also moduleA in scope test wont help you any further:

<dependency>
<groupId>com.waltercedric.maven</groupId>
<artifactId>modulesA</artifactId>
</dependency>

<dependency> <!-- do not work! -->
<groupId>com.waltercedric.maven</groupId>
<artifactId>modulesA</artifactId>
<scope>test</scope>
</dependency>

This is exactly where artifact classifier may help you, but lets first look at the artifact naming convention.

artifact name = {name/artifactId}-{version}-{classifier}.{extension}

Maven is also introducing some conventions:

-> name is most of the time the artifactId
-> version being the version number of the artifact, simply don't use something containing SNAPSHOTS 
    in it as it is considered being non stable by some plugin (maven-release-plugin for example) 
-> classifier is either

  • empty like for ex: jaxb-1.2.jar and then it will contains the binary package of the library jaxb
  • source like for ex: jaxb-1.2-source.jar
  • javadoc like for ex: jaxb-1.2-javadoc.jar
  • but it can be anything! as it just classify an artifact, but don't use it in place where an artifact could
    have been use, a classifier proxy/stub is not recommended: make a module of it instead. You must see
    classifier more as a way to categorize artifact in a module.

You'll find a lot of artifact across Internet not following these guidelines, this is not an issue as Maven repositories are
able to search for pom.properties or project.xml or pom.xml in jar if they exist and use the right <groupId> and
<artifactId>

Back to code reuse of test classes across modules, the trick is to tell maven to make a jar of every module test code so
you can depend on it in others modules.

a normal build of componentA, would create:

# /componentA/mvn clean install

/.m2/repository/com/waltercedric/maven/moduleA/0.0.1.SNAPSHOTS/moduleA-0.0.1.SNAPSHOTS.jar
/.m2/repository/com/waltercedric/maven/moduleB/0.0.1.SNAPSHOTS/moduleB-0.0.1.SNAPSHOTS.jar

where by just adding to componentA/pom.xml the following inside the <build></build>:

<!--  this create jar file of code from src/test/java so modules with tests can share code -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
<executions>
  <execution>
    <goals>
       <goal>test-jar</goal>
    </goals>
  </execution>
</executions>
</plugin>

# /componentA/mvn clean install

/.m2/repository/com/waltercedric/maven/moduleA/0.0.1.SNAPSHOTS/moduleA-0.0.1.SNAPSHOTS.jar
/.m2/repository/com/waltercedric/maven/moduleA/0.0.1.SNAPSHOTS/moduleA-0.0.1.SNAPSHOTS-tests.jar
/.m2/repository/com/waltercedric/maven/moduleB/0.0.1.SNAPSHOTS/moduleB-0.0.1.SNAPSHOTS.jar
/.m2/repository/com/waltercedric/maven/moduleB/0.0.1.SNAPSHOTS/moduleB-0.0.1.SNAPSHOTS-tests.jar

Maven now create new artifacts  moduleA-0.0.1.SNAPSHOTS-tests.jar!

So you can now add a dependencies in moduleB/pom.xml to moduleA in scope test with a tests classifier

<dependency> <!-- reuse src/main/java code from moduleA, normal dependencies --> 
<groupId>com.waltercedric.maven</groupId> <artifactId>modulesA</artifactId> </dependency> <dependency> <!-- reuse src/test/java code from moduleA! --> <groupId>com.waltercedric.maven</groupId> <artifactId>modulesA</artifactId> <scope>test</scope> <classifier>tests</classifier> </dependency>

You can now share test code across module.

You might also like:
Apache Maven 3 Cookbook
83 days ago
Apache Maven 3 Cookbook
  First a big thanks to Packt Publishing for having sent me this book to review! I did enj
Apache Maven 3 Cookbook Review
129 days ago
Apache Maven 3 Cookbook Review
Thanks to Packt Publishing for having sent me this book to review. I will publish a review in the
List conflicting dependencies in the Maven reactor
245 days ago
List conflicting dependencies in the Maven reactor
The Maven Dependency Plugin among other things include a dependency:analyze-duplicate The depe
Break Maven build when there is a dependency conflict
245 days ago
Break Maven build when there is a dependency conflict
Scenarios You want to control Maven during dependency resolution and break the build i
blog comments powered by Disqus
Parent Category: Framework
Category: Apache Maven

Donations

Thank You for supporting my work

Follow Me

Follow cedricwalter on Twitter Subscribe via RSS Subscribe via RSS Follow us on Facebook Follow us on Google+

Latest Articles

  • Thanks to Nathan Rennie-Waldock. you can have the latest PHP5 5.3.10 running in Ubuntu Oneiric, Natty, Maverick and Lu... ...
  • Download and install the latest  VMware Player 4.0.2 to run this Virtual Appliance “Ubuntu 11.10 x64 Server” ... ...
  • Download and install VMware Player 4.0.2 to run this Virtual Appliance “Ubuntu 11.10 x64 Server” Ready to user st... ...
  • Thanks to Ondřej Surý,  maintainer for some Debian packages, you can have the latest PHP5 maintained by Debian ... ...
  • Munin is a networked resource monitoring tool that can help analyze resource trends and "what just happened to ki... ...

Latest Comments

Popular Posts

rockettheme advertisement

dropbox logo

Help Us & Leave Feedback!

  • Do you have an excellent article idea you would like to read about here? Share it!
  • Do you have some interesting tips how we could improve our site?
  • Something missing here? Help us make this blog a better place, leave feedback!
We would love to hear from you! Be active! Write us now!

Blogs

Didier Beck Tech Head Brothers

google+ badge