02. The first MAVEN application

Source: Internet
Author: User
Tags maven central

First MAVEN application preconditions
    • MAVEN is installed

Create a directory and write Maven's Pom.xml

Set up the project directory under any directory of the system and establish the Pom.xml file in the directory, modify the contents of the Pom.xml file as follows:

<?xml version= "1.0"  encoding= "UTF-8"? ><project xsi:schemalocation= "http// Maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "         xmlns= "http://maven.apache.org/POM/4.0.0"          Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" >        < Modelversion>4.0.0</modelversion>        <groupid> Org.test</groupid>        <artifactid>demo</artifactid >        <version>1.0-SNAPSHOT</version>         <properties>                 <project.build.sourceencoding>utf-8</ project.build.sourceencoding>     &Nbsp;  </properties></project> 

Note: The first behavior standard XML header

Third behavior XML constraints, for fixed notation, if you use a different version of Maven may have different

The model version of the POM project of the behavior maven is generally unchanged.

The seventh line, GroupId , is the organization ID, which is typically the organization that the project belongs to, and the path generated when the build occurs.

Line eighth Artifactid is the project name, which uniquely identifies a component (project) together with the group ID.

The Nineth line version is the project revision number, the current version of the project, in the format: Major. minor version. Incremental version-limited version number

The 11th row of the properties node and the project.build.sourceEncoding in it define the encoding format of the project source code

Create source code and test code catalog

Create the following directory structure in the project directory:

basedir    └─src        ├─main         │  └─java        │       └─org        │           └─test        │               └─main         └─test            └─java                 └─org                     └─test                         &nBsp;└─main 
Build source code and test code

New file: Src\main\java\org\test\main\hellomaven.java, the content is as follows:

