Maven Learning Summary (i)--maven introduction

Source: Internet
Author: User
Tags maven central

First, the basic concept of Maven

  Maven (translated as "expert", "insider") is a cross-platform project management tool. It mainly serves the project building based on the Java platform, and relies on management and project information management .

1.1. Project Construction

The project build process consists of "cleanup project" → "Compile project" → "Test Project" → "Generate test Report" → "Package Project" → "Deploy project" steps, which are the complete building process of a project.

  

The ideal project build is highly automated, cross-platform, reusable components, standardized, and using MAVEN can help us complete the project building process described above.

1.2. Dependency Management

Dependency refers to the interdependence between jar packages, such as when we build a STRUTS2 development framework, Struts2-core-2.3.16.3.jar This jar package is not available, Struts2-core-2.3.16.3.jar also relies on other jar packages, and dependency management refers to using MAVEN to manage the jar packages used in the project, and Maven manages the way "Automatically download the jar packages required for the project, and manage the dependencies between the jar packages uniformly."

1.3. Benefits of using Maven

The use of conventions in Maven, the Convention Java source code code must be placed in which directory, compiled Java code must be placed in which directory, these directories have a clear convention.

Each MAVEN action has a lifecycle, such as executing the MVN install to automate the build process such as compiling, testing, packaging, etc.

Just need to define a pom.xml and then put the source into the default directory, Maven helps us with other things

With Maven, you can build highly automated projects, rely on management (which is the biggest benefit of using MAVEN), and warehouse management.

Second, maven download

: http://maven.apache.org/download.cgi

  

After the download is complete, get a zipped package, unzip it, and you can see the MAVEN composition directory

  

Maven Directory Analysis

    • Bin: script containing mvn run
    • Boot: Contains the Plexus-classworlds class loader framework
    • Conf: contains settings.xml configuration file
    • LIB: Contains the Java class library required by the MAVEN runtime
    • LICENSE.txt, NOTICE.txt, README.txt a brief introduction to the MAVEN version, third-party software, etc.
Third, MAVEN installation

1, first of all to ensure that the computer has installed JDK(to JDK 1.6+ version), configure the JDK environment variables, using the following two commands to check the JDK installation.

1 Echo%java_home%2 Java-version

  

2. Unzip the Apache-maven-3.2.3-bin.zip

Unzip the Apache-maven-3.2.3-bin.zip, for example extract to the following directory ( unzip the directory is best not to have Chinese )

  

3. Set the SYSTEM environment variable: maven_home

  

  4, set the environment variable path, add%maven_home%\bin to the path, be sure to use a semicolon, separated from other values, as shown in:

  

%maven_home%\bin stands for the "E:\apache-maven-3.2.3\bin" directory.

  5. Verify that MAVEN installation is successful

Open cmd window

  

Enter the "mvn–v" command to see information about MAVEN, as shown in:

  

The ability to present this information indicates that MAVEN's installation has been successful.

6. Set maven_opts environment variable (optional configuration)

Because the MAVEN command actually executes the Java command, the maven run parameter can be set by the java command parameter. Maven_opts environment variables are precisely used for this purpose

Maven_opts

-xms128m-xmx512m, set the minimum and maximum memory for the JVM, as shown in:

  

Iv. simple use of Maven 4.1. Directory conventions for MAVEN projects

Mavenprojectroot (Project root directory)
|----src
| |----Main
|         | |----java--. java files that store the project
|         | |----resources--to store project resource files, such as spring, hibernate configuration file
| |----Test
|         | |----java--Store all tests. java files, such as JUnit test classes
|         | |----resources--to store project resource files, such as spring, hibernate configuration file
|----target--Project OUTPUT location
|----pom.xml----used to identify the project as a MAVEN project

4.2. Manually create a MAVEN project and use MAVEN to compile

1. Create the project root folder, for example Maven01

  

2. Create a "pom.xml" file in the Maven01 folder, as shown in:

  

The contents of the Pom.xml file are as follows:

1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <project xmlns= "http://maven.apache.org/POM/4.0.0"  3 xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"  4 xsi:schemalocation= "http://maven.apache.org/POM/4.0.0  5 http://maven.apache.org/xsd/maven-4.0.0.xsd "> 6     <!--all MAVEN projects must be configured with these four configuration items--7     < Modelversion>4.0.0</modelversion> 8     <!--groupId refers to a project group with the project name, which by default is the package name--9     <groupId> Cn.gacl.maven.hello</groupid>10     <!--Artifactid refers to one of the modules in the project, the default naming method is "project name-module name"-->11     < Artifactid>hello-first</artifactid>12     <!--version refers to versions, which use the snapshot version of Maven-->13     < Version>snapshot-0.0.1</version>14 </project>

3. To write Java class files, the MAVEN project stipulates that all *.java files must be placed in the Java directory under the main directory in the SRC directory, create a src directory in the Maven01 project root directory, and then create the main directory in the SRC directory. In the main directory, create the Java folder again, as shown in:

  

Create a Hello.java file under the Java folder, as shown in

  

Write the following code in the Hello.java file:

1 public class Hello{2 public     static void Main (string[] args) {3         System.out.println ("Hello Maven"); 4     }5}

4. Using MAVEN to compile the Hello.java, first go to the project root and then compile with the command "mvn compile" as shown in:

  

  

After using MAVEN compilation, a target folder is generated under the project root directory, as shown in:

  

Open the target folder and you can see that there is a classes folder in it, as shown in:

  

The Classes folder is the. class file generated after the successful compilation, as shown in:

  

5. Use the "mvnClean" command to clear the compilation result, which is to delete the target folder compiled by the build, as shown in:

  

After executing the "mvnClean" command, the target folder will be deleted.

V. Modify the default storage location of the jar packages downloaded from the MAVEN central repository to the local

The default storage of the jar packages downloaded from the MAVEN central warehouse to the local is in "${user.home}/.m2/repository", and ${user.home} represents the user directory (such as "C:\Users\gacl") that is currently logged into the system, as shown in

  

  

The jar package is not very good to store in this location, we would like to be able to define the location of the downloaded jar package, so we can set ourselves to download to the local jar package storage directory.

Create a "repository" folder under the "E:\" directory

  

Locate the settings.xml file in the apache-maven-3.2.3\conf directory, as shown in:

  

Edit the Setting.xml file as shown in:

  

Add the following code

1 <localRepository>E:/repository</localRepository>

  

This allows the jar package to be downloaded to our specified e:/repository directory, as shown in:

  

The advantage of downloading the jar package to the local is that when it is compiled, it will take precedence from the local jar package, and if it exists locally, it will be used directly, and if it does not exist, it will be downloaded from the MAVEN central repository. As shown in the following:

  

The first time you execute the "mvn compile" and "mvn clean" commands, MAVEN will go to the central repository to download the required jar packages, and the second time you execute the two commands, you can use them directly because the required jar packages are already stored in the local repository. This eliminates the time to download the jar package to the central warehouse.

These are the simple introductory tutorials for maven.

Maven Learning Summary (i)--maven introduction

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.