Skip to content
On this page

Using Plugins

We introduced Maven's lifecycle, phases, and goals earlier: using Maven to build a project involves executing a lifecycle up to a specified phase. Each phase executes one or more default goals. A goal is the smallest unit of work.

Let's take the compile phase as an example. If you execute:

bash
$ mvn compile

Maven will execute the compile phase, which will invoke the compiler plugin to execute the associated compiler:compile goal.

In fact, each phase is executed through a specific plugin. Maven itself does not know how to perform the compile; it is only responsible for finding the corresponding compiler plugin and then executing the default compiler:compile goal to complete the compilation.

Therefore, using Maven essentially involves configuring the required plugins and then invoking them through phases.

Maven has built-in some commonly used standard plugins:

Plugin NameCorresponding Phase
cleanclean
compilercompile
surefiretest
jarpackage

If standard plugins do not meet the requirements, we can also use custom plugins. When using a custom plugin, it needs to be declared. For example, using the maven-shade-plugin can create an executable jar. To use this plugin, it needs to be declared in the pom.xml:

xml
<project>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            ...plugin configuration...
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Custom plugins often require some configuration. For example, the maven-shade-plugin needs to specify the entry point of the Java program. Its configuration is:

xml
<configuration>
    <transformers>
        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>com.itranswarp.learnjava.Main</mainClass>
        </transformer>
    </transformers>
</configuration>

Note that standard plugins provided by Maven, such as compiler, do not need to be declared. Only when introducing other plugins do they need to be declared.

Below are some commonly used plugins:

  • maven-shade-plugin: Packages all dependencies and generates an executable jar;
  • cobertura-maven-plugin: Generates unit test coverage reports;
  • findbugs-maven-plugin: Performs static analysis on Java source code to identify potential issues.

Exercise

Create an executable jar using the maven-shade-plugin.

Summary

Maven can perform additional functionalities required during project build through custom plugins. Using custom plugins requires declaring the plugins and configurations in the pom.xml;

Plugins are executed when a specific phase is invoked;

The configuration and usage of plugins should refer to the official documentation of the plugins.

Using Plugins has loaded