Horje
Maven POM

The Maven Project Object Model (POM) is a fundamental concept in Apache Maven. The POM is an XML-based file that contains information about project configuration and project dependencies, including their versions, which are used by Maven to build the project. The POM file specifies dependencies, build plugins, and other project-related settings. In this article, we explain the POM in the Maven build automation tool.

Maven uses the POM to manage the project’s dependencies and build lifecycle, and plugins. When you run Maven commands, it reads the pom.xml file to understand how to compile, test, and package the code. It also resolves and downloads the necessary dependencies specified in the POM from remote repositories.

Workflow of Maven POM:

  • Initialization: The Maven reads the pom.xml file and initializes the build process.
  • Dependency Resolution: Downloads the Specified dependencies from remote repositories.
  • Build Life cycle Execution: Executes the build life cycle phases like compile, test, package, verify and other phases.
  • Plugin Execution: Runs the configured plugins for various takes like code analysis and other tasks.
  • Packaging: Packages the compiled code into specified format like JAR, WAR and other formats.
  • Deployment: Deploys the packaged code to a remote repository or server.
POM-Work-Flow



Key Components of a POM File:

  • Project Coordinates:
    • <groupId>: Defines the group or organization to which the project belongs.
    • <artifactId>: The unique name of the project.
    • <version>: The specific version of the project.
  • Build Configuration:
    • <build>: Contains the build configuration for the project, such as the source directory, test source directory, and output directory.
  • Dependencies:
    • <dependencies>: Contains the list of dependencies required by the project. Each dependency is defined by its groupId, artifactId, version, and scope.
  • Plugins:
    • <plugins>: Defines the list of plugins that are used in the build process.
  • Repositories:
    • <repositories>: Defines the list of remote repositories from which dependencies and plugins can be downloaded.
  • Profiles:
    • <profiles>: Allows the definition of different configurations for different environments, such as development, testing, and production.


Example Project of a Basic POM File

Here, we created a simple maven project by using Spring Tool Suite IDE.

  • First open the STS IDE.
  • Click on new and select Spring Stater Project option.
  • Then provide project related information like project name, package name, artifact name and other things.
  • Now select required dependencies for the project.
  • Now click on next.
  • Once project is developed run the project.

Project Dependencies:

pom.xml file:

XML
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.app</groupId>
    <artifactId>mavencommends</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mavencommends</name>
    <description>Spring Reactive</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


pom.xml file in Project Structure:

pom.xml File


Uses Of Maven POM

  • Dependency Management: It can declares and manages project dependencies.
  • Build Configuration: It can specifies plugins and their configuration for various build tasks.
  • Project Information: It contains metadata like project version, description, developers and other things.
  • Reporting: Defines reporting plugins for generating Project reports like Java Docs.
  • Build Profiles: It configures Different build profiles for various environments.

Advantages of Maven POM

The Maven Tools provides lot of advantages below we listed them.

  • Standardization: Provides a standard way to manage project builds.
  • Dependency Management: Automatically handles dependency resolution and version conflicts.
  • Reproducibility: It Builds are reproducible by using versioned dependencies.
  • Integration with CI/CD: Easily integrates with continuous integration and delivery pipelines.
  • Extensibility: Supports a wide range of plugins to extend build functionality.





Reffered: https://www.geeksforgeeks.org


Advance Java

Related
Using Git as a Backend for Spring Cloud Config Server Using Git as a Backend for Spring Cloud Config Server
Difference Between hasRole() and hasAuthority() in Spring Security Difference Between hasRole() and hasAuthority() in Spring Security
Maven Central Repository Maven Central Repository
Reactive JWT Authentication Using Spring WebFlux Reactive JWT Authentication Using Spring WebFlux
Spring WebFlux Application Integration with Reactive Messaging System Kafka Spring WebFlux Application Integration with Reactive Messaging System Kafka

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14