Maven POM
POM (Project Object Model) is the core configuration file of Maven, in XML format, with the default name ofpom.xml。
POM is the basic unit of work in a Maven project. It is an XML file that contains the basic information of the project, used to describe how the project is built, declare project dependencies, and so on.
When executing a task or goal, Maven looks for the POM in the current directory, obtains the required configuration information, and then executes the goal.
The following configurations can be specified in the POM:
- Project dependencies
- Plugins
- Execution goals
- Project build profiles
- Project version
- Project developer list
- Related mailing list information
Before creating a POM, we first need to describe the project group (groupId), the unique ID of the project.
All POM files require the project element and three required fields: groupId, artifactId, and version.
| Node | Description |
|---|---|
| project | The root tag of the project. |
| modelVersion | The model version needs to be set to 4.0. |
| groupId | This is the identifier of the project group. It is usually unique within an organization or project. For example, a banking organization com.companyname.project-group owns all banking-related projects. |
| artifactId | This is the identifier of the project. It is usually the name of the project. For example, Consumer Banking. groupId and artifactId together define the location of the artifact in the repository. |
| version |
This is the version number of the project. In the artifact repository, it is used to distinguish different versions. For example: com.company.bank:consumer-banking:1.0 com.company.bank:consumer-banking:1.1 |
Basic structure of a POM file:
Example
<!-- 1. Basic Information-->
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<!-- 2. Meta information-->
<name>My Application</name>
<description>A demo project</description>
<url>https://example.com</url>
<!-- 3. Dependency management-->
<dependencies>...</dependencies>
<!-- 4. Build configuration-->
<build>...</build>
<!-- 5. Environment configuration-->
<properties>...</properties>
<repositories>...</repositories>
</project>
Project coordinates (required):
| Element | Description | Example |
|---|---|---|
modelVersion | POM model version (fixed4.0.0) | <modelVersion>4.0.0</modelVersion> |
groupId | Organization/company identifier (reverse domain name) | <groupId>com.example</groupId> |
artifactId | Project name | <artifactId>my-app</artifactId> |
version | Project version | <version>1.0.0</version> |
packaging | Packaging type (jar/war/pom) | <packaging>jar</packaging> |
Meta information (optional):
| Element | Description |
|---|---|
name | Project name (for display) |
description | Project description |
url | Project homepage |
licenses | License information |
organization | Organization information |
Example
<description>A demo project for learning Maven</description>
<url>https://github.com/example/my-app</url>
Dependency Management
Dependencies of the project can be defined in the POM file:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>| Element | Description |
|---|---|
groupId | The organization identifier of the dependency |
artifactId | The project name of the dependency |
version | The version of the dependency |
scope | Dependency scope (compile/test/provided/runtime) |
optional | Whether it is an optional dependency (defaultfalse) |
Plugin Management
Plugins used during the build process can also be defined in the POM file:
<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>Common plugins:
maven-compiler-plugin: Specify Java versionmaven-surefire-plugin: Control test executionmaven-jar-plugin: Customize JAR package
Other Common Elements
properties: Define some property variables in the project.
Used to define variables and avoid duplication:
<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>Reference in dependencies:
<version>${java.version}</version>
repositories: Repository configuration.
Specify remote repository:
<repositories>
<repository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
</repositories>
dependencyManagement: Used to manage dependency versions, especially in multi-module projects.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.9</version>
</dependency>
</dependencies>
</dependencyManagement>profiles: Used to define different build configurations, allowing builds to be performed according to different environments.
<profiles>
<profile>
<id>development</id>
<properties>
<environment>dev</environment>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<environment>prod</environment>
</properties>
</profile>
</profiles>
Inheritance and Aggregation
Inheritance:Through the parent element, a POM file can inherit the configuration of another POM file:
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>Aggregation:Through the modules element, a POM file can manage multiple sub-modules:
<modules>
<module>module1</module>
<module>module2</module>
</modules>Complete Example
A simple example of a complete POM:
Example
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>My Application</name>
<description>A demo project</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</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>
</project>
Parent (Super) POM
The Parent (Super) POM is Maven's default POM. All POMs inherit from a parent POM (whether or not this parent POM is explicitly defined). The parent POM contains default settings that can be inherited. Therefore, when Maven finds that it needs to download a dependency in the POM, it will download it from the default repository http://repo1.maven.org/maven2 configured in the Super POM.
Maven uses the effective POM (the Super POM plus the project's own configuration) to execute related goals. It helps developers make as few configurations as possible in pom.xml, but of course these configurations can be overridden.
Use the following command to view the Super POM default configuration:
mvn help:effective-pom
Next, we create the directory MVN/project, and create pom.xml in that directory, with the following content:
In the command console, enter the MVN/project directory and execute the following command:
C:\MVN\project>mvn help:effective-pom
Maven will start processing and display the effective-pom.
[INFO] Scanning for projects... Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom ... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:36 min [INFO] Finished at: 2018-09-05T11:31:28+08:00 [INFO] Final Memory: 15M/149M [INFO] ------------------------------------------------------------------------
The resulting Effective POM is as shown in the console; after inheritance and interpolation, the configuration takes effect.
In the pom.xml above, you can see the default project source directory structure, output directory, required plugins, repositories, and report directories that Maven needs when executing goals.
Maven's pom.xml file does not need to be written manually.
Maven provides a large number of archetype plugins to create projects, including the project structure and pom.xml.
Detailed Explanation of POM Tags
The following is a categorized explanation of all important tags in pom.xml, covering core content such as project information, dependency management, build configuration, and environment settings.
Tags that all POM files must contain:modelVersion, groupId, artifactId, and version。
| Tag | Category | Description | Example/Optional values | Whether required |
|---|---|---|---|---|
| Basic Information | ||||
<modelVersion> |
Project Structure | POM model version | 4.0.0 |
Yes |
<groupId> |
Coordinates | Organization identifier (reverse domain name) | com.example |
Yes |
<artifactId> |
Coordinates | Project name | my-project |
Yes |
<version> |
Coordinates | Project version | 1.0.0-SNAPSHOT |
Yes |
<packaging> |
Project type | Packaging format | jar/war/pom |
No (default jar) |
<name> |
Meta information | Project display name | My Application |
no |
<description> |
Meta information | Project description | A demo project |
no |
<url> |
Meta information | Project homepage URL | https://example.com |
no |
| Dependency Management | ||||
<dependencies> |
Dependency | Dependency list container | Contains multiple<dependency> |
no |
<dependency> |
Dependency | Single dependency definition | ContainsgroupIdand other sub-tags |
Optional |
<scope> |
Dependency | Dependency scope | compile/test/provided/runtime |
No (default compile) |
<optional> |
Dependency | Whether optional dependency | true/false |
No (default false) |
<exclusions> |
Dependency | Exclude transitive dependencies | Contains<exclusion>List |
no |
| Build configuration | ||||
<build> |
Build | Build configuration container | Contains plugin/resource configurations, etc. | no |
<plugins> |
Build | Plugin list container | Contains multiple<plugin> |
no |
<plugin> |
Build | Single plugin definition | Must specifygroupIdandartifactId |
Optional |
<resources> |
Build | Resource file configuration | Definition<resource>Path |
no |
<testResources> |
Build | Test resource file configuration | Similar<resources> |
no |
<finalName> |
Build | Final packaging file name | my-app |
no |
| Environment configuration | ||||
<properties> |
Configuration | Custom variable container | Defines key-value pairs | no |
<java.version> |
Properties | Java version variable | 11/17wait |
no |
<repositories> |
Repositories | Custom remote repository list | Contains<repository> |
no |
<pluginRepositories> |
Repository | Custom plugin repository | Similar<repositories> |
no |
| Multi-module management | ||||
<modules> |
Modules | Submodule list | Contains multiple<module> |
Required for aggregator projects |
<parent> |
Inherit | Parent POM reference | Must specify parent project coordinates | Required for inherited projects |
<dependencyManagement> |
Dependencies | Centrally manage dependency versions | Defines versions but does not introduce | no |
<profiles> |
Configuration | Environment profile container | Defines different environment configurations | no |
| Other information | ||||
<licenses> |
Legal | License information | Contains<license> |
no |
<developers> |
People | Developer list | Contains<developer> |
no |
<contributors> |
People | Contributor list | Similar<developers> |
no |
<issueManagement> |
Management | Issue tracking system | Defines issue system URL | no |