Create a multimodule Maven project

It is a common practice or I would say it is a best practice to create multi modules application in enterprise level. The modularised architecture provides a better design with less coupling. Also, your application would be easy to maintain as the project grows. There are different ways to split up an application, in this case, our application is split base on package boundaries.

Let's create our project in the following structure using maven archetypes,

parent
     |__ common
     |__ model
     |__ persistence
     |__ service
     |__ api
     |__ web

A multimodule maven project is defined by a parent POM referencing to multiple sub-modules.

My first task is creating the parent POM. Let's name this project mydailyjavawork.

STEP 01: Create parent module


Open command prompt from the desired file location, then type

mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=RELEASE


Now we have to enter values for groupId, artifactId, version and package. Just hit enter if you want to keep default values.

Define value for groupId: : com.mydailyjavawork
Define value for artifactId: : mydailyjavawork
Define value for version:  1.0-SNAPSHOT: :
Define value for package:  com.mydailyjavawork: : pom
Confirm properties configuration:
groupId: com.mydailyjavawork
artifactId: mydailyjavawork
version: 1.0-SNAPSHOT
package: pom
 Y: :


We have created the parent POM, next step is creating the sub-modules.

STEP 02: Create sub-modules (except web module)


Let's create module common as the first sub-module, To do this, navigate to the created folder mydailyjavawork then type 

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE



Now we have to enter values for groupId, artifactId, version and package. Just hit enter if you want to keep default values.

Repeat this for other submodules except for the web module since web module has to be created using maven-archetype-webapp archetype.

STEP 03. Create web module


The only difference here is the archetype. Type following maven command to create the web module. 

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=RELEASE -DinteractiveMode=true



All done!

Let's take a look at the folder hierarchy, 


Also, let's take a look at the parent POM.

  
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mydailyjavawork</groupId>
  <artifactId>mydailyjavawork</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>mydailyjavawork</name>
  <modules>
    <module>api-mydailyjavawork</module>
    <module>model-mydailyjavawork</module>
    <module>persistence-mydailyjavawork</module>
    <module>service-mydailyjavawork</module>
    <module>common-mydailyjavawork</module>
    <module>web-mydailyjavawork</module>
  </modules>
</project>












Comments

Popular Posts