Chapter 6 Maven Core Concepts - Lifecycle and Plugins

What is the life cycle?

The life cycle, to put it bluntly, is a series of processes from birth to death. I believe that people who have done web development are no strangers to the life cycle of servlets. In the same way, the life cycle of a person can basically be divided into quack landing, kindergarten, primary school, junior high school, high school, college, work, retirement, and finally hanging on the wall.

maven life cycle

The human life cycle abstracts the human life, and similarly, the maven life cycle abstracts and unifies the construction process. Maven's life cycle includes project cleanup, initialization, compilation, testing, packaging, integration testing, verification, deployment, and site generation.

Maven abstracts three sets of life cycles, which can be understood as dividing the construction process of the project into a class, each of which is a small life cycle



 Maven abstracts three life cycles, clean, default, and site. Each life cycle contains multiple tasks, and a task becomes a stage.

clean: project cleanup

default: contains a series of key build actions

site: project site report related

If we want to perform a certain build task, we can use these life cycle stages. For example, in the command line, executing mvn clean corresponds to the clean stage of the clean life cycle, and mvn install corresponds to the install stage of the default life cycle. Similarly , the same goes for running maven commands in the IDE.

 

The life cycles are independent of each other, the life cycle stages are in order, and the later stages depend on the previous ones

The life cycles are independent of each other, which means that there is no relationship between different life cycles. For example, executing the clean phase of the clean life cycle will not affect the phases of the default life cycle; and within the same life cycle, there is an order between the life cycle phases. Yes, the above figure lists the order of important life cycles, from top to bottom, for example, when executing mvn install, in the default life cycle, the stages before install will be executed in sequence, and maven will first execute validate, .. .compile..., until install.

Stages of different life cycles can be used in combination. For example, before testing or installation, it is generally necessary to clean up the output of the last build. Then you can combine clean and test or package. For example, mvn clean test, maven will execute the corresponding stage of the clean life cycle first. , all stages of the default life cycle from the beginning to the test stage.

 

plugin

The concept of plug-ins is familiar to everyone. For example, eclipse has many plug-ins. You can add the corresponding plug-ins for any function you need. Maven is also implemented based on the plugin mechanism. In fact, the tasks completed in different stages of the life cycle described earlier are all completed by the corresponding plug-ins. The life cycle itself cannot work, and the specific tasks are completed by the corresponding plug-ins .

A plugin will provide multiple functions, and a function is a goal.

We will find that after installing maven, we have basically done nothing, and we can already execute the corresponding build tasks through the commands of mvn clean, mvn test, and mvn install. We also said that these tasks are performed by the corresponding plugins Done, but we didn't configure these plugins, so how could it work? This is because maven has built-in configuration of common plug-ins by default, which is the embodiment of maven convention over configuration. Commonly used plugins built in maven are as follows:


 

 

The middle column of the above figure lists the plugins and their corresponding goals. A plugin may have multiple goals, and each goal completes the corresponding task. For example, when we compile the project, the compilation task is completed by maven-compile-plugin. It has two goals, compile and testCompile, for compiling main code and test code respectively.

As mentioned earlier, the life cycle itself is abstract, just like an interface, it cannot work by itself, but why do we execute it? We execute the corresponding stages of the life cycle, such as mvn clean install, clean and install are the clean life cycle and The stages of the default life cycle, they are the life cycle, they are abstract, they should not work, why can maven do the corresponding things when entering the life cycle stage here? This is because maven has bound these life cycle stages to the corresponding plugin goals by default. The first column in the above figure is the corresponding life cycle stage, and the second column is the plugin goal corresponding to the corresponding life cycle stage. Therefore, we are in When entering the life cycle stage, maven will find the target of the corresponding plugin to execute. Therefore, the effect of entering mvn clean and mvn maven-clean-plugin:clean on the command line is the same.

 

Maven has built-in common plug-ins by default to complete basic building tasks, but these plug-ins are far from enough. Maven provides a large number of plug-ins for us to use. We can configure the corresponding plug-ins in the pom file:

Configure plugins under the sub-element <plugins> of the <build> configuration element in the pom


 A plugin element represents a plug-in, the plug-in is also the same as the GAV flag, and the execution represents a binding, which binds the stage of the life cycle to a goal of the plug-in, so that when the life cycle stage is entered on the command line, the corresponding plug-in will be executed. target and complete the corresponding task.

 

Plugin repository

Like the component warehouse, we introduced the warehouse configuration in Chapter 5, which is only valid for ordinary jar packages. The plug-in has an independent warehouse, and the configuration method is similar to that of the jar package warehouse.


 It's just that the configuration element is changed to pluginRepository

 

Maven provides many plug-ins, which can be viewed on the maven official website at the following address

http://maven.apache.org/plugins/index.html

So far, we understand the meaning behind those maven commands, we can use the lifecycle phases to perform the corresponding build tasks, but these lifecycle phases don't work natively, maven binds these lifecycle phases to the corresponding plugin's Goals, when executing these life cycle stages, the goals of the corresponding plugins are actually executed to complete the corresponding tasks.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326807735&siteId=291194637