Maven Getting Started Tutorial

Source: Internet
Author: User

1.1. Introduction to Maven

MAVEN is a software project management tool that is based on a Project object model (POM) that can manage the construction, reporting, and documentation of a project through a short description of the information.

In addition to its program building capabilities, Maven offers advanced project management tools. Because MAVEN's default build rules are highly reusable, it is common to build scripts with two or three lines of Maven to build simple projects. Thanks to MAVEN's project-oriented approach, many Apache Jakarta projects use Maven, and the percentage of company projects that are MAVEN-based continues to grow.

MAVEN is a software project management tool that is based on a Project object model (POM) that can manage the construction, reporting, and documentation of a project through a short description of the information. The word maven comes from Yiddish, meaning the accumulation of knowledge, which was first used in the Jakata turbine project to try to simplify the construction process. There were many projects at the time, their ant build files were only slightly different, and the jar files were maintained by CVS. So the Maven founders started MAVEN, a clear definition of the project, including a convenient way to publish project information, and a way to share jars across multiple projects.

1.2. maven download and install 1.2.1. Download maven

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

1.2.2. Installing Maven

Simply unzip the Maven tarball into the specified folder

1.2.3. Configuring Environment variables

The variable name is: M2_home

The value of the variable is: D:\javaEEServer\apache-maven-3.0.3


Add% m2_home%\bin in Path path


1.2.4. Test whether the installation was successful

The version information for Maven will appear at the command line by typing mvn–v. JDK version and other information.


1.3. Hello Maven1.3.1. Build the project structure

The structure of the project is as follows:


1.3.2. Pom.xml

<?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.0http://maven.apache.org/xsd/maven-4.0.0.xsd" >

<modelVersion>4.0.0</modelVersion>

<groupId>com.morris.maven.hello</groupId>

<artifactId>hello</artifactId>

<version>1.0-SNAPSHOT</version>

<packaging>jar</packaging>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.10</version>

<scope>test</scope>

</dependency>

</dependencies>

</project>

1.3.3. Hello.java

public class hello{

Publicstring SayHello (String name) {

Return "Hello:" +name;

}

}

1.3.4. Hellotest.java

Import org.junit.*;

Import static junit.framework.assert.*;

public class Hellotest

{

@Test

Publicvoid Testsayhello () {

Hellohm = new Hello ();

Assertequals (Hm.sayhello ("Morris"), "Hello:morris");

}

}

1.3.5. Compiling MVN Compile

Compiling the project at the command line using MVN compile will print the following information, and if it is the first run, the jar package will be downloaded from the Web.

1.3.6. Testing MVN Test

To unit test a project using MVN test at the command line, the following information is printed:

[INFO] Scanning for projects ...

[INFO]

[INFO]------------------------------------------------------------------------

[INFO] Building Hello 1.0-snapshot

[INFO]------------------------------------------------------------------------

[INFO]

[INFO]---maven-resources-plugin:2.6:resources (default-resources) @ Hello---

[WARNING] Using platform Encoding (gbkactually) to copy filtered resources, i.e. build is platform dependent!

[INFO] Skip non existing resourcedirectorye:\java\maven\maven-hello\src\main\resources

[INFO]

[INFO]---maven-compiler-plugin:3.1:compile (default-compile) @ Hello---

[INFO] Compile-all Classes Areup to date

[INFO]

[INFO]---maven-resources-plugin:2.6:testresources (default-testresources) @ Hello---

[WARNING] Using platform Encoding (gbkactually) to copy filtered resources, i.e. build is platform dependent!

[INFO] Skip non existing resourcedirectorye:\java\maven\maven-hello\src\test\resources

[INFO]

[INFO]---maven-compiler-plugin:3.1:testcompile (default-testcompile) @ Hello---

[INFO] Compile-all Classes Areup to date

[INFO]

[INFO]---maven-surefire-plugin:2.12.4:test (default-test) @ Hello---

[INFO] Surefire Report Directory:e:\java\maven\maven-hello\target\surefire-reports

-------------------------------------------------------

T ES t S

-------------------------------------------------------

Running Hellotest

Tests Run:1, failures:0, errors:0,skipped:0, Time elapsed:0.052 sec

Results:

Tests Run:1, failures:0, errors:0,skipped:0

[INFO]------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO]------------------------------------------------------------------------

[INFO] Total time:1.941 S

[INFO] Finished at:2015-03-01t17:39:10+08:00

[INFO] Final memory:7m/21m

[INFO]------------------------------------------------------------------------

1.3.7. Observing the project directory

We will find that some folders and files will be added to the project directory.


1.3.8. Packaging MVN Package

Command line run MVN package will automatically make the project into a jar, and we will find a jar package in the target directory Hello-1.0-snapshot.jar

