Maven is a highly configurable build tool that provides rich plug-in support to help developers automate the execution of various tasks, such as compilation, testing, packaging, deployment, etc. during the project construction process. Each plug-in can be customized to meet different build needs.
In this blog, we will explore in-depth how to customize the configuration of Maven build plug-ins, helping you adjust the behavior of plug-ins according to project needs and improve the construction efficiency.
1. Maven plugin overview
A Maven plug-in is a module that performs specific build tasks, and each plug-in consists of multiple targets (Goals). Plugins perform tasks at different stages of the Maven build life cycle.
For example:
-
maven-compiler-plugin
: Used to compile code. -
maven-jar-plugin
: Used to package JAR files. -
maven-surefire-plugin
: Used to perform unit tests.
Every plugin can be passedCustom configuration is performed, and the configuration method usually includes the version of the plug-in, the execution target, the configuration parameters, etc.
2. Custom configuration of Maven plug-in
The configuration of the Maven plug-in is usuallyFiled
<build>
Under the element<plugins>
Perform in the element.
2.1 Basic plug-in configuration structure
<build> <plugins> <plugin> <groupId>Plugin groupId</groupId> <artifactId>Plugin artifactId</artifactId> <version>Plugin版本</version> <configuration> <!-- Plugin-specific configuration --> </configuration> <executions> <execution> <phase>Target phase</phase> <!-- Specify the execution phase of the plugin during the build lifecycle --> <goals> <goal>Target1</goal> <goal>Target2</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
- groupId: The organization ID of the plugin.
- artifactId: The name of the plugin.
- version: The version of the plugin.
- configuration: Custom configuration items for plug-in.
- executions: Define the execution goals and execution stages of the plug-in.
3. Custom configuration of common plug-ins
3.1 maven-compiler-plugin
maven-compiler-plugin
It is a plug-in for Maven to compile Java code, and you can customize compilation options (such as source code version, target version, etc.).
Configuration example:
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <!-- Set the source code version --> <target>1.8</target> <!-- Set the target compiled version --> <encoding>UTF-8</encoding> <!-- Set the encoding method --> </configuration> </plugin> </plugins> </build>
Common configuration items:
-
source
: Specify the Java version of the source code (such as1.8
)。 -
target
: Specifies the Java version that generates the bytecode. -
encoding
: Set the encoding method of the source code.
3.2 maven-jar-plugin
maven-jar-plugin
Used to package the project as a JAR file, you can configure the name of the JAR file, attached files, etc.
Configuration example:
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifestEntries> <Main-Class></Main-Class> <!-- set up JAR of Main kind --> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build>
-
Main-Class
: Specify the main class of the JAR package. -
outputDirectory
: Set the JAR file output directory (default istarget/
)。 -
finalName
: Set the name of the final generated JAR file.
3.3 maven-surefire-plugin
maven-surefire-plugin
Used to perform unit tests. You can customize the test configuration, select different test frameworks, set up parallel testing, etc.
Configuration example:
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <includes> <include>**/*</include> <!-- Only execute Test The ending test class --> </includes> <parallel>methods</parallel> <!-- Enable parallel testing --> <threadCount>4</threadCount> <!-- Set the number of threads for parallel tests --> </configuration> </plugin> </plugins> </build>
-
includes
: Set the mode of the test class to include. -
parallel
: Configure the level of parallel execution, such asmethods
(Parallel by method level). -
threadCount
: Set the number of threads for parallel testing.
3.4 maven-dependency-plugin
maven-dependency-plugin
Used to manage and view dependencies for projects, you can perform operations such as copying dependencies, viewing dependency trees, etc.
Configuration example:
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.1.2</version> <executions> <execution> <goals> <goal>copy-dependencies</goal> <!-- Copy dependencies to the specified directory --> </goals> <configuration> <outputDirectory>${}/libs</outputDirectory> <!-- Depend on output directory --> </configuration> </execution> </executions> </plugin> </plugins> </build>
Common configuration items:
-
outputDirectory
: Specify the target directory to which the dependency is copied. -
excludeTransitive
: Whether to exclude transitive dependencies.
4. Customize the execution order of the plug-in
During the Maven build process, the order of execution of plugins is very important. passexecutions
Elements, we can control the execution timing of the plug-in.
Configuration example: Change the execution phase
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <executions> <execution> <phase>compile</phase> <!-- Execute in the compilation phase --> <goals> <goal>compile</goal> </goals> </execution> <execution> <phase>test</phase> <!-- Execute in the test phase --> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
-
phase
: Specify the life cycle phase of plug-in execution (such ascompile
、test
)。 -
goals
: Specify the specific goals that the plug-in needs to execute (such ascompile
、testCompile
)。
5. Summary
- The Maven plug-in provides many extension functions for building tasks. Through the <plugins> configuration in it, you can customize the execution of the plug-in.
- Common plug-ins such as maven-compiler-plugin, maven-jar-plugin, maven-surefire-plugin, and maven-dependent-plugin, can help complete compilation, packaging, testing, dependency management and other tasks.
- Custom configuration allows Maven to flexibly adapt to different needs in complex projects, and the executions element can control the timing and goals of the plug-in execution.
masterMaven plug-in configuration and customization, can help developers manage project construction and optimize construction processes more efficiently! 🚀
This is the article about the custom configuration method of Maven build plug-ins. For more related content on Maven build plug-ins, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!