POM is short form of Project Object Model. Maven POM file in the project describe the resources of the project. This include all dependencies, directories of source code, test, plugin, goals etc. The POM file is named pom.xml. In earlier version of Maven (before 2.0) the name of this file was project.xml after version 2.0 it had changes to pom.xml. The pom file should be located into the root directory of the project.

POM file also has inheritance concept means a project divided into sub-projects. So parent project has one POM file and each sub-project have own POM file with inheriting parent POM file into the parent project. So with this structure we can built a project in one step including all sub-projects or either we can build one sub-project separately.

Maven reads the pom.xml file, then executes the goal.

Whenever we are creating POM file in our project first we should decide the project group id (i.e. groupId) and its name (i.e. artifactId) and its version for versioning of project in the repository it help in identifying.

Here is a minimal POM file:

<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.dineshonjava</groupId>
    <artifactId>java-api-learner</artifactId>
    <version>1.0.0.RELEASE</version>
</project>

XML elements of Maven POM file

  1. project-It is the root XML element of POM file.
  2. modelVersion-It is the sub element of project. It specifies the modelVersion. It should be set to 4.0.0.
  3. groupId-It is name of group or organization of project. It is the sub element of project element.
  4. artifactId– It is name of project (~artifact) what we are creating and it is the sub element of project element. An artifact is something that is either produced or used by a project.
  5. version-It specifies the version of the artifact under given group.

The above groupId, artifactId and version elements would result in a JAR file being built and put into the local Maven repository at the following path:

MAVEN_REPO/com/dineshonjava/java-api-learner/1.0.0.RELEASE/java-api-learner-1.0.0.RELEASE.jar

As above listed elements are mandatory. All POM files require at least the project element and three mandatory fields: groupId, artifactId, version.

Super POM

All Maven POM files inherit from a super POM. If no super POM is specified, the POM file inherits from the base POM just like Object class in java.

<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>
    
        <parent>
          <groupId>org.dineshonjava</groupId>
          <artifactId>parent-project</artifactId>
          <version>2.0</version>
           <relativePath>../parent-project</relativePath>
        </parent>
    

    <artifactId>parent-project</artifactId>
    ...
</project>

An inheriting POM file may override settings from a super POM. Just specify new settings in the inheriting POM file

@ImageSource-tutorials.jenkov.com

An easy way to look at the default configurations of the super POM is by running the following command: mvn help:effective-pom

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago