Maven POM
POM (Project Object Model) is Maven's core configuration file, in XML format, named pom.xml by default.
POM is the basic unit of work in a Maven project, it is an XML file containing basic project information, used to describe how the project is built, declare project dependencies, etc.
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 POM:
- Project Dependencies
- Plugin
- Execute goals
- Project build profile
- Project version
- Project developer list
- Related mailing list information
Before creating a POM, we first need to describe the project group (groupId), the project's unique ID.
<
project
<
modelVersion
4.0.0
modelVersion
<
groupId
com.companyname.project-group
groupId
<
artifactId
project
artifactId
<
version
1.0
version
projectAll POM files need the project element and three required fields: groupId, artifactId, 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 bank-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's 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
<
project
<
modelVersion
4.0.0
modelVersion
<
groupIdcom.
example
groupId
<
artifactId
my
app
artifactId
<
version
1.0.0
version
<
packaging
jar
packaging
<
nameMy Application
name
<
description
A demo project
description
<
url
https:
//example.com
<
dependencies
...
dependencies
<
build
...
build
<
properties
...
properties
<
repositories
...
repositories
project
Project coordinates (required):
| Element | Description | Example |
|---|---|---|
modelVersion |
POM model version (fixed 4.0.0) |
|
groupId |
Organization/company identifier (reverse domain name) | |
artifactId |
Project name | |
version |
Project version | |
packaging |
Packaging type (jar/war/pom) |
|
Meta information (optional):
| Element | Description |
|---|---|
name |
Project name (for display) |
description |
Project description |
url |
Project homepage |
licenses |
License information |
organization |
Organization information |
Example
<
name
My Awesome App
name
<
description
A demo project
for
learning Maven
description
<
url
https:
//github.com/example/my-app
Dependency Management
Project dependencies can be defined in the POM file:
junit
junit
4.12
test
| Element | Description |
|---|---|
groupId |
Dependency's organization identifier |
artifactId |
Dependency's project name |
version |
Dependency's version |
scope |
Dependency scope (compile/test/provided/runtime) |
optional |
Whether it is an optional dependency (default false) |
Plugin management
Plugins in the build process can also be defined in the POM file:
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
1.8
1.8
** Common plugins:**
-
maven-compiler-plugin: Specify the Java version -
maven-surefire-plugin: Control test execution -
maven-jar-plugin: Customize JAR package
Other Common Elements
properties: define some property variables in the project.
Used to define variables to avoid repetition:
11
UTF-8
Reference in dependencies:
${java.version}
repositories: repository configuration.
Specify remote repositories:
aliyun
https://maven.aliyun.com/repository/public
dependencyManagement: used to manage dependency versions, especially in multi-module projects.
org.springframework
spring-core
5.3.9
profiles: used to define different build configurations, allowing builds based on different environments.
development
dev
production
prod
Inheritance and Aggregation
Inherited: Through the parent element, one POM file can inherit the configuration of another POM file:
com.example
parent-project
1.0-SNAPSHOT
Aggregation: Through the modules element, one POM file can manage multiple submodules:
module1
module2
Complete example
Simple example of a complete POM:
Example
4.0.0
com.example
my-app
1.0.0
jar
My Application
A demo project
11
junit
junit
4.12
test
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
${java.version}
${java.version}
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 some default settings that can be inherited. Therefore, when Maven finds that it needs to download a dependency in the POM, it will go to the default repository http://repo1.maven.org/maven2 configured in the Super POM to download.
Maven uses the effective pom (Super pom plus the project's own configuration) to execute related goals, it helps developers do as little configuration as possible in pom.xml, 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:
<
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.companyname.project-group
groupId
<
artifactId
project
artifactId
<
version
1.0
version
projectIn 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 result of the Effective POM is just like what is displayed in the console; after inheritance and interpolation, the configuration takes effect.
<
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 h ttp://maven.apache.org/xsd/maven-4.0.0.xsd
"
<
modelVersion
4.0.0
modelVersion
<
groupId
com.companyname.project-group
groupId
<
artifactId
project
artifactId
<
version
1.0
version
<
build
<
sourceDirectory
C:\MVN\project\src\main\java
sourceDirectory
<
scriptSourceDirectory
src/main/scripts
scriptSourceDirectory
<
testSourceDirectory
C:\MVN\project\src\test\java
testSourceDirectory
<
outputDirectory
C:\MVN\project\target\classes
outputDirectory
<
testOutputDirectory
C:\MVN\project\target\test-classes
testOutputDirectory
<
resources
<
resource
<
mergeId
resource-0
mergeId
<
directory
C:\MVN\project\src\main\resources
directory
resource
resources
<
testResources
<
testResource
<
mergeId
resource-1
mergeId
<
directory
C:\MVN\project\src\test\resources
directory
testResource
testResources
<
directory
C:\MVN\project\target
directory
<
finalName
project-1.0
finalName
<
pluginManagement
<
plugins
<
plugin
<
artifactId
maven-antrun-plugin
artifactId
<
version
1.3
version
plugin
<
plugin
<
artifactId
maven-assembly-plugin
artifactId
<
version
2.2-beta-2
version
plugin
<
plugin
<
artifactId
maven-clean-plugin
artifactId
<
version
2.2
version
plugin
<
plugin
<
artifactId
maven-compiler-plugin
artifactId
<
version
2.0.2
version
plugin
<
plugin
<
artifactId
maven-dependency-plugin
artifactId
<
version
2.0
version
plugin
<
plugin
<
artifactId
maven-deploy-plugin
artifactId
<
version
2.4
version
plugin
<
plugin
<
artifactId
maven-ear-plugin
artifactId
<
version
2.3.1
version
plugin
<
plugin
<
artifactId
maven-ejb-plugin
artifactId
<
version
2.1
version
plugin
<
plugin
<
artifactId
maven-install-plugin
artifactId
<
version
2.2
version
plugin
<
plugin
<
artifactId
maven-jar-plugin
artifactId
<
version
2.2
version
plugin
<
plugin
<
artifactId
maven-javadoc-plugin
artifactId
<
version
2.5
version
plugin
<
plugin
<
artifactId
maven-plugin-plugin
artifactId
<
version
2.4.3
version
plugin
<
plugin
<
artifactId
maven-rar-plugin
artifactId
<
version
2.2
version
plugin
<
plugin
<
artifactId
maven-release-plugin
artifactId
<
version
2.0-beta-8
version
plugin
<
plugin
<
artifactId
maven-resources-plugin
artifactId
<
version
2.3
version
plugin
<
plugin
<
artifactId
maven-site-plugin
artifactId
<
version
2.0-beta-7
version
plugin
<
plugin
<
artifactId
maven-source-plugin
artifactId
<
version
2.0.4
version
plugin
<
plugin
<
artifactId
maven-surefire-plugin
artifactId
<
version
2.4.3
version
plugin
<
plugin
<
artifactId
maven-war-plugin
artifactId
<
version
2.1-alpha-2
version
plugin
plugins
pluginManagement
<
plugins
<
plugin
<
artifactId
maven-help-plugin
artifactId
<
version
2.1.1
version
plugin
plugins
build
<
repositories
<
repository
<
snapshots
<
enabled
false
enabled
snapshots
<
id
central
id
<
nameMaven Repository Switchboard
name
<
url
http://repo1.maven.org/maven2
url
repository
repositories
<
pluginRepositories
<
pluginRepository
<
releases
<
updatePolicy
never
updatePolicy
releases
<
snapshots
<
enabled
false
enabled
snapshots
<
id
central
id
<
nameMaven Plugin Repository
name
<
url
http://repo1.maven.org/maven2
url
pluginRepository
pluginRepositories
<
reporting
<
outputDirectory
C:\MVN\project\target/site
outputDirectory
reporting
projectIn the above pom.xml, 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 either.
Maven provides a large number of archetype plugins to create projects, including project structure and pom.xml.
Detailed Explanation of All 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 | ||||
|
Project Structure | POM model version | 4.0.0 |
Is |
|
Coordinates | Organization identifier (reverse domain name) | com.example |
Is |
|
Coordinates | Project name | my-project |
Is |
|
Coordinates | Project version | 1.0.0-SNAPSHOT |
Is |
|
Project type | Packaging format | jar/war/pom |
No (default jar) |
|
Metadata | Project display name | My Application |
No |
|
Metadata | Project description | A demo project |
No |
|
Metadata | Project homepage URL | https://example.com |
No |
| Dependency Management | ||||
|
Depends on | Dependency list container | Contains multiple |
No |
|
Depends on | Single dependency definition | ContainsgroupIdand other subtags |
Optional |
|
Depends on | Dependency scope | compile/test/provided/runtime |
No (default compile) |
|
Depends on | Whether it is an optional dependency | true/false |
No (default false) |
|
Depends on | Exclude transitive dependencies | ContainsList |
No |
| Build configuration | ||||
|
Build | Build configuration container | Contains plugin/resource and other configurations | No |
|
Build | Plugin list container | Contains multiple |
No |
|
Build | Single plugin definition | Need to specifygroupIdandartifactId |
Optional |
|
Build | Resource file configuration | definePath |
No |
|
Build | Test resource file configuration | Similar to |
No |
|
Build | Final packaging file name | my-app |
No |
| Environment Configuration | ||||
|
Configure | Custom variable container | Define key-value pairs | No |
|
property | Java version variable | 11/17etc. |
No |
|
Repository | Custom remote repository list | Contains |
No |
|
Repository | Custom plugin repository | Similar to |
No |
| Multi-module management | ||||
|
module | Submodule list | Contains multiple |
Required for aggregation projects |
|
inheritance | Parent POM reference | Need to specify parent project coordinates | Required for inheritance projects |
|
Depends on | Manage dependency versions uniformly | Define versions but do not introduce | No |
|
Configure | Environment profile container | Define different environment configurations | No |
| Other information | ||||
|
Legal | License information | Contains |
No |
|
People | Developer list | Contains |
No |
|
People | Contributor list | Similar to |
No |
|
Management | Issue tracking system | Define issue system URL | No |
<
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.0http://maven.apache.org/maven-v4_0_0.xsd
"
<
parent
<
artifactId
/>
<
groupId
/>
<
version
/>
<
relativePath
/>
parent
<
modelVersion
4.0.0
modelVersion
<
groupId
asia.banseon
groupId
<
artifactId
banseon-maven2
artifactId
<
packaging
jar
packaging
<
version
1.0-SNAPSHOT
version
<
name
banseon-maven
name
<
url
http://www.baidu.com/banseon
url
<
descriptionA maven project to study maven.
description
<
prerequisites
<
maven
/>
prerequisites
<
issueManagement
<
system
jira
system
<
url
http://jira.baidu.com/banseon
url
issueManagement
<
ciManagement
<
system
/>
<
url
/>
<
notifiers
<
notifier
<
type
/>
<
sendOnError
/>
<
sendOnFailure
/>
<
sendOnSuccess
/>
<
sendOnWarning
/>
<
address
/>
<
configuration
/>
notifier
notifiers
ciManagement
<
inceptionYear
/>
<
mailingLists
<
mailingList
<
name
Demo
name
<
post
banseon@126.com
post
<
subscribe
banseon@126.com
subscribe
<
unsubscribe
banseon@126.com
unsubscribe
<
archive
http:/hi.baidu.com/banseon/demo/dev/
archive
mailingList
mailingLists
<
developers
<
developer
<
idHELLO WORLD
id
<
name
banseon
name
<
email
banseon@126.com
email
<
url
/>
<
roles
<
roleProject Manager
role
<
role
Architect
role
roles
<
organization
demo
organization
<
organizationUrl
http://hi.baidu.com/banseon
organizationUrl
<
properties
<
dept
No
dept
properties
<
timezone
-5
timezone
developer
developers
<
contributors
<
contributor
<
name
/>
<
email
/>
<
url
/>
<
organization
/>
<
organizationUrl
/>
<
roles
/>
<
timezone
/>
<
properties
/>
contributor
contributors
<
licenses
<
license
<
nameApache 2
name
<
url
http://www.baidu.com/banseon/LICENSE-2.0.txt
url
<
distribution
repo
distribution
<
commentsA business-friendly OSS license
comments
license
licenses
<
scm
<
connection
scm:svn:http://svn.baidu.com/banseon/maven/banseon/banseon-maven2-trunk(dao-trunk)
connection
<
developerConnection
scm:svn:http://svn.baidu.com/banseon/maven/banseon/dao-trunk
developerConnection
<
tag
/>
<
url
http://svn.baidu.com/banseon
url
scm
<
organization
<
name
demo
name
<
url
http://www.baidu.com/banseon
url
organization
<
build
<
sourceDirectory
/>
<
scriptSourceDirectory
/>
<
testSourceDirectory
/>
<
outputDirectory
/>
<
testOutputDirectory
/>
<
extensions
<
extension
<
groupId
/>
<
artifactId
/>
<
version
/>
extension
extensions
<
defaultGoal
/>
<
resources
<
resource
<
targetPath
/>
<
filtering
/>
<
directory
/>
<
includes
/>
<
excludes
/>
resource
resources
<
testResources
<
testResource
<
targetPath
/>
<
filtering
/>
<
directory
/>
<
includes
/>
<
excludes
/>
testResource
testResources
<
directory
/>
<
finalName
/>
<
filters
/>
<
pluginManagement
<
plugins
<
plugin
<
groupId
/>
<
artifactId
/>
<
version
/>
<
extensions
/>
<
executions
<
execution
<
id
/>
<
phase
/>
<
goals
/>
<
inherited
/>
<
configuration
/>
execution
executions
<
dependencies
<
dependency......
dependency
dependencies
<
inherited
/>
<
configuration
/>
plugin
plugins
pluginManagement
<
plugins
<
plugin
<
groupId
/>
<
artifactId
/>
<
version
/>
<
extensions
/>
<
executions
<
execution
<
id
/>
<
phase
/>
<
goals
/>
<
inherited
/>
<
configuration
/>
execution
executions
<
dependencies
<
dependency......
dependency
dependencies
<
goals
/>
<
inherited
/>
<
configuration
/>
plugin
plugins
build
<
profiles
<
profile
<
id
/>
<
activation
<
activeByDefault
/>
<
jdk
/>
<
os
<
nameWindows XP
name
<
family
Windows
family
<
arch
x86
arch
<
version
5.1.2600
version
os
<
property
<
name
mavenVersion
name
<
value
2.0.3
value
property
<
file
<
exists
/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/
exists
<
missing
/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/
missing
file
activation
<
build
<
defaultGoal
/>
<
resources
<
resource
<
targetPath
/>
<
filtering
/>
<
directory
/>
<
includes
/>
<
excludes
/>
resource
resources
<
testResources
<
testResource
<
targetPath
/>
<
filtering
/>
<
directory
/>
<
includes
/>
<
excludes
/>
testResource
testResources
<
directory
/>
<
finalName
/>
<
filters
/>
<
pluginManagement
<
plugins
<
plugin
<
groupId
/>
<
artifactId
/>
<
version
/>
<
extensions
/>
<
executions
<
execution
<
id
/>
<
phase
/>
<
goals
/>
<
inherited
/>
<
configuration
/>
execution
executions
<
dependencies
<
dependency......
dependency
dependencies
<
goals
/>
<
inherited
/>
<
configuration
/>
plugin
plugins
pluginManagement
<
plugins
<
plugin
<
groupId
/>
<
artifactId
/>
<
version
/>
<
extensions
/>
<
executions
<
execution
<
id
/>
<
phase
/>
<
goals
/>
<
inherited
/>
<
configuration
/>
execution
executions
<
dependencies
<
dependency......
dependency
dependencies
<
goals
/>
<
inherited
/>
<
configuration
/>
plugin
plugins
build
<
modules
/>
<
repositories
<
repository
<
releases
<
enabled
/>
<
updatePolicy
/>
<
checksumPolicy
/>
releases
<
snapshots
<
enabled
/>
<
updatePolicy
/>
<
checksumPolicy
/>
snapshots
<
id
/>
<
name
/>
<
url
/>
<
layout
/>
repository
repositories
<
pluginRepositories
<
pluginRepository
<
releases
<
enabled
/>
<
updatePolicy
/>
<
checksumPolicy
/>
releases
<
snapshots
<
enabled
/>
<
updatePolicy
/>
<
checksumPolicy
/>
snapshots
<
id
/>
<
name
/>
<
url
/>
<
layout
/>
pluginRepository
pluginRepositories
<
dependencies
<
dependency......
dependency
dependencies
<
reports
/>
<
reporting......
reporting
<
dependencyManagement
<
dependencies
<
dependency......
dependency
dependencies
dependencyManagement
<
distributionManagement
......
distributionManagement
<
properties
/>
profile
profiles
<
modules
/>
<
repositories
<
repository
<
releases
<
enabled
/>
<
updatePolicy
/>
<
checksumPolicy
/>
releases
<
snapshots
<
enabled
/>
<
updatePolicy
/>
<
checksumPolicy
/>
snapshots
<
id
banseon-repository-proxy
id
<
name
banseon-repository-proxy
name
<
url
http://192.168.1.169:9999/repository/
url
<
layout
default
layout
repository
repositories
<
pluginRepositories
<
pluginRepository......
pluginRepository
pluginRepositories
<
dependencies
<
dependency
<
groupId
org.apache.maven
groupId
<
artifactId
maven-artifact
artifactId
<
version
3.8.1
version
<
type
jar
type
<
classifier
classifier
<
scope
test
scope
<
systemPath
systemPath
<
exclusions
<
exclusion
<
artifactId
spring-core
artifactId
<
groupId
org.springframework
groupId
exclusion
exclusions
<
optional
true
optional
dependency
dependencies
<
reports
reports
<
reporting
<
excludeDefaults
/>
<
outputDirectory
/>
<
plugins
<
plugin
<
groupId
/>
<
artifactId
/>
<
version
/>
<
inherited
/>
<
configuration
/>
<
reportSets
<
reportSet
<
id
/>
<
configuration
/>
<
inherited
/>
<
reports
/>
reportSet
reportSets
plugin
plugins
reporting
<
dependencyManagement
<
dependencies
<
dependency......
dependency
dependencies
dependencyManagement
<
distributionManagement
<
repository
<
uniqueVersion
/>
<
id
banseon-maven2
id
<
namebanseon maven2
name
<
url
file://${basedir}/target/deploy
url
<
layout
/>
repository
<
snapshotRepository
<
uniqueVersion
/>
<
id
banseon-maven2
id
<
nameBanseon-maven2 Snapshot Repository
name
<
url
scp://svn.baidu.com/banseon:/usr/local/maven-snapshot
url
<
layout
/>
snapshotRepository
<
site
<
id
banseon-site
id
<
namebusiness api website
name
<
url
scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web
url
site
<
downloadUrl
/>
<
relocation
<
groupId
/>
<
artifactId
/>
<
version
/>
<
message
/>
relocation
<
status
/>
distributionManagement
<
properties
/>
project