Author |
Topic: Maven Multi Module Project Example |
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Maven Multi Module Project Example |
Let's say that your company has a project which consists of two module's directories app and util. Each module has its own dedicated team working on it.
[+] ... hello-one
[+] ... app
{+] ... util
[-] ... pom.xml
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Create Directories |
Container:
Sub-directories for modules
$ cd hello-one
$ mkdir app
$ mkdir util
Source and test dir for util module
$ mkdir -p util/src/main/java/com/xyz/commons
$ mkdir -p util/src/test/java/com/xyz/commons
Source dir for app module
$ mkdir -p app/src/main/java/com/xyz/integration
$ mkdir -p app/src/test/java/com/xyz/integration
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
The top-level container |
The top-level project is just a container consisting of modules and a pom.xml file
$ nano /opt/maven/hello-one/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- project coordinates -->
<groupId>com.xyz</groupId>
<artifactId>hello-one-pom</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>app</module>
<module>util</module>
</modules>
</project>
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
The sub-level project -- util module |
The source code:
$ nano /opt/maven/hello-one/util/src/main/java/com/xyz/commons/DateUtil.java
package com.xyz.commons;
import java.util.Date;
import org.apache.commons.lang.time.DateFormatUtils; // the third party class
public class DateUtil {
public static String getToday() {
String today = DateFormatUtils.format(new Date(), "dd-MMM-yyyy");
return today;
}
}
The test code:
$ nano /opt/maven/hello-one/util/src/test/java/com/xyz/commons/DateUtilTest.java
package com.xyz.commons;
import static org.junit.Assert.assertEquals;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Test;
public class DateUtilTest {
@Test
public void testGetToday() {
String actual = DateUtil.getToday();
String expected = new SimpleDateFormat("dd-MMM-yyyy")
.format(new Date());
assertEquals(expected, actual);
}
}
pom.xml
$ nano /opt/maven/hello-one/util/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- parent coordinates -->
<parent>
<groupId>com.xyz</groupId>
<artifactId>hello-one-pom</artifactId>
<version>1.0</version>
</parent>
<!-- project coordinates -->
<groupId>com.xyz</groupId>
<artifactId>util-jar</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<!-- project dependencies -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
The sub-level project -- app module |
The source code:
$ nano /opt/maven/hello-one/app/src/main/java/com/xyz/integration/Hello.java
package com.xyz.integration;
import com.xyz.commons.*;
public class Hello {
String name = "World"; // default
public Hello(){
}
public Hello(String name){
this.name = name;
}
public String getName() {
return name;
}
public String getGreetings() {
return "Hello " + getName() + "!";
}
public static void main(String[] args) {
Hello hello = new Hello("World");
String msg = hello.getGreetings() + " Today is " + DateUtil.getToday();
System.out.println(msg);
}
}
The test code:
$ nano /opt/maven/hello-one/app/src/test/java/com/xyz/integration/HelloTest.java
package com.xyz.integration;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class HelloTest {
@Test
public void testGetName() {
String actual = new Hello("World").getName();
String expected = "World";
assertEquals(expected, actual);
}
@Test
public void testGetGreetings() {
String actual = new Hello().getGreetings();
String expected = new Hello("World").getGreetings();
assertEquals(expected, actual);
}
}
pom.xml
$ nano /opt/maven/hello-one/app/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- parent coordinates -->
<parent>
<groupId>com.xyz</groupId>
<artifactId>hello-one-pom</artifactId>
<version>1.0</version>
</parent>
<!-- project coordinates -->
<groupId>com.xyz</groupId>
<artifactId>app-jar</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<!-- project dependencies -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.xyz</groupId>
<artifactId>util-jar</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Build from the top level container |
$ cd /opt/maven/hello-one
$ mvn package
The output:
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] hello-one-pom
[INFO] util-jar
[INFO] app-jar
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-one-pom 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building util-jar 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ util-jar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /opt/maven/hello-one/util/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ util-jar ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ util-jar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /opt/maven/hello-one/util/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ util-jar ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ util-jar ---
[INFO] Surefire report directory: /opt/maven/hello-one/util/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.xyz.commons.DateUtilTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.231 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ util-jar ---
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building app-jar 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ app-jar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /opt/maven/hello-one/app/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ app-jar ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ app-jar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /opt/maven/hello-one/app/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ app-jar ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ app-jar ---
[INFO] Surefire report directory: /opt/maven/hello-one/app/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.xyz.integration.HelloTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.119 sec
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ app-jar ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] hello-one-pom ...................................... SUCCESS [ 0.014 s]
[INFO] util-jar ........................................... SUCCESS [ 3.746 s]
[INFO] app-jar ............................................ SUCCESS [ 0.631 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.647 s
[INFO] Finished at: 2017-10-30T22:28:33-07:00
[INFO] Final Memory: 10M/60M
[INFO] ------------------------------------------------------------------------
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Assembly -- to wrap the entire project for deploying |
To create the archive (.tar.gz & .zip) of the entire project including all the modules, run assembly:assembly goal from the project’s top level directory.
$ cd /opt/maven/hello-one
$ mvn assembly:assembly -DdescriptorId=project
The output:
...
...
...
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ app-jar ---
[INFO] Surefire report directory: /opt/maven/hello-one/app/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.xyz.integration.HelloTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.112 sec
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ app-jar ---
[INFO]
[INFO] <<< maven-assembly-plugin:2.2-beta-5:assembly (default-cli) < package @ hello-one-pom <<<
[INFO]
[INFO]
[INFO] --- maven-assembly-plugin:2.2-beta-5:assembly (default-cli) @ hello-one-pom ---
[INFO] Building tar : /opt/maven/hello-one/target/hello-one-pom-1.0-project.tar.gz
[INFO] Building tar : /opt/maven/hello-one/target/hello-one-pom-1.0-project.tar.bz2
[INFO] Building zip: /opt/maven/hello-one/target/hello-one-pom-1.0-project.zip
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] hello-one-pom ...................................... SUCCESS [ 6.223 s]
[INFO] util-jar ........................................... SKIPPED
[INFO] app-jar ............................................ SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.943 s
[INFO] Finished at: 2017-10-30T22:43:42-07:00
[INFO] Final Memory: 24M/60M
[INFO] ------------------------------------------------------------------------
|
|
|
|
|
|
|
|