要想在eclipse中使用maven,需要安裝maven plugin for eclipse。
安裝遠程插件的方法:打開eclipse,點擊“幫助(help)”-->"軟件更新(Software Updates)"-->"查找安裝(Find and Install)"-->"尋找安裝新特性(Search for new features to install)"-->"新建遠程站點(New Remote Site)",輸入名字爲maven,url中輸入http://m2eclipse.codehaus.org/update-dev/,確定。
這時勾上我們剛纔建立的maven,點下一步,根據提示安裝,OK。安裝完成之後重新啓動eclipse。
安裝插件完成之後,打開eclipse,點擊Windows-->Preferences,可以看到Maven的選項,選中maven,可以看到右邊有一個關於maven的設置,其中有一項是user settings file,指向user configuration file,通常是${user.homt}/.m2/settings.xml。
所以,需要在settings.xml中進行配置,我只配置了Local Repository,配置方法見前面寫的maven配置。
然後,選中一個項目,運行爲maven compile,這時可以看到maven在下載所需要的依賴庫,再到我們配置的本地Repository中看一下,可以發現,maven下載的庫文件都放到這裏來了。
但是,還有一個問題,就是提示:
1) com.sun:tools:jar:1.5.0
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=com.sun -DartifactId=tools \
-Dversion=1.5.0 -Dpackaging=jar -Dfile=/path/to/file
Path to dependency:
在網上找了找資料,發現是因爲pom.xml中有這麼一段:
<dependency></dependency>
- <dependency>
- <groupId>com.sun</groupId>
- <artifactId>tools</artifactId>
- <version>1.5.0</version>
- <scope>system</scope>
- <systemPath>${java.home}/../lib/tools.jar</systemPath>
- </dependency>
在上面這段代碼中,用到了${java.home},所以,我找到eclipse的安裝目錄,找到下面的eclipse.ini,
在eclipse的啓動參數中加了以下一句:-vm
E:\jdk1.6\bin\javaw.exe,表明你想使用的JDK。重新啓動eclipse,發現仍然不行,不過報的是JRE的錯,查看eclipse的JRE設置,發現用的是另外一個JRE,更改爲E:\jdk1.6\jre1.6,重啓eclipse後,錯誤解決。
注:有的時候,即使在eclipse.ini中設置了啓動時用的JDK,實際上用的JDK也未必是這個,可以用一個工具procexp.exe來查看到底用的是哪個。