Maven’s build lifecycle and common plugins

brief introduction

Maven and gradle should be the two building tools most used by modern Java programmers. Before they appeared, it was ant's world.

Maven encapsulates many very useful operations in construction for us. We only need to execute a few simple MVN commands.

Today we're going to talk about building the lifecycle under the MVN command.

More highlights:

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 life cycles by default: default, clean and site. Default is mainly used for project development, clean is mainly used for project cleaning, and site is mainly used to generate project documents.

A lifecycle consists of one or more phases.

Take default as an example. It consists of about 23 phases, which will be executed in order to complete the default lifecycle.

Let's select several very common phases in the default lifecycle to illustrate:

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

Phases and goals

Phases is a collection of tasks, which is composed of one or more goals. Goals can be executed in phases or by commands alone.

So where did goals come from? Goals is defined in the plugin in Maven.

Let's look at the following intuitive diagram:

The following figure lists the phase in the existing lifecycle and the plugin corresponding to the corresponding phase.

We can see that basically every phase 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, while copy dependencies are goals.

Introduction to common plugins

Here we introduce two very common 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 the project depends on some jar packages, it is very inconvenient to copy these dependent jar packages during deployment. With Maven dependency plugin, you can borrow its copy dependencies to copy the dependent jar packages of the project, as shown below:

 <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 is associated with the corresponding phase. In the above example, we associate copy dependencies with packages, and copy dependencies will be automatically executed when we execute MVN package. From the configuration file, we can know that we will copy the dependent jar package of the project to the Lib directory of the project's build directory.

maven-jar-plugin

With the dependent lib, the main program can be packaged 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 executable jar packages, we need to create them in manifest Add the path of mainclass file to MF file, so that it can run without additional parameters when executing jar package.

Unfortunately, if our class file depends on an external jar package, an error will occur when the jar package is run directly, because the dependent jar package cannot be found.

When introducing Maven dependency plugin, we have copied the Lib used. Here we can use it directly:

<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>

Add two additional addclasspath parameters, and we will decompress the packaged jar package.

You can see that there is an additional manifest MF files:

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. At this time, the jar package can be executed directly.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>