[Taught you] Maven build process

Maven is a be dependent on management, project building, information management as one of the tools.

Make it like a complicated command, unlike Ant need to manually write a lot of code duplication can build projects;

Also provides powerful dependencies management, to avoid confusion and conflict jar package;

The content information may also facilitate the management of projects and intellectual property rights.

Benpian purpose of writing is to help those who previously had not used or is not a friend maven much the concept, learn to use maven, use maven to build the project.

Maven explained by downloading, installation, write, perform tasks quickly experience Maven modes of operation and use of skills ...

download

High current version of Eclipse has been integrated with Maven, but considering the stability of the plug-in, it is recommended to use Maven own installation (requires binding in Eclipse).

Manually install Maven need to download the official website, the latest version is 3.3.9: http://maven.apache.org/download.cgi

Users download the zip under the general windows on it, if you want to learn the source code, you can download that version of src.

installation

1 first need to install the JDK, enter java -version verify the version of the command line:

2 and then extract the downloaded archive:

3 configuration environment variable

In the "Environment Variables" configuration:

M2_HOME:xxxx/apache-maven-3.3.9

在path中添加:";%M2_HOME%/bin"

4 mvn -v test input on the command line

write

If you've used all know Ant Ant build tasks are performed by build.xml, Maven is to perform tasks by pom.xml.

POM, project object model, that is, the project object model, which is described by the pom.xml build a project, and information.

<?<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xinoo.test</groupId>
    <artifactId>first-maven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>First Maven Project</name>
</project>

The first line specifies the XML version and encoding of the document

The second line that is a core element of --project each pom.xml

Here are a few sub-project elements, these sub-elements is generally for each project will be used to:

1 modelVersion This element specifies the version of POM, Maven2 or Maven3 can only be 4.0.0

2 groupId is the ID of the project team, generally com. Company organization name Project name

3 artifactId is the ID project in the project group, such as the current project is a project group of agents, can be called myproxy

4 version is a revision of the project for upgrade and maintenance projects and publishing

5 name generally no practical use, but it used to identify the project

More important parameter is the groupId, artifactId, version, these three attributes determine only one project.

Mission

General Maven project will include such a directory tree:

project
    |-src--main--java--你的源代码
    |    |--test----java--你的单元测试代码
    |-target--编译出的文件和jar包
    |-pom.xml--项目信息以及任务定义

Therefore, we in the above pom.xml same level directory, create

src / main / java / com / xingoo / test / firstmaven folder

Then create a folder in the file HelloWorld.java

package com.xingoo.test.firstmaven;

public class HelloWorld{
    public String sayHello(){
        return "Hello Maven";
    }
    public static void main(String[] args){
        System.out.println(new HelloWorld().sayHello());
    }
}

Then again pom.xml directory where the run mvn clean compile command:

Will find the directory more than a target folder, folder adds two folders:

Wherein java classes are compiled class files, maven-status is dependent compile plug.

reference

[1] The final task execution Example: Baidu cloud disk file mvn1127.zip

[2] "Maven combat": Baidu cloud disk file Maven combat tutorial

Reproduced in: https: //my.oschina.net/u/204616/blog/545302

Guess you like

Origin blog.csdn.net/weixin_34235105/article/details/91989969