1 maven安装
1.1 下载地址:http://maven.apache.org/download.cgi
1.2 环境变量
解压后配置环境变量
windows
MAVEN_HOME D:\maven-3.0.4
PATH C:\Program Files (x86)\Common Files\NetSarang;%SystemRoot%\system32;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;D:\10.2.0\client_1\BIN;C:\Program Files\TortoiseSVN\bin;%GIT_HOME%\bin;%HADOOP_HOME%\bin;%MAVEN_HOME%\bin;%GIT_HOME%\bin
linux
~/.bashrc下添加
MAVEN_HOME=${MAVEN_HOME_DIR}
PATH=$MAVEN_HOME/bin:$PATH
export MAVEN_HOME
export PATH
1.3 查看版本
安装成功后可以使用命令查看maven版本。
C:\Users\hepan>mvn -v
C:\Users\hepan>set MAVEN_OPTS=-XX:MaxPermSize=128m
Apache Maven 3.0.4 (r1232337; 2012-01-17 16:44:56+0800)
Maven home: D:\maven-3.0.4\bin\..
Java version: 1.6.0_32, vendor: Sun Microsystems Inc.
Java home: D:\Java\jdk1.6.0_32\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
2 MAVEN HELLO WORLD
新建目录及文件结构如下:
(先建Hello.java文件及其文件结构,HelloTest.java后面会用到。)
E:\work_test\mvnhello\hello01
|----pom.xml
|----src
| |----main
| | |----java
| | |----com
| | |----hptest
| | |----hp
| | |----Hello.java
| |----test
| |----java
| |----com
| |----hptest
| |----hp
| |----HelloTest.java
Hello.java代码如下:
package com.hptest.hp;
public class Hello{
public String sayHello(String name){
return "Hello:"+name;
}
}
pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.hptest.hp</groupId>
<artifactId>maven-project-test</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
groupId:用来表示项目的名称
artifactId:模块名称一般用“项目名-模块名”
version:版本信息
在hello01目录下运行maven编译命令
E:\work_test\mvnhello\hello01>mvn compile
E:\work_test\mvnhello\hello01>set MAVEN_OPTS=-XX:MaxPermSize=128m
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-project-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ maven-proj
ect-test ---
[debug] execute contextualize
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\work_test\mvnhello\hello01\src\mai
n\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ maven-project
-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.033s
[INFO] Finished at: Mon Nov 24 14:13:42 CST 2014
[INFO] Final Memory: 4M/104M
[INFO] ------------------------------------------------------------------------
ps:第一次回下载许多jar包
新建HelloTest.java文件及目录结构,代码如下:
package com.hptest.hp;
import org.junit.Assert;
import org.junit.Test;
public class HelloTest
{
@Test
public void TestHello(){
Hello hello=new Hello();
String str=hello.sayHello("maven");
Assert.assertEquals(str,"Hello:maven");
}
}
因引用了junit的jar包所以需要在pom.xml中添加相关的依赖。
<?xml version="1.0" encoding="UTF-8"?>
<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.hptest.hp</groupId>
<artifactId>maven-project-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!--
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
-->
</dependencies>
</project>
maven运行命令:
maven test
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.hptest.hp.HelloTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.047 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.395s
[INFO] Finished at: Mon Nov 24 14:24:32 CST 2014
[INFO] Final Memory: 4M/58M
[INFO] ------------------------------------------------------------------------
3 修改maven配置文件
因为maven会把jar包下载到系统用户目录下,为了方便管理需要将maven下载路径修改。
D:\maven-3.0.4\conf\settings.xml
<localRepository>E:\.m2\repository</localRepository>
小结:
maven命令
编译
mvn clean 清理(默认会把target目录下的文件数据清理掉)
mvn clean package 清理并打包
mvn clean install 清理并安装
mvn clean test 清理并运行测试类
mvn clean deploy 清理并发布到私服上(私服:企业私有服务器)
mvn compile 编译
mvn test 测试
mvn install 安装到本地库
资源库位置:http://www.mvnrepository.com/
这里可以查找jar包,及pom如何引用。