Joomla Extensions Demo

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

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

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 copy local file to a remote server server using
39 days ago
Apache Maven copy local file to a remote server server using
I will show you in an Apache Maven configuration file how to copy files to server each time the pa
Apache M2Eclipse: Get rid of Duplicate resources when openin
45 days ago
Apache M2Eclipse: Get rid of Duplicate resources when openin
In this small post, I’ll show you how to remove duplicated resources in the Open Resource view o
Apache Maven 3 Cookbook
187 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
233 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
349 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
349 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
OSGi-Bundle with the Maven-Bundle-Plugin
350 days ago
OSGi-Bundle with the Maven-Bundle-Plugin
OSGi (Open Service Gateway Initiative) is a Java framework for developing and deploying modu
Apache Maven Cargo deploy with Tomcat 7
360 days ago
Apache Maven Cargo deploy with Tomcat 7
Following the post about Deploy to Tomcat 6 using Maven, here is a ready to use example with the
blog comments powered by Disqus
Category: Apache Maven

Donations

Thank You for supporting my work
Subscribe to me on YouTube

Latest Articles

  • In this series of post I will outline some common techniques to help Joomla extensions development. As you know Jooml... ...
  • CedTag  has been updated to version 2.5.3 and correct a lot of bugs and contains some nice features. CedTag is t... ...
  • CedThumbnails has been updated to version 2.5.5 and contains 1 new features for both Joomla 1.7 and Joomla 2.5. For ex... ...
  • CedSmugmug  has been updated to version 2.5.2 and correct some bugs and contains some nice features. CedSmugmug&... ...
  • If you want an extra gigabyte of storage on your Dropbox account, the online cloud service invites you to compete in i... ...

Subscribe

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!