POM in Maven

 

In MavenPOM stands for Project Object Model. It is the fundamental unit of work in Maven and is represented by an XML file named pom.xml.


What is pom.xml?

The pom.xml file is located at the root of every Maven project and contains information about the project and configuration details used by Maven to build the project.


Key Roles of POM in Maven:


RoleDescription
Project ConfigurationDefines project metadata such as groupIdartifactIdversionname, etc.
Dependency ManagementLists libraries (JARs) the project depends on. Maven automatically downloads them.
Build SettingsSpecifies build plugins and goals (example: compiler, packaging, etc.).
Project HierarchySupports inheritance and aggregation via parent and modules.
Plugin ConfigurationAllows you to define behavior during build lifecycle phases.
Repository ConfigurationDefines remote repositories (like Maven Central, or private repos).
ProfilesSupports build profiles for different environments (dev, test, prod).




Basic Structure of 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.example</groupId>        <!-- Like a package name -->
  <artifactId>my-app</artifactId>       <!-- Project name -->
  <version>1.0.0</version>              <!-- Version of your build -->

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.3.9</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>



Important Tags in pom.xml:


TagPurpose
<groupId>Defines the project's group (usually a domain in reverse).
<artifactId>The name of the artifact (output like a JAR or WAR).
<version>Project version.
<packaging>Type of output: jar, war, pom, etc.
<dependencies>Lists all the external libraries your project needs.
<build>Customizes the build process using plugins.
<plugin>Defines a build plugin and configuration.
<repositories>Configures external repositories to search for dependencies.
<profiles>Creates environment-specific configurations.
<parent>Inherits configuration from a parent POM.




Example: Dependency Management


<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
  </dependency>
</dependencies>



Example: Parent POM

<parent>
  <groupId>com.mycompany</groupId>
  <artifactId>parent-project</artifactId>
  <version>1.0.0</version>
</parent>