The core configuration file of Maven is(Project Object Model refers to the project object model), which describes the project's structure, dependencies, construction configuration and other information.
A good oneConfiguration not only improves the maintainability of the project, but also ensures consistency and reliability of the build process.
The following will discuss in depthEach configuration item and its usage method.
1. Basic structure of POM files
A typicalFiles usually contain the following main parts:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <modelVersion>4.0.0</modelVersion> <groupId></groupId> <artifactId>my-app</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <properties> <!-- Project Properties --> </properties> <dependencies> <!-- Project dependency --> </dependencies> <build> <!-- Build configuration --> </build> <profiles> <!-- Build configuration --> </profiles> </project>
1.1 Basic project information
-
modelVersion
: Specify the version of the POM model, usually4.0.0
。 -
groupId
: The group ID of the project, usually written inversely by the company or organization's domain name. -
artifactId
: The unique identifier of the project, usually the name of the project. -
version
: The version number of the project. -
packaging
: The packaging method of the project, such asjar
、war
、pom
wait.
Example:
<project> <modelVersion>4.0.0</modelVersion> <groupId></groupId> <artifactId>my-app</artifactId> <version>1.0.0</version> <packaging>jar</packaging> </project>
2. Project properties
properties
Part of it is used to define the properties used in the project, which can be used in thecited in other parts.
Example:
<properties> <>1.8</> <>UTF-8</> </properties>
2.1 Reference attributes
existOther parts of
${}
.
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>${}</source> <target>${}</target> </configuration> </plugin> </plugins> </build>
3. Project dependency
dependencies
Part of it is used to declare the dependency library required for the project. Each dependency includesgroupId
、artifactId
、version
etc.
Example:
<dependencies> <dependency> <groupId></groupId> <artifactId>spring-core</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies>
Dependency scope:
-
compile
: Default scope, dependencies are available at compile, test and runtime. -
test
: Available only during the test compilation and execution phases. -
runtime
: Available at runtime, but not at compile time. -
provided
: Available at compile and test, but provided by the container at runtime. -
system
: Similar toprovided
, but the path to dependencies needs to be explicitly specified.
4. Build configuration
build
Part of it is used to configure the construction process of the project, including plug-ins, resources, source code directories, etc.
Example:
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>${}</source> <target>${}</target> </configuration> </plugin> <plugin> <groupId></groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <skipTests>false</skipTests> </configuration> </plugin> </plugins> </build>
Common plugins:
-
maven-compiler-plugin
: Configure the Java compiler. -
maven-surefire-plugin
: Configure test execution. -
maven-jar-plugin
: Configure the generation of JAR files. -
maven-war-plugin
: Configure the generation of WAR files.
5. Build Profiles
profiles
Parts are used to define different build configuration files to use different configurations in different environments.
Example:
<profiles> <profile> <id>dev</id> <properties> <env>dev</env> </properties> <build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> </profile> <profile> <id>prod</id> <properties> <env>prod</env> </properties> <build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <skipTests>false</skipTests> </configuration> </plugin> </plugins> </build> </profile> </profiles>
5.1 Activate the configuration file
Specific configuration files can be activated via the command line:
mvn clean install -P dev
6. Best Practices
6.1 Version Management
- use
<dependencyManagement>
To manage dependent versions uniformly. - use
<parent>
Inherit the parent POM and avoid duplicate configuration.
6.2 Plug-in Management
- use
<pluginManagement>
To manage plug-in versions uniformly. - Avoid repeatedly configuring the same plug-ins in each project.
6.3 Dependency scope
- Use the dependency scope reasonably to avoid unnecessary dependency transmission.
- use
<scope>test</scope>
to limit the scope of test dependencies.
6.4 Document generation
- use
maven-javadoc-plugin
Generate project documentation. - use
maven-site-plugin
Generate project site.
7. Summary
It is the core configuration file of the Maven project, and it is configured reasonably
, can improve the maintainability and construction efficiency of the project.
masterThe various configuration items and their usage methods will help you better manage and build Java projects.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.