The original POM: https://maven.apache.org/guid…

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: https://maven.apache.org/ref/…

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 com.xich.app:super-teacher: 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 https://repo.maven.apache.org/maven2, 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.

example

Taking the POM of the smallest POM as an example, com.mycompany.app:my-app:1, we introduce another item: com.mycompany.app:module-alpha:1

Directory structure of the project:

.
    |--module-alpha
    |    --pom.xml
    --pom.xml

You can see that module-alpha.pom.xml is the POM configuration for com.mycompany.app:module-alpha:1.

Now we want com.mycompany.app:my-app:1 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>com.mycompany.app</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
example

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. mycompany.app:module-alpha:1 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>

variable

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.

<version>${project.version}</version>

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

Maven.build. timestamp: The timestamp of the project build

The format of the timestamp can be customized as follows:

<project>
  ...
  <properties>
    <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format>
  </properties>
  ...
</project>

Custom timestamp format syntax: https://docs.oracle.com/javas…

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