maven_经典入门

更多资料,请访问www.naxsu.com


下载地址:http://maven.apache.org/download.html
apache-maven-3.0.4-bin.zip
把maven所在的bin路径(E:\maven\apache-maven-3.0.4\bin)添加到环境变量中。
测试maven是否安装成功:
C:\Users\itway>mvn -version
Apache Maven 3.0.4 (r1232337; 2012-01-17 16:44:56+0800)
Maven home: E:\maven\apache-maven-3.0.4\bin\..
Java version: 1.6.0_20, vendor: Sun Microsystems Inc.
Java home: D:\Program Files\Java\jdk1.6.0_20\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

修改仓库存放的位置(Default: ~/.m2/repository),找到apache-maven-3.0.4\conf下的settings.xml
找到localRepository,修改内容如下:
<localRepository>E:/maven/repository</localRepository>

开始helloworld之旅
源码一定要放在项目的src\main\java目录下,测试代码要放在项目的src\test\java目录下。
先来写一个普通类Hello.java
package com.naxsu.maven.hello;
public class Hello{
public String sayHello(String name) {
return "hello:"+name;
}
public static void main(String[] args) {
System.out.println("hello world!");
}
public String hello() {
return "hello";
}
}
再写一个测试类
package com.naxsu.maven.hello;
import org.junit.*;
import static junit.framework.Assert.*;
import com.naxsu.maven.hello.*;
public class TestHello{
@Test
public void testHello() {
Hello h = new Hello();
assertEquals(h.sayHello("zs"),"hello:zs");
}
}
每个项目都要有一个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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<!--
groupId:项目的项目组(包名)
artifactId:项目中的哪一个模块(命名推荐用【项目名】-【模块名】)
version:版本
-->
<groupId>com.naxsu.maven.hello</groupId>
<artifactId>hello-first</artifactId>
<version>SNAPSHOT-0.0.1</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>


编辑源码:E:\maven\workspace\01>mvn compile
编辑成功后,在项目下会生成一个target目录,该目录下classes目录,classes目录下就有编辑好的Hello.class文件(带包的,也就是target\classes\com\naxsu\maven\hello\Hello.class)

同理,执行E:\maven\workspace\01>mvn test命令会把测试代码编辑好,不过不是在class目录下,而是在test-classes。
还有surefire和surefire-reports,这主要是存放测试报告的文件夹。

删除编辑好的target目录及子目录、文件:
E:\maven\workspace\01>mvn clean

编辑及打包
E:\maven\workspace\01>mvn clean package

编辑及打包,并且把打包好的jar安装到本地仓库
E:\maven\workspace\01>mvn clean install

新建一个项目,调用刚才打包放到本地仓库的类。
World.java
package com.naxsu.maven.world;
import com.naxsu.maven.hello.*;
public class World{
public static void main(String[] args) {
Hello h = new Hello();
h.sayHello("李四");
h.hello();
}
}
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.naxsu.maven.hello</groupId>
<artifactId>hello-second</artifactId>
<version>SNAPSHOT-0.0.1</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.naxsu.maven.hello</groupId>
<artifactId>hello-first</artifactId>
<version>SNAPSHOT-0.0.1</version>
<scope>compile</scope>
</dependency>

</dependencies>
</project>

编辑也是可以通过的。

使用maven运行java的main方法
在World.java的main方法中加入一个
System.out.println("测试main方法输出!");

编辑好,Maven的exec插件允许你运行Java项目中的main方法
mvn exec:java -Dexec.mainClass="com.example.Main" -Dexec.args="arg0 arg1 arg2" -Dexec.classpathScope=runtime

E:\maven\workspace\02>mvn exec:java -Dexec.mainClass="com.naxsu.maven.world.Worl
d"


更多资料,请访问www.naxsu.com

猜你喜欢

转载自itway.iteye.com/blog/1479645