Wednesday 12 January 2022

Create maven multi module project in Eclipse

 What is multi module project?

A multi module project is used to manage a group of submodules. A multi-module project is built from an aggregator POM which is located in the project’s root directory and has a package type as pom. Child modules inherit from the parent pom.

In a multi-module project, using the single maven command you can build all the artifacts and maintain all the artifact versions in sync. Configuring a CI/CD pipeline is easy, one Jenkin job will do the job.

Let’s see it with an example. I am going to create a root project ‘platform’ and create two modules under this project.

a.   connectors

b.   transformers

Follow the below step-by-step procedure to build multi-module project in Eclipse.

Open Eclipse.

File -> New -> Other



Select ‘Maven Project’.


Click on Next button.

Select the check box ‘Create a simple project (skip archetype selection)’.



Click on Next button.


Set following fields.

a.   Group Id as com.sample.app

b.   Artifact Id as platform

c.    Version to 1.

d.   Packaging type should be pom

 


Click on Finish button.

 

Project hierarchy looks like below.



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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sample.app</groupId>
  <artifactId>platform</artifactId>
  <version>1</version>
  <packaging>pom</packaging>
</project>

When you open pom.xml, you can observe packaging type is pom. This pom is called the parent pom. That means it acts as a parent pom to all the projects underneath this project.

 

Let’s create two modules under this project.

a.   connectors

b.   transformers

 

Create connectors module

File -> New -> Other



Select ‘Maven Module’ and click on the Next button.



Select the check box ‘Create a simple project (skip archetype selection).

Set module name as connectors and set the parent project to platform.

 

Click on Next button.



Use default options (packaging is set to jar) and click on Finish button.



connectors project pom.xml is given below.

 

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.sample.app</groupId>
		<artifactId>platform</artifactId>
		<version>1</version>
	</parent>
	<artifactId>connectors</artifactId>
</project>

As you observe pom.xml, parent portion is added with platform project details.

 

‘platform’ project pom.xml contains new module information.

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>platform</artifactId>
	<version>1</version>
	<packaging>pom</packaging>
	<modules>
		<module>connectors</module>
	</modules>
</project>

Create transformers module

Follow the same procedure as above. Post transformers module creation, you can observe new module ‘transformers’ is added to platform 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>platform</artifactId>
	<version>1</version>
	<packaging>pom</packaging>
	<modules>
		<module>connectors</module>
		<module>transformers</module>
	</modules>
</project>

Total project structure is given below.



Open a terminal and navigate to the platform pom.xml file and execute the command ‘mvn clean install’. Once the command is executed successfully, you can observe two jar files (connectors-1.jar, transformers-1.jar) get created.


Previous                                                    Next                                                    Home

No comments:

Post a Comment