Maven getting started ---- introduction and environment setup (1), maven ----

Source: Internet
Author: User

Maven getting started ---- introduction and environment setup (1), maven ----

Knowledge point:

Maven introduction:

Baidu Encyclopedia:

It is a project management tool.

 

Build a Maven environment on the local machine:

The Maven environment must be supported by the jdk environment. First, ensure that the jdk environment already exists on the local machine and the environment variables are configured successfully.

 

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

Currently, version 3.39 is available. I use version 3.3.3 here. Now maven3 is basically used, and the previous 2 has been eliminated.

 

Create a maven folder under the local drive D, put the downloaded compressed package, and decompress it to this folder, as shown in figure

Environment Variable Configuration:

M2_HOME = D: \ maven \ apache-maven-3.3.3

 

Path =; % M2_HOME % \ bin;

 

Test whether the installation is successful:

Mvn-version

The installation is successful here.

DEMO test:

Create a path to the D: \ HelloWorld \ src \ main \ java \ com \ qinb \ helloWorld folder under drive D, and create a HelloWorld. java class under this path:

Code:

Package com. qinb. helloWorld;

 

Public class HelloWorld {

 

Public String sayHello (){

Return "Hello World! ";

}

Public static void main (String [] args ){

System. out. println (new HelloWorld (). sayHello ());

}

}

Create pom. xml under D: \ HelloWorld,

Code:

<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/xsd/maven-4.0.0.xsd">

<ModelVersion> 4.0.0 </modelVersion>

<GroupId> com. qinb. HelloWorld </groupId>

<ArtifactId> HelloWorld </artifactId>

<Version> 0.0.1-SNAPSHOT </version>

 

<Dependencies>

 

</Dependencies>

</Project>

Note:

ModelVersion: the POM model version 4.0.0 is fixed.

GroupId: a project of a company or organization, such as org. springframework

ArtifactId: generally refers to a specific module of a specific project, such as spring-context.

Version: Version of the project.

 

Maven Common commands

Mvn compile Compilation

Mvn clean clear

Mvn test

Mvn package Packaging

Mvn install the project to the local repository

Mvn remote repository address: http://mvnrepository.com/

 

Now you can start the test. In the cmd window, first enter D: \ HelloWorld under the project. The first run will download many support packages from the remote repository, which will take a little time.

Test: mvn compile

Compile the test class:

All Maven path names are required.

Create a HelloWorldTest. java class under D: \ HelloWorld \ src \ test \ java \ com \ qinb \ helloWorld

Code:

Package com. qinb. helloWorld;

Import org. junit. Test;

 

Public class HelloWorldTest {

 

@ Test

Public void testSayHello (){

HelloWorld helloWorld = new HelloWorld ();

String result = helloWorld. sayHello ();

System. out. println (result );

}

}

Because the test classes used here require Junit support, all the Junit. jar packages added to pom. xml

First, go to the mvn remote warehouse to find the package we need: simply search for Junit. Select the first one. This remote warehouse is quite useful. What do you want, you only need to add the relevant code. when the project is running, the corresponding package is automatically downloaded from the remote repository to the local repository.

Select the first one.

Copy the code to pom. xml.

 

 

Then test the test class: During the first running, many support packages will also be downloaded from the remote repository. Wait a moment.

 

Mvn test

 

The test is successful.

The Mvn package can be used directly. After it is used up, a jar package is generated under D: \ HelloWorld \ target.

 

The local warehouse is migrated out.

When we run it for the first time or later, we will encounter more jar packages, which will be downloaded a lot. maven is downloaded to drive C by default. Here, I will change the address of the local repository downloaded by default to disk D.

First open setting. xml under D: \ maven \ apache-maven-3.3.3 \ conf to add the content in the following REDLINE box.

<LocalRepository> D: \ maven \ repository </localRepository>

 

Here, I will change the address of the local repository to D: \ maven \ repository and save it. However, after the address is changed, many jar packages will be downloaded for the first time when mvn is run, it takes time.

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.