标签:
更多内容请参考官方文档:http://maven.apache.org/guides/index.html
官方文档很详细,基本上可以查找到一切相关的内容。
(一)搭建环境
1、下载maven,并将之解压
或者直接使用yum 来安装
2、配置环境变量
export PATH=/home/jediael/setupfile/apache-maven-3.1.1/bin:$PATH
3、验证环境
[jediael@master maven_demo]$ mvn -v
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 23:22:22+0800)
Maven home: /home/jediael/setupfile/apache-maven-3.1.1
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: /usr/java/jdk1.7.0_51/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-431.17.1.el6.x86_64", arch: "amd64", family: "unix"
(二)项目编写
1、建立如下目录结构
[jediael@master maven_demo]$ tree
.
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── ljh
│ └── mavendemo
│ └── HelloMaven.java
└── test
└── java
└── com
└── ljh
└── mavendemo
└── HelloMavenTest.java
11 directories, 3 files
2、HelloMaven.java内容如下
package com.ljh.mavendemo;
public class HelloMaven{
public String sayHello(String s){
return "hello:"+s;
}
public static void main(String[] args){
HelloMaven hm = new HelloMaven();
System.out.println(hm.sayHello("maven"));
}
}
3、HelloMavenTest.java内容如下:
package com.ljh.mavendemo;
import org.junit.*;
import static org.junit.Assert.*;
public class HelloMavenTest{
@Test
public void testSayHello(){
HelloMaven hm = new HelloMaven();
assertEquals("hello:Jason",hm.sayHello("Jason"));
}
}
4、pox.xml内容如下:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ljh.mvn</groupId>
<artifactId>mavendemo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>mavendeom</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(三)运行项目
1、编译项目
$ mvn compile
2、运行测试
$ mvn test
3、也可以对项目直接打包成jar包
$ mvn package
此过程也会执行test
4、若所生成的jar包中包括main方法,则可以使用以下命令执行
$ java -cp target/mavendemo-1.0-SNAPSHOT.jar com.ljh.mavendemo.HelloMaven
hello:maven
经过这些步骤后的目录结构如下:
[jediael@master maven_demo]$ tree
.
├── pom.xml
├── src
│ ├── main
│ │ └── java
│ │ └── com
│ │ └── ljh
│ │ └── mavendemo
│ │ └── HelloMaven.java
│ └── test
│ └── java
│ └── com
│ └── ljh
│ └── mavendemo
│ └── HelloMavenTest.java
└── target
├── classes
│ └── com
│ └── ljh
│ └── mavendemo
│ └── HelloMaven.class
├── maven-archiver
│ └── pom.properties
├── mavendemo-1.0-SNAPSHOT.jar
├── surefire-reports
│ ├── com.ljh.mavendemo.HelloMavenTest.txt
│ └── TEST-com.ljh.mavendemo.HelloMavenTest.xml
└── test-classes
└── com
└── ljh
└── mavendemo
└── HelloMavenTest.class
22 directories, 9 files
标签:
原文地址:http://blog.csdn.net/jediael_lu/article/details/44986075