SoFunction
Updated on 2025-05-13

Maven: Custom configuration method for building plug-ins

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

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;Plugin groupId&lt;/groupId&gt;
            &lt;artifactId&gt;Plugin artifactId&lt;/artifactId&gt;
            &lt;version&gt;Plugin版本&lt;/version&gt;
            &lt;configuration&gt;
                &lt;!-- Plugin-specific configuration --&gt;
            &lt;/configuration&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                    &lt;phase&gt;Target phase&lt;/phase&gt; &lt;!-- Specify the execution phase of the plugin during the build lifecycle --&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;Target1&lt;/goal&gt;
                        &lt;goal&gt;Target2&lt;/goal&gt;
                    &lt;/goals&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;
  • 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-pluginIt 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

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;&lt;/groupId&gt;
            &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.8.1&lt;/version&gt;
            &lt;configuration&gt;
                &lt;source&gt;1.8&lt;/source&gt; &lt;!-- Set the source code version --&gt;
                &lt;target&gt;1.8&lt;/target&gt; &lt;!-- Set the target compiled version --&gt;
                &lt;encoding&gt;UTF-8&lt;/encoding&gt; &lt;!-- Set the encoding method --&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

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-pluginUsed to package the project as a JAR file, you can configure the name of the JAR file, attached files, etc.

Configuration example

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;&lt;/groupId&gt;
            &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.2.0&lt;/version&gt;
            &lt;configuration&gt;
                &lt;archive&gt;
                    &lt;manifestEntries&gt;
                        &lt;Main-Class&gt;&lt;/Main-Class&gt; &lt;!-- set up JAR of Main kind --&gt;
                    &lt;/manifestEntries&gt;
                &lt;/archive&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;
  • 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-pluginUsed to perform unit tests. You can customize the test configuration, select different test frameworks, set up parallel testing, etc.

Configuration example

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;&lt;/groupId&gt;
            &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
            &lt;version&gt;2.22.2&lt;/version&gt;
            &lt;configuration&gt;
                &lt;includes&gt;
                    &lt;include&gt;**/*&lt;/include&gt; &lt;!-- Only execute Test The ending test class --&gt;
                &lt;/includes&gt;
                &lt;parallel&gt;methods&lt;/parallel&gt; &lt;!-- Enable parallel testing --&gt;
                &lt;threadCount&gt;4&lt;/threadCount&gt; &lt;!-- Set the number of threads for parallel tests --&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;
  • 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-pluginUsed to manage and view dependencies for projects, you can perform operations such as copying dependencies, viewing dependency trees, etc.

Configuration example

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;&lt;/groupId&gt;
            &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.1.2&lt;/version&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;copy-dependencies&lt;/goal&gt; &lt;!-- Copy dependencies to the specified directory --&gt;
                    &lt;/goals&gt;
                    &lt;configuration&gt;
                        &lt;outputDirectory&gt;${}/libs&lt;/outputDirectory&gt; &lt;!-- Depend on output directory --&gt;
                    &lt;/configuration&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

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. passexecutionsElements, we can control the execution timing of the plug-in.

Configuration example: Change the execution phase

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;&lt;/groupId&gt;
            &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.8.1&lt;/version&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                    &lt;phase&gt;compile&lt;/phase&gt; &lt;!-- Execute in the compilation phase --&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;compile&lt;/goal&gt;
                    &lt;/goals&gt;
                &lt;/execution&gt;
                &lt;execution&gt;
                    &lt;phase&gt;test&lt;/phase&gt; &lt;!-- Execute in the test phase --&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;testCompile&lt;/goal&gt;
                    &lt;/goals&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;
  • phase: Specify the life cycle phase of plug-in execution (such ascompiletest)。
  • goals: Specify the specific goals that the plug-in needs to execute (such ascompiletestCompile)。

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!