Maven Learning (1): Creating MAVEN projects and understanding basic principles

Source: Internet
Author: User
Tags object model
1. What is maven:

Maven first is a powerful Java build tool, and of course there are other tools. But Maven is a better quality. And Maven itself is also developed in Java. Understanding how MAVEN works, and the idea of Maven's software, is also good for Java learning.
Of course we've all built tools, like the smartide I'm using now. The main function of the build tool is to compile the code. MAVEN does not just do the job, it can also package, generate project documentation, execute tests, and install packaged code onto the server. The whole process of building a project so that you can automate with Maven, the benefits of automation are self-evident, reducing human error costs and increasing efficiency. One of the best things about Maven is dependency management.
The installation of MAVEN documents on the Internet, mainly to download the direct decompression can be configured environment variables, in the Windows interrupt input mvn-v has a back up OK. Then install the Eclipse MAVEN plug-in, configure the Basic object designator (Groupid,artichid,type) and compile, and note that Maven needs to compile with JDK, so the default is to modify Eclipse's JRE path to JDK.
There are several ways to create a MAVEN project, using the command line, manually creating, Ecelipse. Suggestions are tried, the overall grasp will be better. 2. We understand the core concept of maven: POM

First of all, this pom refers to the project object model, not the Pom.xml in the engineering directory, but there is still a connection between them. Model the project that will be built. As an object, the coordinates of this object (PO) are made up of the 5 parts of Groupid,artifactid,version,classifier,type. Another aspect a project certainly cannot be composed of an object, so this PO should have depecdencies this attribute to represent the external project it has been in.

So the properties of this PO can be described in Java code
Class po{
Private String groupId;
Private String Artifactid;
Private String version;
Private String classifier;
Private String type;
Private set<po> dependencies;
}
OK, try to describe it with an XML file through the structure above,

<PO>
    <groupId></groupId>
    <artifactId></artifactId>
    <version> </version>
    <classifier><classifier>
    <type></type>
    <dependencies >
        <PO></PO>
        <PO></PO> ...
    </dependencies>
</PO>

So, obviously, this is pom.xml. So, Pom.xml is used to describe the PO. So when we see the pom.xml in the MAVEN project, it comes to mind that the XML file is used to describe the project to build.
What other attributes does this pom.xml have at the time of construction? There are build-related elements in the pom.xml. The construction of MAVEN must first understand the Lifecyle declaration cycle, build a project in Maven, correspond to a lifecyle, this lifecyle also distinguishes several stages, this stage is called phase, A standard build Lifecyle contains these phase

validate: Used to verify the validity of a project and what it needs for its project is initialize: initialization, such as creating a directory for the build needed.
Generate-sources: Used to generate some source code that needs to be used in compile phase to process-sources: doing something with the source code, such as filtering some source code
Generate-resources: Generate resource files (these files will be included in the final input file) Process-resources: processing resource Files compile: compiling source code
Process-classes: Processing of compiled generated files generate-test-sources: Generating source code for the test process-test-sources: processing the generated test sources
Generate-test-resources: Generating a Test resource file process-test-resources: Processing a test resource file Test-compile: Compiling the source code for the test     
Process-test-classes: Processing of a compiled file for test source tests test: Unit Test Prepare-package: Packaging Predecessors Package: Packaging Pre-integration-test: Integrated test predecessors Integration-test: Integrated test post-integration-test: Integrated test post Operations install: Install packaged products to a local MAVEN warehouse deploy: Install packaged products to a remote repository 

These phase are executed sequentially within each lifecycle, such as executing mvn install so deploy before phase are executed.
So how are these phase executed? This introduces the concept of goal, in fact, phase a bit like Java interface, but only for the implementation of the negotiation, but there is no further action, and goal is to define what action to perform. A goal is a mojo in maven. A Excute method is defined in this mojo, and the goal action is defined in this Excute method. And this Mojo class is in the Maven plugin. The so-called plugin is actually a MAVEN project, except that the project references some of MAVEN's APIs. Similarly, since he is a MAVEN project, of course there are maven coordinates.
So how did they relate to each other?
When performing concrete steps, we need to bind a goal for each phase of the Lifecyhle. This allows for a specific action to be performed at each step. For example, there is a compile phase in lifecycle that requires that the process of the build be compiled, and maven-compile-plugin this plugin has a compile Goal is the use of Javac to compile source files into class files. The binding place is the build element in the Pom.xml.

<build> <plugins> <plugin> <artifactid>maven-myquery-plugin</ artifactid> <version>1.0</version> <executions> <execution> <id>exe cution1</id> <phase>test</phase> <configuration> <URL>HTTP://WWW.F oo.com/query</url> <timeout>10</timeout> <options> <option>o ne</option> <option>two</option> <option>three</option> &L t;/options> </configuration> <goals> <goal>query</goal> </ goals> </execution> </executions> </plugin> </plugins> </build> 

This binds the Maven-myquery-plugin test (phase) to the Quey (goal)
Then, the compiler will need to compile the source file, but so far we have not seen pom.xml source files. How does maven find the source file?
There is a rule in Maven that conventions are better than configurations. Maven defaults from the root directory of the project to look for Java source files from the Src/mian/java directory. The compiled class is saved in the Target/classes directory. In Maven, all the PO has a root object, Super POM. All default configuration items are defined in the Super Pom.

Excerpt from: Http://www.jianshu.com/p/0fb5e3fb704d The original written in the very good. Manual point praise.
Life cycle of 3.Maven:
On the previous one we introduced the lifecycle of this build, and from the above description we can feel that the life cycle of MAVEN is abstract, that is, the lifecycle does not do any real work, the actual work is done by goal, and Maven has several other lifecycles:
1.clean Life cycle: Cleanup Project, three phase
2.default Lifecycle: Building the project, the important phase are as follows:
3.site Lifecycle: Build and publish Project site, phase as follows:
There are no sequential dependencies between these command cycles, each of which is independent of each other, and the phase of one lifecycle is dependent. relationship between the command line and the lifecycle: (Invoke make clean actual execution: Pre-clean and clean) (Invoke the Teset of the default lifecycle, and of course, all phases prior to teset)
The 3.make clean install invokes the clean phase of the clean lifecycle and the install phase of default, which actually executes Pre-clean and Clean,install as well as all previous phases.
Right-click the MAVEN project in eclipse to see some mvn commands.
Of course, you can configure the command Line Association, as the principle of this automatic association is the relationship between goal and phase. Create a MAVEN project

Okay, now that we know so much, let's create a MAVEN project.
After installing MAVEN eclipse.


3. Use the MVN command line to create a basic MVN project.

First you need to know the basic MVN project directory structure.

Write Pom.xml and put it under the SRC sibling directory.
Then mvn compile mvn test mvn install

At this point you can see that there is no particular requirement for this groupid. The only requirement is to make this project easier to manage without the need for compilation. Just make sure that other projects depend on the project and find it.
4. Two choices for creating a TOC

1.MVN archetype:generate
2.MVN archetype:generate-dgoupid====

Processing of project dependencies in Maven:

Aggregation using module Remember to modify packageing to Pom
Classpath corresponds to scope.
Three kinds of classpath, compile, run, test.
The value of the scope option determines which classpath to use
Compile,provided,test,import,system,runntime.

This article is very well written. http://blog.csdn.net/wanghantong/article/details/36427165

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.