码迷,mamicode.com
首页 > 其他好文 > 详细

maven入门教程

时间:2015-03-02 09:42:17      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:maven

1.1.  Maven简介

Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。

Maven 除了以程序构建能力为特色之外,还提供高级项目管理工具。由于 Maven 的缺省构建规则有较高的可重用性,所以常常用两三行 Maven 构建脚本就可以构建简单的项目。由于 Maven 的面向项目的方法,许多 Apache Jakarta 项目发文时使用 Maven,而且公司项目采用 Maven 的比例在持续增长。

Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。Maven这个单词来自于意第绪语,意为知识的积累,最早在Jakata Turbine项目中它开始被用来试图简化构建过程。当时有很多项目,它们的Ant build文件仅有细微的差别,而JAR文件都由CVS来维护。于是Maven创始者开始了Maven这个项目,该项目的清晰定义包括,一种很方便的发布项目信息的方式,以及一种在多个项目中共享JAR的方式。

1.2.  Maven的下载与安装

1.2.1.  下载maven

下载地址: http://maven.apache.org/download.html 

1.2.2.  安装maven

只要将maven的压缩包解压到指定文件夹即可

1.2.3.  配置环境变量

变量名为:M2_HOME

变量值为: D:\javaEEServer\apache-maven-3.0.3

技术分享

在path路径中添加% M2_HOME %\bin

技术分享

1.2.4.  测试是否安装成功

在命令行输入mvn –v  就会出现maven的版本信息.jdk的版本及其他信息.

技术分享

1.3.  Hello Maven

1.3.1.  搭建项目结构

项目的结构如下图所示:

技术分享

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.  编译Mvn compile

在命令行使用mvn compile编译项目,会打印如下信息,如果是第一次运行的话,会从网上下载jar包

技术分享

1.3.6.  测试mvn test

在命令行使用mvn test对项目进行单元测试,会打印如下信息:

[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] Nothing to 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] Nothing to 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.  观察项目目录

我们会发现项目的目录中会增加一些文件夹和文件

技术分享

1.3.8.  打包mvn package

命令行运行mvn package会将项目自动打成一个jar包,我们会发现在target目录下会多出一个jar包hello-1.0-SNAPSHOT.jar

 

命令行会打印如下信息:

[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] Nothing to 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] Nothing to 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.  发布mvn install

命令行使用mvn install 命令会将我们的项目发布在本地仓库中,本地仓库的地址默认是在电脑的我的文档中:C:\Users\Morris\.m2\repository

我们可以发现C:\Users\Morris\.m2\repository\com\morris\maven\hello\hello\1.0-SNAPSHOT这个路径下会有项目的文件

后台打印信息如下:

[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] Nothing to 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] Nothing to 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入门教程

标签:maven

原文地址:http://blog.csdn.net/u022812849/article/details/44004101

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!