PayPal Donation

Enter Amount:

Skype me

My status

Follow me

Facebook Digg LinkedIn MySpace Twitter Playstation network Xbox Live
Digg Del.icio.us Reddit Simpy StumbleUpon Ask Facebook Slashdot Backflip Spurl MisterWong Netvouz Diigo Segnalo RawSugar Shadows Google Furl Newsvine Yahoo Technorati Live Blogmarks Netscape Fark Wink LinkaGoGo Bibsonomy FeedMe Magnolia Blue Tailrank Del.irio.us Y PlugIM SpotBack LinkSwarm
Maven reusing test classes across multi modules projects Print E-mail
User Rating: / 11
PoorBest 
Friday, 18 July 2008 22:14

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.

Tags See All Tags Add New Tag...

Please Enter New Tags Separated By Comma's
  Or Close

apache  framework  howto  maven 

Comments
Add New Search RSS
great idea
Connor (67.176.44.xxx) 2009-02-01 22:37:39

Cedric, this is a great idea and works if you first do "mvn install" on
the tests artifact.

I have a multi-module project, with a parent POM and two
children: Core and Website. Website depends on Core. When I run "mvn
package" from the parent POM level, it compiles Core, and then when
compiling the Website it automatically knows to use the Core snapshot that was
just created (and not the older one in my repository).

But when I run
"mvn integration-test", it compiles Core, generates both the normal Core
artifact and the Core tests artifact. But it doesn't automatically include the
Core-1.0-SNAPSHOT-tests.jar when running the tests. Have you found a way around
this? Maybe this is a Maven bug?
same problme
Bo Gundersen (77.243.62.xxx) 2009-02-12 16:45:17

Im having the same problem as you Conner. The proposed solution works greate if
you do an "mvn install" in the parent, but if you do a "mvn
test" it will break.
Test jar included without the test scope
Vincenzo Vitale (82.210.249.xxx) 2009-04-15 10:28:22

I have a submodule in which I want to include the test code of another module
but not in the tests (than without the specifying the test scope). The intention
is to execute the integration all the integration tests of the project in a web
application.

When I build from the root there is a compilation problem since
the dependency is not correctly resolved. What's strange is that when I build
the submodule everything is fine.

It looks to me as a Maven bug
(2.0.9).

Thanks for the article,
V.
It was a bug [MNG-4032]
Vincenzo Vitale (82.210.249.xxx) 2009-04-15 10:36:12

And actually it was a bug fixed in Maven 2.1.0

Look
here:
http://jira.codehaus.org/browse/MNG-4032
Dzmitry Lazerka (80.94.225.xxx) 2009-05-10 11:49:19

Thanks!
Great!!!!
CmaJ (88.16.30.xxx) 2009-06-25 13:15:47

Thanks!!!!!
Write comment
Name:
Email:
 
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
:):grin;)8):p:roll:eek:upset:zzz:sigh:?:cry
:(:x
Please input the anti-spam code that you can read in the image.
 


Other articles:

Powered By relatedArticle

Want to put your ad banner here, contact us

Top 200 Tags