介紹前面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實戰》