这里用的是myeclpise的自带的maven插件。
maven最好配置成你自己安装的那个,myeclipse自带会有些许bug。
用nexus代理maven的中央仓库,setting.xml的配置文件修改内容如下:
<mirrors>
<mirror>
<id>nexusid>
<mirrorof>*mirrorof>
<name>nexus mirrorname>
<url>http://localhost:8081/nexus/content/groups/publicurl>
mirror>
mirrors>
<profiles>
<profile>
<id>nexusid>
<repositories>
<repository>
<id>centralid>
<url>http://centralurl>
<releases><enabled>trueenabled>releases>
<snapshots><enabled>trueenabled>snapshots>
repository>
repositories>
<pluginrepositories>
<pluginrepository>
<id>centralid>
<url>http://centralurl>
<releases><enabled>trueenabled>releases>
<snapshots><enabled>trueenabled>snapshots>
pluginrepository>
pluginrepositories>
profile>
profiles>
<activeprofiles>
<activeprofile>nexusactiveprofile>
activeprofiles>
http://localhost:8081/nexus/content/groups/public 是仓库组的地址。
打下myeclipse新建工程的界面,选择maven下的maven project,打开如下图的向导:
这里我们要选中create a simple project。
点击下一步,填写gav相关内容。
点击完成后,我们就已经成功创建了一个maven project了。
工程的默认目录结构如下:
所有的java源文件都要写在src/main/java目录下,所有的测试类都要写在src/test/java下面,这是maven的默认值。
此时,pom.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.0modelversion>
<groupid>com.testgroupid>
<artifactid>testartifactid>
<version>0.0.1-snapshotversion>
project>
这是最精简的pom.xml了。
这时我们加入junit的支持,新建一个测试类。
在项目上右键maven-add dependency,显示如下界面:
输入junit加入测试支持类库。
在src/test/java下新建一个测试类如下:
package com;
import org.junit.test;
public class testrun
{
@test
public void testa()
{
system.out.println("test a method ");
}
@test
public void testb()
{
system.out.println("test b method ");
}
}
右键run as ----- maven test,进行测试,显示结果如下:
[info] scanning
for projects
[info]
[info] ------------------------------------------------------------------------
[info] building test 0.0.1-snapshot
[info] ------------------------------------------------------------------------
[info]
[info] --- maven-resources-plugin:2.5:resources (
default-resources) @ test ---
[debug] execute contextualize
[warning] using platform encoding (gbk actually) to copy filtered resources, i.e. build is platform dependent!
[info] copying 0 resource
[info]
[info] --- maven-compiler-plugin:2.3.2:compile (
default-compile) @ test ---
[info] nothing to compile - all classes are up to date
[info]
[info] --- maven-resources-plugin:2.5:testresources (
default-testresources) @ test ---
[debug] execute contextualize
[warning] using platform encoding (gbk actually) to copy filtered resources, i.e. build is platform dependent!
[info] copying 0 resource
[info]
[info] --- maven-compiler-plugin:2.3.2:testcompile (
default-testcompile) @ test ---
[info] nothing to compile - all classes are up to date
[info]
[info] --- maven-surefire-plugin:2.10:test (
default-test) @ test ---
[info] surefire report directory: d:\workspace\test\target\surefire-reports
-------------------------------------------------------
t e s t s
-------------------------------------------------------
running com.testrun
test a method
test b method
tests run: 2, failures: 0, errors: 0, skipped: 0, time elapsed: 0.071 sec
results :
tests run: 2, failures: 0, errors: 0, skipped: 0
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 1.847s
[info] finished at: tue sep 11 14:20:59 cst 2012
[info] final memory: 3m/6m
[info] ------------------------------------------------------------------------
ok,一个基本的maven项目已经构建完成。我们还可以将现存的java项目利用myclipse方便的转换成maven project,此部分内容我们在下一节里讨论。