go to  ForumEasy.com   
JavaPro  
 
 
   Home  |  MyForum  |  FAQ  |  Archive    You are not logged in. [Login] or [Register]  
Forum Home » Ubuntu » Maven Multi Module Project Example
Email To Friend  |   Set Alert To This Topic Rewarding Points Availabe: 0 (What's this) New Topic  |   Post Reply
Author Topic: Maven Multi Module Project Example
Linux
member
offline   
 
posts: 120
joined: 01/24/2011
from: San Jose, CA
  posted on: 10/31/2017 05:18:44 AM    Edit  |   Quote  |   Report 
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

 Profile | Reply Points Earned: 0
Linux
member
offline   
 
posts: 120
joined: 01/24/2011
from: San Jose, CA
  posted on: 10/31/2017 05:20:14 AM    Edit  |   Quote  |   Report 
Create Directories

Container:
$ mkdir hello-one


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

 Profile | Reply Points Earned: 0
Linux
member
offline   
 
posts: 120
joined: 01/24/2011
from: San Jose, CA
  posted on: 10/31/2017 05:21:36 AM    Edit  |   Quote  |   Report 
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>

 Profile | Reply Points Earned: 0
Linux
member
offline   
 
posts: 120
joined: 01/24/2011
from: San Jose, CA
  posted on: 10/31/2017 05:22:54 AM    Edit  |   Quote  |   Report 
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>

 Profile | Reply Points Earned: 0
Linux
member
offline   
 
posts: 120
joined: 01/24/2011
from: San Jose, CA
  posted on: 10/31/2017 05:25:18 AM    Edit  |   Quote  |   Report 
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>

 Profile | Reply Points Earned: 0
Linux
member
offline   
 
posts: 120
joined: 01/24/2011
from: San Jose, CA
  posted on: 10/31/2017 05:34:31 AM    Edit  |   Quote  |   Report 
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] ------------------------------------------------------------------------

 Profile | Reply Points Earned: 0
Linux
member
offline   
 
posts: 120
joined: 01/24/2011
from: San Jose, CA
  posted on: 10/31/2017 05:46:33 AM    Edit  |   Quote  |   Report 
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] ------------------------------------------------------------------------

 Profile | Reply Points Earned: 0

 
Powered by ForumEasy © 2003-2005, All Rights Reserved. | Privacy Policy | Terms of Use
 
Get your own forum today. It's easy and free.