由於看到網上諸多的公共Maven倉庫,以及本身管理Jar包的不方便,趁此次搭建畢設項目環境,把建立Maven項目涉及到的步驟理一遍。 apache
安裝Nexus(以Windows32平臺舉例): 服務器
下載Nexus:http://www.sonatype.org/nexus/;app
解壓nexus-2.6.4-02-bundle.zip至任意目錄;eclipse
NOTE:Nexus在一個名爲Jetty的servlet容器中運行,它使用一個名爲Tanuki Java Service Wrapper的本地服務包裹器啓動。這個服務包裹器能夠被配置成以Windows服務或Unix守護線程的形式運行Nexus。要啓動Nexus,你須要爲你的平臺找到合適的啓動腳本。要查看可用平臺的列表,查看${NEXUS_HOME}/bin/jsw目錄的內容。【查看原文】maven
所以,首先須要安裝Wrapper:${NEXUS_HOME}/bin/jsw下找到相應平臺,以管理員身份啓動install-nexus.bat。ide
管理員身份(否則會有wrapper | OpenSCManager failed - Access is denied. (0x5)之類錯誤)運行CMD,cd進入${NEXUS_HOME}/bin/目錄,nexus start啓動nexus。學習
控制檯提示wrapper | nexus started.後,便可進入http://localhost:8081/nexus/進入nexus平臺,默認用戶名:admin,密碼:admin123。this
看圖快速瞭解Nexus: url
具體的能夠參考網上資料,好比這裏。spa
安裝Maven:
Nexus是用來管理構建(Artifacts)的,Maven是使用這些構建並搭建項目的;
下載Maven:http://maven.apache.org/download.cgi;
解壓安裝包至任意目錄,在環境變量中加入M2_HOME=D:/apache-maven-3.0.4(解壓目錄),PATH中加入bin路徑:%M2_HOME%\bin;
CMD輸入命令:mvn -version檢查是否正確安裝;
接下來就是配置Maven了,這些操做都沒有使用命令行,打開位於%M2_HOME%/conf/目錄下的setting.xml文件;
主要更改如下幾項,【】符號標註,注意看註釋:
<!-- 【localRepository】 | The path to the local repository maven will use to store artifacts. | | Default: ~/.m2/repository --> <localRepository>D:\Maven3.0.4\repository</localRepository>
<!-- 【servers】 | This is a list of authentication profiles, keyed by the server-id used within the system. | Authentication profiles can be used whenever maven must make a connection to a remote server. |--> <servers> <!-- server | Specifies the authentication information to use when connecting to a particular server, identified by | a unique name within the system (referred to by the 'id' attribute below). | | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are | used together. | --> <server> <id>nexus-releases</id> <username>admin</username> <password>admin123</password> </server>
<!-- 【mirrors】 | This is a list of mirrors to be used in downloading artifacts from remote repositories. | | It works like this: a POM may declare a repository to use in resolving certain artifacts. | However, this repository may have problems with heavy traffic at times, so people have mirrored | it to several places. | | That repository definition will have a unique id, so we can create a mirror reference for that | repository, to be used as an alternate download site. The mirror site will be the preferred | server for that repository. |--> <mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. |【注意:下面的url換成了xx;其實上面咱們安裝nexus的時候是在本機,可是實際應用時會放在服務器,因此這裏用x提醒下。】 --> <mirror> <id>nexus-releases</id> <mirrorOf>*</mirrorOf> <url>http://xx.xx.xx.xx:8080/nexus/content/groups/public</url> </mirror> </mirrors>
<!-- 還有各類profiles、activeProfiles -->
到這裏,Maven基本上是就緒狀態了,接下來咱們爲MyEclipse安裝Maven插件,而後在MyEclipse支持下,創建Maven項目。
安裝MyEclipse的Maven插件(MyEclipse版本爲10,不一樣版本可能會有小差別):
咱們採用MyEclipse的在線安裝功能安裝Maven插件;
打開MyEclipse10,MyEclipse -> MyEclipse Configuration Center -> Software,點擊add site,Name:Maven,URL:http://m2eclipse.sonatype.org/sites/m2e;
安裝完成以後,能夠在MyEclipse的Preferences中看到以下選項:
如圖進行相關設置以後,MyEclipse中的Maven插件就生效了,接下來咱們就來建一個Maven項目。
MyEclipse中創建Maven項目:
仍是看圖吧:
接下來,就是利用pom.xml構建你的項目,項目創建會有一個默認的pom.xml,能夠先學習下這個文件。pom.xml中的repositories指向剛纔搭建的nexus倉庫,dependencies則制定須要的具體jar包。初步能夠認爲pom.xml就是用來管理你須要的jar包,而不用一個個去網上搜,或者在本地本身手動維護一堆jar包;固然這只是初步,Maven還有其餘方面的強大功能,不明覺厲。
後記:
從搭建Nexus開始,到安裝Maven及MyEclipse插件,而後建立Maven項目,本文基本把這些步驟理了一遍。接下來在具體使用Maven的過程當中確定會有不少問題,包括使用Maven高效搭建管理項目,或者在MyEclipse環境下使用Maven會遇到一些莫名其妙的問題,這些問題但願可以和各位交流,我也會在後續的文章中補充。
若是文章對你有用,請在收藏之餘「頂/贊」一下以示鼓勵吧 (/ω\)