The command line prints the following information:

[INFO] Scanning for projects ...

[INFO]

[INFO]------------------------------------------------------------------------

[INFO] Building Hello 1.0-snapshot

[INFO]------------------------------------------------------------------------

[INFO]

[INFO]---maven-resources-plugin:2.6:resources (default-resources) @ Hello---

[WARNING] Using platform Encoding (gbkactually) to copy filtered resources, i.e. build is platform dependent!

[INFO] Skip non existing resourcedirectorye:\java\maven\maven-hello\src\main\resources

[INFO]

[INFO]---maven-compiler-plugin:3.1:compile (default-compile) @ Hello---

[INFO] Compile-all Classes Areup to date

[INFO]

[INFO]---maven-resources-plugin:2.6:testresources (default-testresources) @ Hello---

[WARNING] Using platform Encoding (gbkactually) to copy filtered resources, i.e. build is platform dependent!

[INFO] Skip non existing resourcedirectorye:\java\maven\maven-hello\src\test\resources

[INFO]

[INFO]---maven-compiler-plugin:3.1:testcompile (default-testcompile) @ Hello---

[INFO] Compile-all Classes Areup to date

[INFO]

[INFO]---maven-surefire-plugin:2.12.4:test (default-test) @ Hello---

[INFO] Surefire Report Directory:e:\java\maven\maven-hello\target\surefire-reports

-------------------------------------------------------

T ES t S

-------------------------------------------------------

Running Hellotest

Tests Run:1, failures:0, errors:0,skipped:0, Time elapsed:0.052 sec

Results:

Tests Run:1, failures:0, errors:0,skipped:0

[INFO]------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO]------------------------------------------------------------------------

[INFO] Total time:1.941 S

[INFO] Finished at:2015-03-01t17:39:10+08:00

[INFO] Final memory:7m/21m

[INFO]------------------------------------------------------------------------

1.3.9. Release MVN Install

The command line uses the MVN install command to publish our project in the local warehouse, and the address of the local warehouse is in My computer's document by default: C:\Users\Morris\.m2\repository

We can see that there will be a project file under this C:\Users\Morris\.m2\repository\com\morris\maven\hello\hello\1.0-SNAPSHOT path.

The background print information is as follows:

[INFO] Scanning for projects ...

[INFO]

[INFO]------------------------------------------------------------------------

[INFO] Building Hello 1.0-snapshot

[INFO]------------------------------------------------------------------------

[INFO]

[INFO]---maven-resources-plugin:2.6:resources (default-resources) @ Hello---

[WARNING] Using platform Encoding (gbkactually) to copy filtered resources, i.e. build is platform dependent!

[INFO] Skip non existing resourcedirectorye:\java\maven\maven-hello\src\main\resources

[INFO]

[INFO]---maven-compiler-plugin:3.1:compile (default-compile) @ Hello---

[INFO] Compile-all Classes Areup to date

[INFO]

[INFO]---maven-resources-plugin:2.6:testresources (default-testresources) @ Hello---

[WARNING] Using platform Encoding (gbkactually) to copy filtered resources, i.e. build is platform dependent!

[INFO] Skip non existing resourcedirectorye:\java\maven\maven-hello\src\test\resources

[INFO]

[INFO]---maven-compiler-plugin:3.1:testcompile (default-testcompile) @ Hello---

[INFO] Compile-all Classes Areup to date

[INFO]

[INFO]---maven-surefire-plugin:2.12.4:test (default-test) @ Hello---

[INFO] Surefire Report Directory:e:\java\maven\maven-hello\target\surefire-reports

-------------------------------------------------------

T ES t S

-------------------------------------------------------

Running Hellotest

Tests Run:1, failures:0, errors:0,skipped:0, Time elapsed:0.088 sec

Results:

Tests Run:1, failures:0, errors:0,skipped:0

[INFO]

[INFO]---maven-jar-plugin:2.4:jar (default-jar) @ Hello---

[INFO]

[INFO]---maven-install-plugin:2.4:install (default-install) @ Hello---

[INFO] Installinge:\java\maven\maven-hello\target\hello-1.0-snapshot.jar toc:\users\morris\.m2\repository\com\morris\ Maven\hello\hello\1.0-snapshot\hello-1.0-snapshot.jar

[INFO] Installinge:\java\maven\maven-hello\pom.xml toc:\users\morris\.m2\repository\com\morris\maven\hello\hello\1.0- Snapshot\hello-1.0-snapshot.pom

[INFO]------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO]------------------------------------------------------------------------

[INFO] Total time:2.610 S

[INFO] Finished at:2015-03-01t17:52:46+08:00

[INFO] Final memory:7m/19m

[INFO]------------------------------------------------------------------------

Maven Getting Started Tutorial

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.