Maven私服的搭建
原文:http://www.cnblogs.com/weiguo21/archive/2013/04/16/3023933.htm
1.為什麼要用nexus(代理倉庫)這樣做有什麼好處?
使用Maven構建和管理專案是非常享受的一件事,我們可以從Maven中央倉庫下載所需要的構件(artifact),
但實際開發中由於種種原因我們需要在架設一個Maven本地代理倉庫,
如:不方便訪問公網、節省頻寬和時間、管理自家的共用artifact等等,比如oracle的驅動包等
本地地理倉庫是我自己取的名字,為了不與下文的本地倉庫想混淆。
獲取構建的流程如下:
使用者使用Maven構建專案時,首先是要直接從本地倉庫獲取的,如果本地倉庫沒有,
它會根據setting.xml的設定去首先嚐試從遠端倉庫下載構件至本地倉庫,然後再使用本地倉庫的構件。
如果setting.xml設定的遠端倉庫是本地代理倉庫,則本地代理倉庫先嚐試從自己的庫中獲取,
如果沒有再從遠端倉庫(比如中央倉庫)下載構件至本地倉庫。
Nexus 是一個優秀的Maven倉庫管理器,還提供了強大的倉庫管理功能,構件搜尋功能,
它基於REST,友好的UI是一個extjs的REST客戶端,它佔用較少的記憶體,基於簡單檔案系統而非資料庫。
這些優點使其日趨成為最流行的Maven倉庫管理器。本文就使用Nexus搭建一個本地代理倉庫。
2.nexus 下載安裝
Nexus網站: http://www.sonatype.org/nexus/
Nexus下載: http://nexus.sonatype.org/downloads/
安裝Nexus之前必須配置好java環境變數
Nexu安裝非常容易,因為它內嵌了Jetty,只要有JRE就能直接執行。
解壓Nexu包會得到兩個目錄nexus-2.7.2-03和sonatype-work,sonatype-work是預設倉庫目錄。
執行、安裝都是使用nexus-2.7.2-03/bin/nexus.bat檔案,它的使用方式:
Usage:nexus.bat { console : start : stop : restart : install : uninstall }
其中console是控制檯方式執行,install是以windows service寄存,
uninstall是下載windows service,start是執行windows service,stop是停止windows service,restart是重啟windows service
也可以下載編譯後的包進行安裝。
Nexus預設埠是8081,可以在nexus-2.7.1-01/conf/nexus.properties中修改,
管理倉庫
管理倉庫需要先登入,預設登入使用者名稱/密碼是admin/admin123。登入後就可以看到左欄選單的管理項。
可以管理倉庫,配置Nexus系統,管理任務,管理使用者,角色,許可權,檢視系統的RSS源,管理及檢視系統日誌,等等。
(1).設定Nexus代理上公網
如果您的本地代理倉庫伺服器沒有公網許可權,Nexus為你留了使用代理的路子,點選右邊選單的“Server”,在右邊找到:
新增你的代理伺服器即可。
(2).倉庫管理
點選左邊導航欄的Repositories,介面的主面板會顯示所有一個所有倉庫及倉庫組的列表,你會看到它們的Type欄位的值有group,hosted,proxy,virtual。這裡我們不關心virtual,只介紹下另外三種類型:
hosted,本地代理倉庫,通常我們會部署自己的構件到這一型別的倉庫。
proxy,代理的遠端倉庫,它們被用來代理遠端的公共倉庫,如maven中央倉庫。
group,倉庫組,用來合併多個hosted/proxy倉庫,通常我們配置maven依賴倉庫組。
修改setting.xml配置Maven的倉庫
Maven安裝後預設的是使用中央倉庫,這是為了能讓Maven開箱即用。而Maven預設的本地倉庫地址為${user.home}/.m2/repository。也就是說,一個使用者會對應的擁有一個本地倉庫。你也可以自定義本地倉庫的位置,修改${user.home}/.m2/settings.xml。
首先需要修改Mirrors
<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. | -->
<mirror>
<id>nexus-service</id>
<mirrorOf>*</mirrorOf>
<name>Nexus</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</mirror>
</mirrors>
在執行 Maven 命令的時候, Maven 還需要安裝一些外掛包,這些外掛包的下載地址也讓其指向本地代理倉庫的地址,修改如下:
<profile>
<id>jdk-1.7</id>
<activation>
<jdk>1.7</jdk>
</activation>
<repositories>
<repository>
<id>nexus</id>
<name>local private nexus</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>local private nexus</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
3.nexus 的一些常見的用法
如何安裝中央倉庫沒有的第三方JAR
(1).通過Nexus UI部署
有時候有個jar檔案你無法從公共Maven倉庫找到,但是你能從其它得到這個jar檔案(甚至是POM),那麼你完全可以將這個檔案部署到Nexus中,使其成為標準流程的一部分。步驟如下:
點選左邊導航欄的"Repository",在右邊的倉庫列表中選擇一個倉庫,如“3rd Party”,然後在頁面下方的tab選擇“Artifact Upload Artifact(s)”,
你會看到構件上傳介面。選擇你要上傳的構件,並指定POM,(或者手工編寫GAV等資訊),最後點選Upload,該構件就直接被部署到了Nexus的"3rd Party"倉庫中.
如圖所示
(2).通過maven部署(主要是針對自己開發的包)
更常見的用例是:團隊在開發一個專案的各個模組,為了讓自己開發的模組能夠快速讓其他人使用,你會想要將snapshot版本的構件部署到Maven倉庫中,
其他人只需要在POM新增一個對於你開發模組的依賴,就能隨時拿到最新的snapshot。
以下的pom.xml配置和settings.xml能讓你通過Maven自動化部署構件:
settings.xml
<settings>
...
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
...
</settings>
pom.xml
<project>
...
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
...
</project>
備註:這裡配置所有的snapshot版本構件部署到Nexus的Snapshots倉庫中,所有的release構件部署到Nexus的Releases倉庫中。
由於部署需要登陸,因為我們在settings.xml中配置對應Repository id的使用者名稱和密碼。
然後,在專案目錄中執行mvn deploy ,你會看到maven將專案構件部署到Nexus中,
瀏覽Nexus對應的倉庫,就可以看到剛才部署的構件。當其他人構建其專案時,Maven就會從Nexus尋找依賴並下載.
5.如何用Maven構建專案
maven編譯專案
清理目錄:mvn clean 編譯專案:mvn compile 打包專案:mvn package 到本地倉庫:mvn install
把maven專案轉換為eclipse專案
mvn eclipse:eclipse
maven執行專案
#使用jetty外掛 mvn jetty:run 啟動成功後可以用瀏覽器訪問預設埠8080 debug專案 mvnDebug jetty:run