(12) Maven life cycle and plugins

Source: Internet
Author: User

In addition to coordinates, dependencies, and warehouses, two other core concepts of MAVEN are the lifecycle and plug-ins. In the daily use of MAVEN, the input to the command line often corresponds to the lifecycle, such as the MVN package, which represents the implementation of the default life cycle phase of the package. The life cycle of MAVEN is abstract and its actual behavior is done by plug-ins, such as the package phase of a task that may be completed by Maven-jar-plugin. The life cycle and plug-in work together, inseparable.

1.Maven life cycle

As we develop our projects, we continue to experience the process of compiling, testing, packaging, and deploying, and the life cycle of Maven is an abstraction and unification of all of these processes, and her life cycle includes project cleanup, initialization, compilation, testing, packaging, integration testing, validation, deployment, Almost all of the process, such as site generation, and the life cycle of maven is flexible, each phase of her life cycle is implemented via plugins, and Maven has a lot of plugins built in, so we don't feel the process of compiling, testing, and packaging the project. Like compilation is implemented through Maven-compile-plugin, the test is implemented through Maven-surefire-plugin.

MAVEN has three sets of independent life cycles, please note that this is said to be "three sets", and "mutual independence", beginners easily to the Maven life cycle as a whole, not actually. These three sets of life cycles are:

    • Clean Lifecycle do some cleanup before you make a real build.
    • Default Lifecycle the core part of the build, compile, test, package, deploy and so on.
    • Site Lifecycle generate project reports, sites, publishing sites.

Again, I emphasize that they are independent, you can simply call clean to clear the working directory, just call site to build the site. Of course you can also run mvn clean install site to run all of these three sets of lifecycles directly.

Knowing the approximate use and correlation of each set of life cycles, to look at each life cycle in detail, clean and Site are relatively simple, first explain:

Each set of lifecycles consists of a set of stages (Phase) , and the commands we normally enter at the command line always correspond to a specific stage. For example, running mvnclean, this clean is a phase of the clean life cycle. A little bit around? Know that there is a clean life cycle and There is a clean phase.

The Clean life cycle consists of three phases:

    • Pre-clean perform some of the required Clean work that was done before.
    • Clean removes all the last build-generated files.
    • Post-clean perform some of the required Clean immediately after the completion of the work.

The clean in mvn Clean is the clean in the above, and in one life cycle, all phases before it runs, that is,mvn clean Equivalent to mvn pre-cleanClean, if we run mvn post-clean, then Pre-clean, clean will be run. This is a very important rule for Maven , which can greatly simplify the input of the command line.

Here's a look at each phase of the Site lifecycle:

    • Pre-site perform some work that needs to be done before the site documentation is generated.
    • site builds the site documentation for the project.
    • Post-site perform some work that needs to be done after the site document is built, and prepare for deployment.
    • Site-deploy deploy the generated site document to a specific server.

The site phase and the Site-deploy phase are often used here to generate and publish maven sites, which is a pretty powerful feature of maven , The Manager prefers that documents and statistics are generated automatically and look good.

Finally, take a look at Maven 's most important Default life cycle, where most of the work takes place in this life cycle, where I only explain some of the more important and common phases:

  • Validate
  • Initialize
  • Generate-sources
  • Process-sources processes the Project master resource file. In general, the contents of the Src/main/resources directory are copied to the main classpath directory of the project output after the variable substitution work.
  • Generate-resources
  • Process-resources
  • Compile Compile the source code for the project. In general, it is the compilation of the Java file in the Src/main/java directory into the main classpath directory of the project output.
  • Process-classes
  • Generate-test-sources
  • Process-test-sources Process the project test resource file. In general, after working on variable substitution for the contents of the Src/test/resources directory, copy to the test classpath directory of the project output.
  • Generate-test-resources
  • Process-test-resources
  • Test-compile Compile the test source code for the project. In general, it is the test classpath directory that compiles the Java files in the Src/test/java directory to the project output.
  • Process-test-classes
  • Test run the tests using the appropriate unit test framework. These test codes are not packaged or deployed.
  • Prepare-package
  • Package accept the compiled code and package it in a ready-to-publish format, such as JAR .
  • Pre-integration-test
  • Integration-test
  • Post-integration-test
  • Verify
  • Install Install the package to the local warehouse for other projects to rely on.
  • Deploy Copy the final package to a remote repository for other developers to use with MAVEN projects.

Basically, by name we can guess the purpose of each stage, the detailed explanation of the stage and the explanation of her stage, please refer to http://maven.apache.org/guides/introduction/ Introduction-to-the-lifecycle.html.

Remember, when you run any stage, all the stages in front of it will be run, which is why when we run mvn install , the code is compiled, tested, packaged.

