Maven最好配置成你自己安装的那个,MyEclipse自带会有些许Bug。----------------这个我用的也是自己安装的,自带的没有操作过。

用nexus代理Maven的中央仓库,setting.xml的配置文件修改内容如下:------------------------这一步我没有做修改,他可能配置的是中央仓库,我没整
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>Nexus Mirror</name>
<url>http://localhost:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
打下MyEclipse新建工程的界面,选择Maven下的Maven Project,打开如下图的向导:--------------------------------------这步操作完全一样

这里我们要选中create a simple project。
点击下一步,填写GAV相关内容。----------------------------------这一步填写的内容会在pom.xml文件里体现出来

点击完成后,我们就已经成功创建了一个Maven project了。
工程的默认目录结构如下:-----------------注意,这个地方建立的就是一个项目,而非一个web项目。并且他的目录很全。

所有的Java源文件都要写在src/main/java目录下,所有的测试类都要写在src/test/java下面,这是Maven的默认值。
此时,pom.xml里只有默认的属性
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
这时我们加入junit的支持,新建一个测试类。
在项目上右键Maven-Add Dependency,显示如下界面:---------这个菜单我的myeclipse中压根就没有,作者自己的截上我也没找到,这步后面有我自己的做法

输入junit加入测试支持类库。
在src/test/java下新建一个测试类如下:---------------------------------------加了一个测试类
自己加jar包的方法如下(其实也是网上看来的):
方法一:
打开pom.xml文件,加入
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
还是改pom.xml文件,但方式不一样
点击pom.xml文件.我们可以看到下面有7个标签.

Overview:显示maven项目的一些基本信息.
Dependencies:添加jar包的页面,很重要!
Plugins:添加maven插件的页面.比如tomcat-maven-plugin等.
Reporting:从没用过,无视~
Dependency Hierarchy:用于显示jar包的依赖关系.没事的时候可以看看jar包的依赖关系.
Effective
POM:显示maven的编译路径,plugin之类的.也可以无视.
pom.xml:导入jar包的信息,可以在其中进行修改.重要
pom.xml的基本介绍就是这样了.
现在我们添加jar包.
点击Dependencies标签页.
点击Dependencies中的add按钮
输入我们想添加的jar包名字.就会出现下图中所示.
他会把对应名称的所有版本jar包列出来.

我这里加的是junit包,输入后出来好多,要慢慢把自己想要的找出来。另外,这样做还有个好处,就是弄一个,好些依赖包自己会出来的。
右键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] ------------------------------------------------------------------------
本文介绍如何使用MyEclipse IDE配置Maven并创建基本的Maven项目,包括设置本地Maven仓库、通过Nexus代理中央仓库、手动添加依赖如JUnit等步骤。

8464

被折叠的 条评论
为什么被折叠?



