In this post, we will see how to create a maven Multi-module project
. Multi-module project structuring can be handy when we have to share a particular module between different projects. For instance, you can have different web-applications which are using same module for interacting with database.
We will develop a parent-project and three sub-modules projects (one model library and two web applications), where both web-applications will share the same model library.
mvn archetype:generate -DgroupId=com.websystique.multimodule -DartifactId=parent-project
This command will create a new project with name ‘parent-project’ in your workspace.
Open pom.xml of above created parent-project and change the packaging
to ‘pom’.
<packaging>pom</packaging>
/parent-project/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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.websystique.multimodule</groupId> <artifactId>parent-project</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>parent-project</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
Via command line, navigate to the directory of parent-project, and fire following commands
cd parent-project mvn archetype:generate -DgroupId=com.websystique.multimodule -DartifactId=model-lib mvn archetype:generate -DgroupId=com.websystique.multimodule -DartifactId=webapp1 mvn archetype:generate -DgroupId=com.websystique.multimodule -DartifactId=webapp2
Now if you open the parent-project pom.xml, you will find all three modules being added in there.
<modules> <module>model-lib</module> <module>webapp1</module> <module>webapp2</module> </modules>
Also, in each sub-module’s pom.xml, a parent section being added.
<parent> <groupId>com.websystique.multimodule</groupId> <artifactId>parent-project</artifactId> <version>1.0-SNAPSHOT</version> </parent>
In our case, model-lib is a libreary(jar), we will set its’s packaging
to jar.
<packaging>jar</packaging>
On the other hand, webapp1 & webapp2 are intended to be WAR, so we need to changed their pom.xml to set the packaging
to war.
<packaging>war</packaging>
Below are the final pom.xml of each sub-module and parent-project
/parent-project/model-lib/pom.xml
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.websystique.multimodule</groupId> <artifactId>parent-project</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.websystique.multimodule</groupId> <artifactId>model-lib</artifactId> <version>1.0-SNAPSHOT</version> <name>model-lib</name> <packaging>jar</packaging> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
/parent-project/webapp1/pom.xml
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.websystique.multimodule</groupId> <artifactId>parent-project</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.websystique.multimodule</groupId> <artifactId>webapp1</artifactId> <version>1.0-SNAPSHOT</version> <name>webapp1</name> <packaging>war</packaging> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
/parent-project/webapp2/pom.xml
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.websystique.multimodule</groupId> <artifactId>parent-project</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.websystique.multimodule</groupId> <artifactId>webapp2</artifactId> <version>1.0-SNAPSHOT</version> <name>webapp2</name> <packaging>war</packaging> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
And the parent-project pom.xml is
/parent-project/pom.xml
<?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.websystique.multimodule</groupId> <artifactId>parent-project</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>parent-project</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <modules> <module>model-lib</module> <module>webapp1</module> <module>webapp2</module> </modules> </project>
In Eclipse , File->Import->Maven->Existing maven projects
click next, select your workspace as root directory , you should see something similar :
click finish, you should see the projects got imported successfully in Eclipse.
Now when you build the parent-project, all your child project will be build successively.
One remark: Although it’s not mandatory, when you change any of your project, it’s advisable to perform a Maven-> Update project do get the changes reflected in all the projects which depends on this project.
That’s it.
If you like tutorials on this site, why not take a step further and connect me on Facebook , Google Plus & Twitter as well? I would love to hear your thoughts on these articles, it will help improve further our learning process.
In this post we will be developing a full-blown CRUD application using Spring Boot, AngularJS, Spring Data, JPA/Hibernate and MySQL,…
Spring Boot complements Spring REST support by providing default dependencies/converters out of the box. Writing RESTful services in Spring Boot…
Being able to start the application as standalone jar is great, but sometimes it might not be possible to run…
Spring framework has taken the software development industry by storm. Dependency Injection, rock solid MVC framework, Transaction management, messaging support,…
Let's secure our Spring REST API using OAuth2 this time, a simple guide showing what is required to secure a…
This post shows how an AngularJS application can consume a REST API which is secured with Basic authentication using Spring…
View Comments
mvn archetype:generate -DgroupId=com.websystique.multimodule -DartifactId=parent-project
create goal is deprecated please update your post.
very good step by step example
Its a good example , How can I configure spring boot with this example, I tried but didnt work.
I tried with this example http://websystique.com/spring-boot/spring-boot-angularjs-spring-data-jpa-crud-app-example/
Please inform if you have any article related to maven multi module and spring boot
You have given maven command as mvn archetype:create but It did not work until I used mvn archetype:generate.
Thanks Farhan, i've updated the post with correct type.Thanks again.
Hi, actually you didn't update the maven command that creates the parent project (mvn archetype:create -DgroupId=com.websystique.multimodule -DartifactId=parent-project) so you need to replace create with generate in this one too :-)
Yes I had the same problem. I thank you not for updating the post at step 1.
how to configure springMVC, hibernate and JAx-Rs based maven multimodile project. That means how to configure appIntializer, appconfig in such a way that it will execute all sub project.
test