Maven POM

Maven POM

The Project Object Model (POM) is the core of the Maven build system. The POM is an XML file named pom.xml that contains information about a project, its dependencies, build settings, and other configurations.

This tutorial will guide you through the essential elements of a Maven POM file and explain their purpose.

1. Basic structure:

A basic pom.xml file has the following structure:

<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> <!-- Add project information and configuration here --> </project> 

This XML structure includes the necessary namespaces and schema location for a Maven POM file. The <modelVersion> element specifies the POM model version, which is currently 4.0.0.

2. Project information:

Add the following elements inside the <project> element to provide basic information about your project:

  • <groupId>: The project's group ID, usually representing the organization or group that maintains the project.
  • <artifactId>: The project's artifact ID, a unique identifier for the project within the group.
  • <version>: The project's version, which follows the Maven versioning scheme.
  • <packaging>: The project's packaging type (e.g., jar, war, pom). If not specified, it defaults to jar.

Example:

<groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> 

3. Dependencies:

To manage external libraries used by your project, add a <dependencies> element inside the <project> element:

<dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.5</version> </dependency> </dependencies> 

Each <dependency> element represents a library that your project depends on. It includes the library's group ID, artifact ID, and version.

4. Build configuration:

To configure the build process, add a <build> element inside the <project> element:

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

The <plugins> element contains a list of plugins used to customize the build process. In this example, the maven-compiler-plugin is configured to compile the project using Java 1.8.

5. Repositories:

If your project depends on libraries hosted in custom repositories, add a <repositories> element inside the <project> element:

<repositories> <repository> <id>my-repo</id> <url>https://repo.example.com/maven2/</url> </repository> </repositories> 

Each <repository> element specifies a custom repository where Maven can download dependencies.

6. Defining properties:

To define custom properties, add a <properties> element inside the <project> element:

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <junit.version>5.7.0</junit.version> </properties> 

In this example, we have defined three properties: project.build.sourceEncoding, java.version, and junit.version. These properties can be referenced in other parts of the POM file using the ${propertyName} syntax.

7. Using properties in the POM:

Now, you can use the properties you defined throughout the POM file. For example, let's see how to use the java.version and junit.version properties in the <dependencies> and <build> elements:

<dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build> 

As you can see, we've replaced the hardcoded version numbers and Java version with their respective property references. This approach makes it easier to manage and update values in a centralized location.

Examples

  1. Configuring Maven projects with POM:

    • Maven uses Project Object Model (POM) files to configure projects.
    • The POM specifies project details, dependencies, plugins, and build configurations.
    <!-- Example Maven POM structure --> <project> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0.0</version> <!-- Additional project configurations... --> </project> 
  2. Defining project coordinates in Maven POM:

    • Project coordinates include groupId, artifactId, and version.
    • These coordinates uniquely identify a project and its version.
    <!-- Example project coordinates in POM --> <groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0.0</version> 
  3. Managing dependencies in Maven POM:

    • Declare dependencies in the <dependencies> section.
    • Maven resolves and downloads dependencies automatically.
    <!-- Example dependency in POM --> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>dependency</artifactId> <version>1.0.0</version> </dependency> <!-- Additional dependencies... --> </dependencies> 
  4. Customizing Maven build lifecycle in POM:

    • Customize the build lifecycle by defining build plugins and executions in the <build> section.
    <!-- Example customizing build lifecycle in POM --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <!-- Custom configurations... --> </plugin> <!-- Additional plugins... --> </plugins> </build> 
  5. Using properties and variables in Maven POM:

    • Define properties in the <properties> section for reusable values.
    <!-- Example using properties in POM --> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> 
  6. Configuring Maven plugins in POM.xml:

    • Configure plugins within the <build> section to customize their behavior.
    <!-- Example configuring a plugin in POM --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <!-- Plugin configurations... --> </plugin> <!-- Additional plugins... --> </plugins> </build> 

More Tags

if-statement stylish docker laravel-5.1 custom-controls android-dialer text quill jupyter-lab multilinestring

More Programming Guides

Other Guides

More Programming Examples