Package Org.test.main;public class hellomaven{public static void Main (string[] args) {System.out.println ("Hello    Maven ");    public string Say (string name) {return "Hello" +name; }}

New file: Src\test\java\org\test\main\testhellomaven.java, the content is as follows:

Package Org.test.main;import org.junit.*;import Static org.junit.assert.*;p ublic class testhellomaven{@Test public        void Testhellomaven () {Hellomaven HM = new Hellomaven ();        String result = Hm.say ("Maven");    Assertequals (Result, "Hello Maven"); }}

Open a terminal, switch to the project directory, and execute the following command:

MVN Compile

At this point Maven downloads the project package to be relied upon from the central repository, to the user directory under the. M2 directory (this directory is configurable), and after completion, compiles the source code file and outputs the result, if the result is as follows, the compilation succeeds:

[INFO]---maven-compiler-plugin:3.1:compile (default-compile) @ demo---[INFO] changes detected-recompiling the module! [INFO] Compiling 1 source file to E:\maven\demo\target\classes[info]------------------------------------------------------ ------------------[INFO] BUILD Success[info]-------------------------------------------------------------------- ----[INFO] Total time:3.429 s[info] finished at:2015-10-20t15:23:50+08:00[info] Final Memory:16m/110m[info]---------- --------------------------------------------------------------

At this point, the directory structure is as follows:

├─src│  ├─main│  │  └─java│  │       └─org│  │          └─test│  │               └─main│  └─test│       └─java│          └─org│               └─test│                   └─main└─target     ├─classes    │  └─org    │       └─test    │           └─main    └─maven-status         └─maven-compiler-plugin            └─compile                 └─default-compile

The target directory is the compiled output directory.


Modify the Pom file and add the following under the project node:

<dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit</ Artifactid> <version>4.12</version> </dependency></dependencies>

This node adds a dependency of the project on the JUnit package, where groupId, Artifactid, and version are the coordinates of JUnit in the MAVEN Central warehouse (uniquely identified).

After the modification is complete, execute the following command in the terminal:

MVN test

At this point, MAVEN downloads the package associated with the test, as well as the dependent JUnit package to the. M2 folder of the user directory, and then completes the test. The following display indicates that the command executed successfully:

-------------------------------------------------------T E S t S-------------------------------------------------------Running org.test.main.TestHelloMavenTests run:1, failures : 0, errors:0, skipped:0, Time elapsed:0.087 secresults:tests run:1, failures:0, errors:0, Skipped:0[info]------- -----------------------------------------------------------------[INFO] BUILD Success[info]--------------------- ---------------------------------------------------[INFO] Total time:3.373 s[info] finished at:2015-10-20t15:31:57+ 08:00[info] Final Memory:14m/138m[info]------------------------------------------------------------------------

The directory structure after the test is as follows:

├─src│  ├─main│  │  └─java│  │       └─org│  │          └─test│  │               └─main│  └─test│       └─java│          └─org│               └─test│                   └─main└─target     ├─classes    │  └─org    │       └─test    │           └─main    ├─maven-status    │  └─maven-compiler-plugin     │      ├─compile    │      │   └─default-compile    │      └─testCompile     │          └─default-testCompile     ├─surefire-reports    └─test-classes         └─org            └─test                 └─main

The Surefire-reports directory contains the test results.


The Clean directive clears the directories and files that are generated when compiling, testing, and packaging.

To run a command in the project directory:

MVN clean

The target directory under the project directory is now deleted.


The package directive is used to build the jar packages for the project.

To run a command in the project directory:

MVN Package

At this point, MAVEN downloads the relevant package, compiles the source code, tests the code, executes the test, and if the test passes, the source code will be packaged. The project's jar file is generated under the target directory.


The install instruction installs the package to a local MAVEN repository for other project references.

To run a command in the project directory:

MVN Install

At the end of execution, the corresponding directory and file will be generated under the System user directory as follows:

~/.m2/repository/org/test/demo$ls-alhtotal 5.0kdrwxr-xr-x 1 Cloud 197121 0 Month 08:23. Drwxr-xr-x 1 Cloud 197121 0 Month 20 08:23.. Drwxr-xr-x 1 Cloud 197121 0 Month 08:23 1.0-snapshot-rw-r--r--1 Cloud 197121 2,740 month 08:23 maven-metadata-local.xml
Convert a MAVEN project into an eclipse-supported Java project

To run a command in the project directory:

MVN Eclipse:eclipse

At this point, the. Classpath and. Project are generated under the project directory. Open the Eclipse Import project with the MAVEN integration for Eclipse plugin installed.

Combination directives

MAVEN's compilation, cleanup, testing, packaging, deployment commands can be combined with several commands at the same time, common command combinations

Such as:

First clean up and then compile:mvn clean compile

First clean up and then pack:mvn clean package

Default conventions for Maven

In the example above, Maven has done a lot of work with a single instruction. For example, compile the source code under the Src/main/java directory and output it to the target directory. These directories are pre-set by Maven. MAVEN uses the principle of class-grails "conventions over Configuration".

Directory Describe
${project.dir}/src/main/java Java Source Directory
${project.dir}/src/main/resources Resource file directory
${project.dir}/src/main/filters Resource Filter Catalog
${project.dir}/src/main/webapp Web application file directory (when packaged as a war), such as Web-inf/web.xml
${project.dir}/src/test/java Test Java Source Directory
${project.dir}/src/test/resources Test resource file Directory
${project.dir}/src/test/filters Test Resource Filter Directory
${project.dir}/src/it Integration testing (mainly plug-ins)
${project.dir}/src/assembly Assembly descriptors
${project.dir}/src/site Project Site Output Directory
${project.dir}/license.txt Project License
${project.dir}/notice.txt The project relies on the property requirements of the class library, and to advertise
${project.dir}/readme.txt The Readme file for the project, which describes what the source code is used for? How do I use projects? structure information for important files and subdirectories in your project?

This article is from the "Happiness Password" blog, please be sure to keep this source http://programmersky.blog.51cto.com/5326405/1772682

02. The first MAVEN application

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.