Maven Combat: Maven life cycle

Source: Internet
Author: User

Objective

Before I wrote an article on Maven, I introduced some of the basic concepts of MAVEN, and the basic knowledge of Maven for a beginner, and it seemed to me that mastering these basics was enough.

With the depth of work, more and more feel for Maven's understanding is not enough, many times the use of Maven out of the problem can not be solved quickly, so intends to go deep in the construction of MAVEN engineering began to learn about MAVEN, this article will be a record of their own learning process and friends to share.

Start with the simplest MAVEN project

LZ uses myeclipse, so it is to build a simple Maven project with MyEclipse. The first step, new, is a maven Project:

Click Next:

It is not recommended to tick "Create A simple project (skip archetype selection)" so that you can use many Maven archetype, the MAVEN prototype, and click Next:

Seeing a lot of the prototypes that Maven recommends to developers, "Org.apache.maven" begins with Maven's official recommendation, choosing a simple "maven-archetype-quickstart" and then filling in some basic information:

This information is more free to fill out, of course, after the group ID, Artifact ID, Version, package, and so on to explain, click Finish,myeclipse to give us a MAVEN project generated:

This is the standard directory structure generated by the "Maven-archetype-quickstart" prototype, "Src/main/java" is used to write Java code, "Src/test/java" is used to write test classes and see the most important pom.xml:

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance  " xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >  < modelversion>4.0.0</modelversion>  <groupId>org.xrq.mvnstart</groupId>  < artifactid>hello-world</artifactid>  <version>0.0.1-SNAPSHOT</version>  < packaging>jar</packaging>  <name>hello-world</name>  <url>http:// maven.apache.org</url>  <properties>    <project.build.sourceencoding>utf-8</ project.build.sourceencoding>  </properties>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version >4.10</version>      <scope>test</scope>    </dependency>  </dependencies ></project>

See the "Maven-archetype-quickstart" prototype for us to import a junit jar package for unit testing. Note that the default generation of the JUnit version is 3.8.1, this version of JUnit does not support annotations , and subsequent unit tests are annotated, so change this to a 4.10 version.

Another important point is that the final packaged file type is a jar package (packaging).

OK, then we'll start writing Java code and testing the code for the class.

Write Master code and compile

Unlike the project master code and the test code, the main code of the project is packaged into the final artifact, and the test code is used only when the test is run and is not packaged.

Under the generated project, there is a default app class that changes slightly:

public class App {public    String SayHelloWorld ()    {        return "Say Hello world!";    }        public static void Main (string[] args)    {        System.out.println (new App (). SayHelloWorld ());}    }

After the code is written, and then compiled, because MAVEN uses commands to operate, so go to the console, enter the project directory, enter "MVN Clean compile":

At this point the engineering change is that there are compiled App.java corresponding app.class under target:

The meaning of this command is:

1. Clean tells Maven to clear the output directory target/

2. Compile tells Maven to compile the project master code

From the output you can see that MAVEN performs the Clean:clean task first, deletes the target/directory, and then executes the resources:resources task (because the project resource is undefined, so this item is skipped), and finally executes compiler: Compile tasks, compile the project master code into the Target/classes directory.

To say the point, Clean:clean, resources:resources, compiler:compile correspond to some MAVEN plug-ins and plug-in targets, such as Clean:clean is the clean plugin clean target, compiler: Compile is the compile target of the compiler plug-in, which is described later in this article.

Writing test code and compiling

To ensure that the project structure is clear, the main code and the test code should be in separate directories. The default main code directory in the MAVEN project is Src/main/java, and the default test code directory in the MAVEN project is Src/test/java, so if you don't have the directory before you write the test case, you should first create it.

The Test code is:

public class Apptest {    @Test public    void Testsayhelloworld ()    {        app app = new app ();        String result = App.sayhelloworld ();        Assert.assertequals (Result, "Say Hello world!");}    }

With JUnit, the pom.xml dependency in the above, JUnit's scope is test, which means that JUnit's package is only valid for the test directory, in other words, using JUnit in the home directory will cause compilation errors.

There is one more question to be specific about, for historical reasons, one of MAVEN's core plugins compiler plug-ins supports only compiled Java1.3, and this version does not support JUnit, so you need to configure the plug-in so that it supports JAVA5 to use JUnit. So add a section to the Pom.xml:

<build>      <plugins>          <plugin>              <groupid>org.apache.maven.plugins</groupid >              <artifactId>maven-compiler-plugin</artifactId>              <configuration>                  <source >1.5</source>                  <target>1.5</target>              </configuration>          </plugin>      </plugins>  </build>

This allows the plugin to support JAVA5 compilation. At this point, enter MVN clean test on the command line:

The test passed, no problem.

Packaged

After the project is compiled and tested, an action is made to package.

Similarly, using the command line operation, enter "MVN clean package", before packaging will be compiled, tested, this information will not be intercepted, only look at the packaged parts:

The name of the package is in accordance with artifactid+version, and can be customized if necessary.

After packing, look at the project catalog:

One more jar file, so you can use the jar file somewhere else.

Installation

The jar files generated above can be copied directly to other projects under the classpath, so how do you get other projects to use the jar package directly? You can use the install.

Enter "mvn clean install" on the command line, and I'll just intercept the key parts:

See that the generated jar and POM are installed in the local repository and can be used by other MAVEN projects only after the build is downloaded to the local repository . The same is true for other MAVEN projects to be able to use it only after the components of Hello World are installed in the local repository.

Maven Life cycle

The above content contains Mavan's most important commands: mvn clean compile, mvn clean test, MVN clean package, and mvn clean install. Before executing the test, execute compile, execute the package before executing the package, execute the package before executing the package, which involves a concept:maven life cycle .

There are three sets of independent lifecycles in Maven:

    • Clean Lifecycle: Do some cleanup before you make a real build
    • Default Lifecycle: The core part of the build, compile, test, package, deploy
    • Site Lifecycle: Generate project reports, build sites, publish sites

The first and third are simpler and better understood, look at Maven's most important default life cycle, where most of the work takes place in this life cycle, where the more important and common phases are:

    • Validate
    • Generate-sources
    • Process-sources
    • Generate-resources
    • Process-resources: Copy and process the resource file to the target directory, ready to package
    • Compile: Compiling project source code
    • Process-clases
    • Generate-test-sources
    • Procss-test-sources
    • Generate-test-resources
    • Process-test-resources: Copy and process the resource file to the target test directory
    • Test-compile: Compiling test source code
    • Process-test-classes
    • Test: Run with the appropriate unit test framework, these test code will not be packaged or deployed
    • Prepare-package
    • Package: Accepts compiled code and packages it into a published format, such as a jar
    • Pre-integration-test
    • Integration-test
    • Post-integration-test
    • Verify
    • Install: Installs the package to the local warehouse so that other projects depend on
    • Deploy: Copy the final package to the remote repository for other developers to share with the project

Basically, we can guess the purpose of each stage as long as it is by name. Keep in mind that at any one stage, all the stages in front of it will be run , which is why when we run MVN clean install, the code is compiled, tested, packaged.

Furthermore, MAVEN's plug-in mechanism relies entirely on the MAVEN lifecycle, so understanding the lifecycle is critical.

Maven Combat: Maven life cycle

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.