dependencyManagement is used to manage all the dependencies in a common or parent pom. In this approach, you are managing the version, scope specific information of dependencies in parent pom, all the child modules can refer the dependencies specified in parent pom just with groupId and artifactId (version, scope is inherited from parent pom). This approach is very helpful in multi module project.
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 connectors' and create a child module 'jdbc-connector' under this project.
Step 1: Open Eclipse.
File -> New -> Maven
Project
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 connectors
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>connectors</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 define the dependencies required by all the child modules beneath connectors module.
Updated pom.xml file
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>connectors</artifactId>
<version>1</version>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.report.outputEncoding>UTF-8</project.report.outputEncoding>
<slf4j.version>2.15.0</slf4j.version>
<lombok.version>1.18.22</lombok.version>
<tomcat-jdbc>10.0.16</tomcat-jdbc>
<mysql.connector.version>8.0.27</mysql.connector.version>
<jackson.version>2.13.1</jackson.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>${tomcat-jdbc}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Create child project jdbc-connector
File -> New -> Other
Search for the string ‘maven’ and select Maven Module.
Click on Next button.
Select the check box ‘Create a simple project (skip archetype selection).
Set module name as jdbc-connector and set the parent project to connectors.
Click on Next button.
Use default options (packaging is set to jar) and click on Finish button.
Project structure is updated like below.
jdbc-connector pom is created 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>
<parent>
<groupId>com.sample.app</groupId>
<artifactId>connectors</artifactId>
<version>1</version>
</parent>
<artifactId>jdbc-connector</artifactId>
</project>
Let’s use the some of the dependencies defined in parent project in the jdbc-connector module.
Updated jdbc-connector 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>connectors</artifactId>
<version>1</version>
</parent>
<artifactId>jdbc-connector</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
</dependencies>
</project>
Clean all the projects (Project -> Clean). You will observe maven dependencies are downloaded and referenced in the child project.
You can download the complete working application from this link.
Previous Next Home
No comments:
Post a Comment