Getting Started with maven (i)

Source: Internet
Author: User

First, the basic concept of Maven

  maven 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, MAVEN's basic content

MAVEN's contract: The contract Java source code must be placed in which directory, compiled Java code must be placed in which directory, these directories have a clear convention.

Maven action: Each process that builds a project maven has a corresponding shell command.

Maven config file: In the project you just need to define a pom.xml, then put the source into the default directory, Maven helps us with other things.

Maven Management: Dependency management, Warehouse management.

  

Ii. maven Download and configure environment variables 1. Download

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

Download the zip package, maven is a green lightweight tool that extracts and configures environment variables to use. (If the Java environment is already installed on the machine)

2. Configure Environment variables

VI Open the. zshrc file under your personal directory (you should open the. bash_profile file with bash).

Edit the. zshrc file as follows:

    

Mainly the contents of the red box, the other content is my own machine environment variables, not related to maven.

Maven_home is the unpacked address of Maven's zip and then configures the Maven bin directory to the path, so you can manipulate the shell command directly for MAVEN.

    

The terminal window enters source. ZSHRC (Students with bash should enter source. bash_profile).

After you enter mvn-v directly, the following information indicates that the configuration was successful.

    

Third, the simple use of Maven 1, directory Conventions:

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

2. Manually create a MAVEN Java project

Create a folder and its Java files in accordance with MAVEN's directory conventions:

    

Create the pom.xml file under the root of the project and configure it as follows:

    

<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "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">    <!--all MAVEN projects must be configured with these four configuration items -    <modelversion>4.0.0</modelversion>    <!--GroupID refers to the project group, which by default is the package name -    <groupId>Com.xxlxx.maven.hello</groupId>    <!--Artifactid refers to a module in the project, the default naming method is "project name-module name" -    <Artifactid>Hello-maven</Artifactid>    <!--version refers to versions, where Maven's snapshot version is used -    <version>SNAPSHOT-0.0.1</version></Project>

Using MAVEN to compile Hellomaven.java, first go to the project root and then compile with the command "mvn compile":

Pre-compilation file directory:

    

Compile information:

    

Compiled file directory:

    

You can see a more target directory, the target directory has the Classes folder, the folder contains the SRC directory in the Java file compiled after the class file.

PS. There is also a command "mvnclean", which deletes the target file under the project root directory.

Iv. maven Settings.xml and repository 1, local dependent warehouse repository

The warehouse is located by default in the. M2 folder in the user's root directory. The first time you execute the two commands "mvn compile" and "mvn clean", MAVEN will go to the central repository and download the required jar packages to the local repository, which is the Repository folder. The second time the two commands are executed, because the required jar packages are already stored in the local repository, they can be used directly, eliminating the need to download the jar package to the central repository.

2. maven configuration file Settings.xml

The XML file has two copies, the first of which is in the Conf folder under Maven_home, which is the global configuration file for maven. The second part is in the. m folder described above, which is Maven's user profile. Two configuration files are called Settings.xml.

    

Use this XML file to customize the location of the repository:

    

        

    

    

    

    

    

    

  

  

Getting Started with maven (i)

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.