Spring Application and Parent Maven Pom

Abdalrhmanalkraien
CodeX
Published in
4 min readMar 24, 2022

Introduction:

When we want to create a new web application using spring boot, ensure we need used maven to use some dependency from maven repository and the maven represent in Pom.xml, but if we have a microservice application from ten web applications or more than, and in some stage we need update some dependency to latest version, we need visit each project and update it manually, but with this article you don’t need to do it because all of the dependencies in the same location called Root or Parent project.

Inside the Parent, the project will put all the dependencies used in every project, and the other projects will see it and use any dependencies from the parent project.

Let’s go to see how we can make it.

Steps to create the project

Building the Root project:

To build the Root project we need open any Idea to write our code and after that, we need to go Pom.xml because in this article I use Maven, you can do the same thing with Gradle. and we can cover Root with Gradle but in another article.

The Root project (Parent):

The parent Project has one file Just Pom.xml, in the Pom.xml will put all Dependencies to use it into other projects,

we need to create the Maven project without any files because Just we need in this case Pom.xml to handle all of the Dependencies in other projects.

After creating it we can see an empty Pom.xml file. Actually, I will use the Parent project with the Spring boot application, and in this case, the Pom file will have a parent project from the spring framework to lunch your application.

The following snippet code represents the Parent project, with Spring parent dependency.

<?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>
<!-- the parent project from spring-boot framework -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId> <!-- the project name we need it to linking parent project with other projects -->
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>




<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<type>maven-plugin</type>
</dependency>

<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<type>maven-plugin</type>
</dependency>

</dependencies>
</project>

Now the parent project is complete and we will put other dependencies after creating the spring-boot project.

Simple Spring boot project

In this section of this article, we need to create a new spring boot project to map it with the parent project.

I think you can create a spring boot project if you don’t can do it you can use this link to initializer spring project https://start.spring.io/ and then open it by any Idea

Okay, After creating a new project, will have a new Pom.xml for the project and we need it to map with the Parent project.

In the Pom.xml in the Simple project just need to put a new tag called <parent></parent> and the parent tag will take me a few parameters to identify the parent for a simple project like the following snippet code.

<parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

Like the above code, I put some data specifications to parents Like project name and version and Package, you can find these details inside the parent pom.xml

And I will take all the dependencies from the simple project (Pom.xml) to the parent (Pom.xml).

Finally Code

Finally, the parent Pom.xml will have the following Code.

<?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>

<!-- the parent project from spring-boot framework -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>org.example</groupId>
<artifactId>parent</artifactId> <!-- the project name we need it to linking parent project with other projects -->
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>


<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<type>maven-plugin</type>
</dependency>

<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<type>maven-plugin</type>
</dependency>

<!-- for spring project -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
</dependencies>
</dependencyManagement>

<!-- for spring project -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

and the simple-project (Pom.xml) will have something the following code.

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.simpleproject</groupId>
<artifactId>simpleProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>simpleProject</name>
<description>simpleProject</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!-- the parent project -->
<parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

</project>

Review the Code in GitHub

you can find this code inside my Repo on the Git Hub by The following Link.

Conclusion:

If we work on the microservices project, we need to use a lot of dependencies and sometimes we need to upgrade the dependency to the latest version if we split the project to maven parent and web application as a service. We don’t need to visit each microservice to change the version of a dependency.

Here we can see the power of this technique in the split my jobs.

What the next:

I will create Common project for this project and use it.

don’t forget to clap on the articles and you can clap 50 times on each article. and you find my LinkedIn here

--

--

Abdalrhmanalkraien
CodeX
Writer for

I am a Java Developer and DevOps Engineer. I used the latest Technology for build completed development or Deployment