介绍前面hello world项目的时候,并没有涉及ide,这样一个简单的项目,使用最简单的编辑器也能完成。但是对一个稍微大一些的项目来说,还是推荐使用ide。这一篇文章我们在m2eclipse帮助下使用eclipse创建maven项目。
1. 创建
使用ide创建一个maven项目非常简单,选择菜单项file->new->other,在弹出的对话框中选择maven下的maven project,然后点击next按钮。

图1
在弹出的new maven project对话框中,使用默认的选项。不要选择create a simple project。然后,点击next按钮。
图2
此时m2eclipse会提示我们选择一个archetype。这里选择maven-archetype-quickstart,再点击next按钮。m2eclipse实际上是使用maven-archetype-plugin插件创建项目。
图3
此时输入groupid,artifactid,version,package。
图4
单击finshi按钮,maven项目就创建完成了。项目目录结构如下:
图5
2. 运行mvn命令
在maven项目或者pom.xml上右击,再点击弹出的快捷菜单选择run as,就能看到常见的maven命令。
图6
选择想要执行的maven命令技能执行相应的构建,同时也能在eclipse的console中看到构建输出。例如:运行mvn test
<code>[info] scanning for projects...</code>
<code>[info]</code>
<code>[info] ------------------------------------------------------------------------</code>
<code>[info] building hello-world 0.0.1-snapshot</code>
<code>[info] --- maven-resources-plugin:2.4.3:resources (default-resources) @ hello-world ---</code>
<code>[info] using 'utf-8' encoding to copy filtered resources.</code>
<code>[info] skip non existing resourcedirectory d:\workspace\javaworkspace\hello-world\src\main\resources</code>
<code>[info] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hello-world ---</code>
<code>[info] nothing to compile - all classes are up to date</code>
<code>[info] --- maven-resources-plugin:2.4.3:testresources (default-testresources) @ hello-world ---</code>
<code>[info] skip non existing resourcedirectory d:\workspace\javaworkspace\hello-world\src\test\resources</code>
<code>[info] --- maven-compiler-plugin:2.3.2:testcompile (default-testcompile) @ hello-world ---</code>
<code>[info] --- maven-surefire-plugin:2.7.1:test (default-test) @ hello-world ---</code>
<code>[info] surefire report directory: d:\workspace\javaworkspace\hello-world\target\surefire-reports</code>
<code> </code>
<code>-------------------------------------------------------</code>
<code>t e s t s</code>
<code>running com.qunar.model.apptest</code>
<code>tests run: 1, failures: 0, errors: 0, skipped: 0, time elapsed: 0.035 sec</code>
<code>results :</code>
<code>tests run: 1, failures: 0, errors: 0, skipped: 0</code>
<code>[info] build success</code>
<code>[info] total time: 4.439s</code>
<code>[info] finished at: sat jan 23 14:11:18 cst 2016</code>
<code>[info] final memory: 6m/15m</code>
如果默认选项张没有我们想要执行的maven命令怎么办?比如,默认带有mvn test,但是没有mvn clean test。我们可以选择maven build来自定义maven 运行命令,在弹出的对话框的goals一项中输入我们想要执行的命令,如clean test,设置一下name,点击run即可。
图7
当你运行时,你可能会遇到下面问题:
<code>[info] --- maven-clean-plugin:2.4.1:clean (default-clean) @ hello-world ---</code>
<code>[info] deleting d:\workspace\javaworkspace\hello-world\target</code>
<code>[info] compiling 1 source file to d:\workspace\javaworkspace\hello-world\target\classes</code>
<code>[info] -------------------------------------------------------------</code>
<code>[error] compilation error :</code>
<code>[error] unable to locate the javac compiler in:</code>
<code>d:\program files (x86)\java\jre7\..\lib\tools.jar</code>
<code>please ensure you are using jdk 1.4 or above and</code>
<code>not a jre (the com.sun.tools.javac.main class is required).</code>
<code>in most cases you can change the location of your java</code>
<code>installation by setting the java_home environment variable.</code>
<code>[info] 1 error</code>
<code>[info] build failure</code>
<code>[info] total time: 1.044s</code>
<code>[info] finished at: sat jan 23 14:54:26 cst 2016</code>
<code>[info] final memory: 5m/15m</code>
<code>[error] failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project hello-world: compilation failure</code>
<code>[error] d:\program files (x86)\java\jre7\..\lib\tools.jar</code>
<code>[error] please ensure you are using jdk 1.4 or above and</code>
<code>[error] not a jre (the com.sun.tools.javac.main class is required).</code>
<code>[error] in most cases you can change the location of your java</code>
<code>[error] installation by setting the java_home environment variable.</code>
<code>[error] -> [help 1]</code>
<code>[error]</code>
<code>[error] to see the full stack trace of the errors, re-run maven with the -e switch.</code>
<code>[error] re-run maven using the -x switch to enable full debug logging.</code>
<code>[error] for more information about the errors and possible solutions, please read the following articles:</code>
<code>[error] [help 1] http://cwiki.apache.org/confluence/display/maven/mojofailureexception</code>
解决方案:这是因为eclipse默认是使用jre作为运行环境,而maven编译需要jdk作为运行环境。
就是在eclipse里设置一个jdk的运行环境,然后将当前项目的运行环境设为jdk运行环境即可。
(1)window -> preferences -> java -> installed jres 修改你的jre,将其location指向你的jdk目录。
(2)点击add按钮,选择standard vm,jre home选择你的jdk目录。点击finish,这时发现多了一个jre,将其勾上,以后新的项目,就默认使用这个jre了。
<code>[info] compiling 1 source file to d:\workspace\javaworkspace\hello-world\target\test-classes</code>
<code>tests run: 1, failures: 0, errors: 0, skipped: 0, time elapsed: 0.014 sec</code>
<code>[info] total time: 3.246s</code>
<code>[info] finished at: sat jan 23 14:58:05 cst 2016</code>
<code>[info] final memory: 11m/27m</code>
来源于:《maven实战》