Saturday, May 17, 2014

A Simple Maven Project with Sub-Modules and Interdependencies


This project has a parent module along with two child modules, one child that depending on the other child.  And I wanted the example to be as simple as possible, not even a “Hello World” web application here — just one module that references a class from it’s sibling module.

In order for “dao-mp” to access “utilities-mp”, a dependency on “utilities-mp” must be included.

Sample Project Overview

my.project Maven project (parent pom.xml)




utilities Maven sub-module (child 1)




dao Maven sub-module (child 2; depends on child 1)




unit test to verify
  1. dao-mp sub-module can access utilities-mp sub-module resources
  2. project JAR files are not installed into the local repository




Util class (part of utilities-mp)





Dao class (part of dao-mp)





The results of running any of these commands should result in showing what's in the reactor and the status of running  the tests.
mvn clean package
mvn clean verify
mvn clean test


 ...




Check to see if any jar files were created in the project.

find . -name "*.jar"
./dao-mp/target/dao-mp-0.0.1-SNAPSHOT.jar
./utilities-mp/target/utilities-mp-0.0.1-SNAPSHOT.jar


mvn compile

find . -name "*.jar"
$


Without including the "compile" phase explicitly, "dependency:tree" will automatically install jars to the local repository.  To get the best of both worlds,...

mvn compile dependency:tree 


Another quick check to verify no jar files were created

find . -name "*.jar"
$


Mission accomplished!  The project can be downloaded to save you some time.

Conclusion

One thing we have learned is that Maven can and should be used during development to build without the need of installing local project jars to the local Maven repository.  This is an important first step in getting off on the right foot with Maven.