The original POM:…

Build the project

The Java development process may use many dependent packages, either from third parties or from self-built Maven repositories. Versioning compatibility management between dependent packages is a thorny problem in development. Maven logs all dependencies in a pom.xml file, and then automatically downloads and builds these dependency packages.

What is a POM

POM, the Project Object Model, is the basic unit of Maven. This XML file contains all the information about the project configuration that will be used by Maven to build the project. It contains some default values. The target folder is the build directory, the SRC /main/ Java folder is the source code directory, and the SRC /test/ Java directory is the test directory. When running Maven, Maven first looks for the folder where the POM is located, then retrieves the configuration information in it, and finally executes the task it is about to do.

Super POM

POM files can have dependencies, and a POM file can use the configuration information of the POM files that it inherits. All the POM file implicitly inheritance in super – POM, super – the content of the POM visible:…

Minimal POM (Minimal POM)

A minimum valid POM file needs to contain the following configuration information

Project: The root of the configuration information

ModelVersion: Set to 4.0.0

GroupId: The group ID of the project

ArtifactId: The ID of the project

Version: The version of the project

Here is an example

The < project > < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Mycompany. App < / groupId > < artifactId > my - app < / artifactId > <version>1</version> </project>

The group ID, ID, and version form the full legal name of a project, such as the group ID com.xich. App, the project ID super-teacher, and the version number 1.0.0.RELEASE. Then a legitimate full project name would be 1.0.0.release.

The minimum POM configuration above does not tell Maven what goal to execute, so Maven will execute the default goal (remember, these default configurations are derived from the super-POM inherited from the POM file). The default goal is to package the current project as a JAR package.

Minimum POM configuration or Maven warehouse address, super – POM declared in the warehouse for, it will download the required packages from the warehouse.

Project inheritance

The elements in the POM that can be merged include:

Dependencies: rely on

Developers and Returns: Developers and Contributors

Plugin Lists: Plugins list

Plugin Executions with Matching: Execute with ID

Plugin Configuration

Resources: resources

You can declare inheritance using the parent element.


Taking the POM of the smallest POM as an example,, we introduce another item:

Directory structure of the project:

    |    --pom.xml

You can see that module-alpha.pom.xml is the POM configuration for

Now we want as the parent project, so module-alpha’s pom.xml should be

The < project > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > com. Mycompany. App < / groupId > <artifactId>my-app</artifactId> <version>1</version> <relativePath>.. /pom.xml</relativePath> </parent> <groupId></groupId> <artifactId>module-alpha</artifactId> <version>1</version> </project>

Where relativePath specifies the location of the POM

Project Aggregation

Project consolidation is similar to project inheritance, but instead of specifying the POM of the module as the parent POM, it specifies the modules in the parent POM to be integrated. This way, the parent project knows what its modules are, and if a Maven directive is executed in the parent project, the Maven directive will also be executed in the parent project’s module.

Specific usage:

  1. Package the parent POMpom
  2. Specifies the directory of its child modules in the parent POM

Again, take the two POMs above as examples

Com.mycompany. app:my-app:1 located at.\pom.xml

The < project > < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Mycompany. App < / groupId > < artifactId > my - app < / artifactId > <version>1</version> </project>

Com. located at.\module-alpha\pom.xml

The < project > < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Mycompany. App < / groupId > < artifactId > module - alpha < / artifactId > <version>1</version> </project>

To integrate module-alpha into my-app, change the POM configuration of my-app to:

The < project > < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Mycompany. App < / groupId > < artifactId > my - app < / artifactId > <version>1</version> <packaging>pom</packaging> <modules> <module>module-alpha</module> </modules> </project>

The above POM does not declare a path to module-alpha because, in general, a module’s POM is in a directory one level below the parent POM, and the directory name is usually the module name. If the module’s path does not conform to the above specification, the module’s path should be indicated in the module element, for example:

<modules> <module>.. /my-module</module> </modules>


POM has predefined variables and custom variables. For example, if the version number element version is a child of the project element, you can reuse this variable in the following ways.


Variables are processed after inheritance. Therefore, if variables that are already in a parent POM are defined in the POM, the variables in the parent POM are overwritten.

Special variables

Project.basedir: The directory of the current project

Project.baseUri: The URL directory of the current project timestamp: The timestamp of the project build

The format of the timestamp can be customized as follows:


Custom timestamp format syntax:…

Custom variables can use the properties element. Example:

<project> ... <properties> </mavenVersion> </properties> < Dependencies > < Dependency > < Dependency > <groupId>org.apache.maven</groupId> <artifactId>maven-artifact</artifactId> <version>${mavenVersion}</version> </dependency> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-core</artifactId> <version>${mavenVersion}</version> </dependency> </dependencies> ... </project>

In the example above, MavenVersion is defined in properties, and both are introduced in dependencies using this variable as the version number. The syntax for calling the variable is ${variable name}.