Maven's build life cycle and commonly used plugins

Posted May 26, 20204 min read

Introduction

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 www.flydean.com

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.

maven-dependency-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:

 <plugin>
            <groupId> org.apache.maven.plugins </groupId>
            <artifactId> maven-dependency-plugin </artifactId>
            <version> 3.1.2 </version>
            <executions>
                <execution>
                    <id> copy </id>
                    <phase> package </phase>
                    <goals>
                        <goal> copy-dependencies </goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/lib
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

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.

maven-jar-plugin

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.

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

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:

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

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

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

Manifest-Version:1.0
Created-By:Maven Jar Plugin 3.2.0
Build-Jdk-Spec:14
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
Main-Class:com.flydean.MavenClass

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 \ [ https://github.com/ddean2009/
learn-java-base-9-to-20 ]([ https://github.com/ddean2009/...] ( https://github.com/ddean2009/learn-java-base-9-to- 20))

Author:flydean program those things

Link to this article: http://www.flydean.com/apache-maven-lifecycle/

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!