Maven's build life cycle and commonly used plugins

Posted May 26, 20204 min read


Maven and gradle should be the two most used build tools among modern java programmers. Before they appeared, it was the world of ant.

Maven encapsulates a lot of useful operations in the build for us, we only need to execute a few simple mvn commands.

Today we will discuss the construction of the life cycle under the mvn command.

More exciting content and see:

For more information, please visit

lifecycle and Phases

The so-called lifecycle can be understood as a set of commands that can be executed to perform specific operations.

Maven has three lifecycles by default:default, clean and site. The default is mainly used to handle project development, clean is mainly used to clean up the project, and site is mainly used to generate project documentation.

Lifecycle is composed of one or more phases.

Take default as an example, it is composed of 23 phases, these phases will be executed in order to complete the default lifecycle.

We select a few common phases in the default lifecycle to illustrate:

  • validate-used to verify whether the project is correct or the information required by the project is available.
  • compile-used to compile project code
  • test-execute unit tests in code
  • package-package the compiled code, there are many ways to package, such as:jar, war, etc.
  • verify-perform integration tests
  • install-Install the project into a local warehouse for use by other projects with dependencies
  • deploy-Deploy the project to a remote repository for sharing with other users

The above phase execution is in order. For example, if we execute mvn verify, it will execute validate, compile, test and package in sequence.

Phases and goals

Phases is a collection of tasks, it is composed of one or more Goals. Goals can be included in Phases to execute, or can be executed with commands alone.

So where did Goals come from? Goals are defined in the plugin in maven.

Let's look at the following intuitive picture:

The following figure lists the phases in the existing lifecycle and the corresponding plugins for the corresponding phases.

We can see that each phase basically corresponds to the golas in a plugin.

In addition to using naming to directly specify the phase to be executed, you can also directly specify goals:

mvn clean dependency:copy-dependencies package

In the above command, clean and package are phase, and copy-dependencies are goals.

Introduction of commonly used plugins

Here we introduce two very commonly used maven plugins, maven-dependency-plugin and maven-jar-plugin.


The dependent jar packages in maven are stored in maven's local warehouse. If some jar packages are dependent on the project, it is necessary to copy these dependent jar packages during deployment. plugin, you can borrow its copy-dependencies to copy the project's dependent jar packages, as follows:

            <groupId> org.apache.maven.plugins </groupId>
            <artifactId> maven-dependency-plugin </artifactId>
            <version> 3.1.2 </version>
                    <id> copy </id>
                    <phase> package </phase>
                        <goal> copy-dependencies </goal>

Goals are associated with the corresponding phase. In the above example, we associated copy-dependencies with the package. When we execute the mvn package, copy-dependencies will be automatically executed. From the configuration file, we can know that we will The dependent jar packages of the project will be copied to the lib directory of the project's build directory.


With the dependent lib, you can package the main program into an executable jar package. At this time we need to use maven-jar-plugin.

            <groupId> org.apache.maven.plugins </groupId>
            <artifactId> maven-jar-plugin </artifactId>
            <version> 3.2.0 </version>
                        <mainClass> com.flydean.MavenClass </mainClass>

In order to generate an executable jar package, we need to add the path of the mainClass file in the MANIFEST.MF file, so that when the jar package is executed, no additional parameters can be run.

Unfortunately, if our class file uses the dependency of an external jar package, the jar package will run directly because there will be an error because the dependent jar package cannot be found.

When introducing maven-dependency-plugin, we have copied out the used lib, here we can directly use:

            <groupId> org.apache.maven.plugins </groupId>
            <artifactId> maven-jar-plugin </artifactId>
            <version> 3.2.0 </version>
                        <addClasspath> true </addClasspath>
                        <classpathPrefix> lib/</classpathPrefix>
                        <mainClass> com.flydean.MavenClass </mainClass>

Added two more parameters of addClasspath, we will unzip the packaged jar package.

You can see that there is an additional MANIFEST.MF file:

Created-By:Maven Jar Plugin 3.2.0
Class-Path:lib/lombok-1.18.10.jar lib/logback-classic-1.2.3.jar lib/log
 back-core-1.2.3.jar lib/slf4j-api-1.7.25.jar

This file contains some metadata of the jar package, and Class-Path and Main-Class files are added to it. At this time, the jar package can be executed directly.

to sum up

This article introduces the life cycle of maven build, and introduces two plugins that are often used.

Example of this article \ [
learn-java-base-9-to-20 ]([] ( 20))

Author:flydean program those things

Link to this article:

Source of this article:flydean's blog

Welcome to pay attention to my public number:those things of the program, more exciting waiting for you!