In addition,maven 's plug-in mechanism relies entirely on the life cycle of maven , so understanding the lifecycle is critical, and I'll explain the maven plug-in mechanism further.

2. Command line and life cycle

The most important way to perform a MAVEN task from the command line is to invoke the MAVEN lifecycle phase. It is important to note that each life cycle is independent of each other and that the phase of a life cycle has a dependency. The following is an example of some common MAVEN commands that explain the lifecycle stages of their execution:

mvn Clean: This command invokes the clean phase of the clean life cycle. The actual execution phase is the Pre-clean and clean phase of the clean life cycle.

MVN test: This command invokes the test phase of the default life cycle. The actual execution phase is validate, initialize, and so on for the default life cycle, until all stages of test. This also explains why the code of the project can be compiled automatically when the test is executed.

mvn clean Install: This command invokes the clean phase of the clean life cycle and the install phase of the default life cycle. The actual execution phase is the Pre-clean, clean phase of the clean life cycle, and all phases of the default lifecycle from validate to install. This command combines two lifecycles, and it is a good practice to clean up the project before executing the project being built.

mvn Clean Deploy Site-deploy: This command invokes the clean phase of the clean life cycle, the Deploy phase of the default life cycle, and the site-deploy phase of the site life cycle. The actual execution phase is the Pre-clean, clean phase of the clean life cycle, all phases of the default life cycle, and all phases of the site life cycle. This command combines all of Maven's three lifecycles, and the deploy is the last phase of the default life cycle, Site-deploy the last phase of the site life cycle.

Since there are not many major life cycle phases in Maven, and the common MAVEN commands are actually based on a simple combination of these phases, the reader can use MAVEN commands correctly and skillfully as long as there is a basic understanding of the Maven life cycle.

3.Maven plug-in mechanism

through the life cycle above we can learn that different life cycles bind different plugins, and we know that the downloaded maven the core thing but 3-4m , it is mainly through the plug-in to complete the work, once encountered the plug-in, it will go to the appropriate place to download, and then to complete the process. So how do we use plugins in our projects?

Open http://maven.apache.org/plugins/index.html URLs that we can see Apache here are a lot of plugins, Apache The following plug-in is more formal, the information inside it is very detailed. Let's take a look at the usage of a plugin with source in it.

Source a plug-in is a plug-in that packages the source code, which, by default, places the generated source in the project catalog's Target below.

Source plug-ins have five goals :

    • source:aggregate aggregrates sources for all modules In an aggregator project.
    • source:jar is used to bundle the main sources of the project into a jar Archive.
    • source:test-jar on The other hand, was used to bundle the test sources Of the project into a jar archive.
    • source:jar-no-fork is similar to  jar  but does not fork the build lifecycle.
    • source:test-jar-no-fork is similar to  Test-jar  but does not fork the build lifecycle.

In our engineering Pom.xml, the following configuration is introduced later:

1     <Build>    2         <Plugins>3             <plugin>4                 <groupId>Org.apache.maven.plugins</groupId>5                 <Artifactid>Maven-source-plugin</Artifactid>6                 <version>2.1.2</version>7             </plugin>8         </Plugins>9     </Build>

The above configuration is to package the source code plug-in, we run source:jar-no-fork , then in the item under the destination directory Target will generate a similar to User-core-0.0.1-snapshot-sources.jar such a file, that is, the project's source file. So how do you bind This plugin to a specific life cycle? Let's look at the following configuration:

1     <Build>    2         <Plugins>3             <plugin>4                 <groupId>Org.apache.maven.plugins</groupId>5                 <Artifactid>Maven-source-plugin</Artifactid>6                 <version>2.1.2</version>7                 <executions>8                     <Execution>9                         <Phase>Package</Phase>Ten                         <Goals> One                             <goal>Jar-no-fork</goal> A                         </Goals> -                     </Execution> -                 </executions> the             </plugin> -         </Plugins> -     </Build>

With This configuration, you can use MVN Package Packaging The project will also make the source code package. Diagram Description:

Apache Maven There are a lot of useful plug-ins, you can try it on their own, the inside instructions are very detailed, as long as the official documents to configure, the general situation is no problem, if you want to use the plug-in with a very skilled advice to ask the big God, or buy "maven combat" books, In the book, the plugin will be introduced in more detail, and then combined with the experience of the great God, I believe you can skillfully use the MAVEN plugin mechanism.

Concluding remarks: The way to go today, you have to remember in mind, no matter how far between you and the goal, but also learn to walk easily. Only in this way, in the process of moving towards the goal, will not feel bored, will not be intimidated by the distant future.

(12) Maven life cycle and plugins

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.