maven lifecycle

An important reason why Maven is powerful is that it has a very complete life cycle model (lifecycle),

This life cycle can be understood from two aspects,

1. As the name suggests, each step of running Maven is defined by it. This predefined default behavior makes it easy for us to use Maven. In contrast, each step of Ant requires you to define it manually.

2. This model is a standard. In different projects, the interface for using Maven is the same, so that you don't need to carefully understand the construction of each project. In general, commands such as mvn clean install are common.

I think, it must have absorbed the experience of many projects, Maven can define such a perfect model.

 

Maven has three sets of independent life cycles. Please note that the "three sets" mentioned here are "independent of each other". It is easy for beginners to regard the life cycle of Maven as a whole, but it is not. The three life cycles are:

Clean Lifecycle does some cleanup before doing the real build.

The core part of the Default Lifecycle build, compile, test, package, deploy, and more.

Site Lifecycle generates project reports, sites, publishing sites.

Again, they are independent of each other, you can just call clean to clean the working directory, and just call site to generate the site. Of course you can also run mvn clean install site directly to run all three life cycles.

 

After knowing the general purpose and relationship of each life cycle, let's take a detailed look at each life cycle one by one. Clean and Site are relatively simple, so let's explain them first.

 

Each life cycle consists of a set of phases (Phase), and the commands we usually enter on the command line always correspond to a specific phase. For example, run mvn clean , which is a phase of the Clean lifecycle. A little round? To know that there is a Clean life cycle, there is also a clean phase. The Clean life cycle consists of three phases:

pre-clean  performs some work that needs to be done before clean

clean  removes all files generated by the last build

post-clean  performs some work that needs to be done immediately after clean

The clean in mvn clean is the above clean. In a life cycle, when running a certain stage, all stages before it will be run, that is, mvn clean is equivalent to mvn pre-clean clean If we run mvn post-clean , then pre-clean , clean will be run. This is a very important rule of Maven, which can greatly simplify the input of the command line.

 

Let's take a look at the various stages of the Site life cycle:

pre-site     performs some work that needs to be done before generating site documentation

site    generates the site documentation for the project

post-site     performs some work that needs to be done after generating the site documentation and prepares it for deployment

site-deploy     deploys the generated site documentation to a specific server

The site stage and site-deploy stage are often used here to generate and publish Maven sites. This is a very powerful function of Maven. Manager prefers it, and documents and statistics are automatically generated, which is very nice.

 

Finally, let's take a look at the most important Default life cycle of Maven. Most of the work occurs in this life cycle. Here, I will only explain some of the more important and commonly used stages:

validate

generate-sources

process-sources

generate-resources

process-resources     copies and processes resource files to the target directory, ready for packaging.

compile     compiles the source code of the project.

process-classes

generate-test-sources

process-test-sources

generate-test-resources

process-test-resources     copies and processes resource files to the target test directory.

test-compile     compiles the test source code.

process-test-classes

test     runs the tests using a suitable unit testing framework. These test code will not be packaged or deployed.

prepare-package

package     accepts compiled code, packaged into a distributable format, such as a JAR.

pre-integration-test

integration-test

post-integration-test

verify

install      installs the package to the local repository for other projects to depend on.

deploy      copies the final package to a remote repository for other developers to share with the project.

Basically, based on the name we can guess the purpose of each stage, the explanation about the other stages,

请参考 http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

 

记住,运行任何一个阶段的时候,它前面的所有阶段都会被运行,这也就是为什么我们运行mvn install 的时候,代码会被编译,测试,打包。

 

此外,Maven的插件机制是完全依赖Maven的生命周期的,因此理解生命周期至关重要,在之后的文章里,我将会进一步解释Maven的插件机制。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326217136&siteId=291194637