maven-logo-2
Maven clearly differentiate "productive code"&160; 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&160;&160; parent is ../componentA/pom.xml

Note2: eclipse has only one Class Loader, meaning that code in /src/main/java&160; and /src/test/java&160; 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&160;
&160;&160;&160; in it as it is considered being non stable by some plugin (maven-release-plugin for example)&160;
-> 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&160; 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.

comments powered by Disqus

You might like also

Fetching artifact programmatically through REST/API in Nexus 3.x
There is so many case where it is desirable to pull down artifact from Sonatype Nexus using REST API, unfortunately Nexus 3.x Rest API are still under development... Some use cases in Nexus 2.x: You have a script that uses REST call to pull down the LATEST maven artifacts every night from Nexus and deploys them. You make extensive use of the REST API in all your puppet modules You use the Atlassian Puppet module for Nexus for creating repository, …
722 Days ago
git-branch-renamer-maven-plugin
When working with many feature/release/bugix/hotfix branches, it is a bad idea to start changing the pom version as this will create merge conflicts using pull request. this plugin allow you to keep in ALL branches the same pom version for all your projects, for example MASTER-SNAPSHOT the version will be derived from branch name automagically :-) You may want to read more first these 2 short articles Update Maven pom version on GIT checkout in TeamCity maven-release-plugin with GIT git-branch-renamer-maven-plugin …
734 Days ago
Review: Getting Started with Apache Maven by Russell Gold
Some time ago I was asked if I would like to write a review about one of the new video courses from Packt Publishing. It was "Getting Started with Apache Maven" http://bit.ly/1fycmpP by Russell Gold and since I have been using Maven for some years now (since 2007) and did publish some articles myself, I thought it would be nice to help them promote Apache Maven. The course is organized in eight chapters, forty videos with a length between two …
1921 Days ago
Update Maven pom version on GIT checkout in TeamCity
Here is a solution to the following problems Deriving Maven artifact version from GIT branch, Update pom version on GIT checkout automatically, Add the ability to use Pull request with Apache Maven. You have a workflow requirement that require you to have the artifact version of a module externally defined from the current branch in GIT. For example You want to start working on a new feature branch “feature-memory-improvement”, so you branch from master a new branch named feature/feature-memory-improvement Having …
1926 Days ago
Easily Compress Web Application Resources with EhCache
Resources such as JavaScript and CSS files can be compressed before being sent to the browser, improving network efficiencies and application load time in certain case. If you are not using Apache with mod_deflate or nginx in front of your web application, you may need to implement resources compression yourself…. Wait! don’t start writing your own filter to compress files like CSS, html, txt, javascript it is way more difficult than you think to properly handle http response headers and …
2408 Days ago
Tomcat 7 and Apache Maven
Here is 3 different way to control the lifetime a local Tomcat 7 container using Apache Maven. A typical scenario would be to start a servlet container prior to running integration tests (Selenium, SAHI or using any other framework you can think of ) With the following examples, you will be able to start an instance of Tomcat 7 running your web application in the pre-integration-test phase and stop the instance in the post-integration-test phase. You can also decide to …
2408 Days ago
Apache Maven copy local file to a remote server server using SSH
I will show you in an Apache Maven configuration file how to copy files to server each time the package phase is executed. Solution with Ant SCP task This snippet of code is a ready to use code that make use of Apache Ant task scp, Just put this snippet of code in your Maven module where the assembly is executed or anywhere else to push all tar.gz files to a server just run a maven mvn package, you can …
2596 Days ago
Apache M2Eclipse: Get rid of Duplicate resources when opening resources and types
In this small post, I’ll show you how to remove duplicated resources in the Open Resource view of Eclipse Eclipse – M2Eclipse – Subversive …
2602 Days ago
Apache Maven 3 Cookbook
&160; First a big thanks to Packt Publishing for having sent me this book to review! I did enjoy going through this book, while I did not learn a lot of new stuff (I am using Apache Maven daily since 2006!), I found it to be concise and would recommend it anytime to any of my colleagues. But let’s go through my review of this cookbook of over 50 recipes towards optimal Java Software Engineering with Maven 3: Apache Maven
2744 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 next coming days Grasp the fundamentals and extend Apache Maven 3 to meet your needs Implement engineering practices in your application development process with Apache Maven Collaboration techniques for Agile teams with Apache Maven Use Apache Maven with Java, Enterprise Frameworks, and various other cutting-edge technologies Develop for Google Web Toolkit, Google App Engine, and Android Platforms using Apache Maven You …
2790 Days ago