分類: java 2014-10-16 11:46 577人閱讀 評論(0) 收藏 舉報 html
一、 爲何使用Nexus java
若是沒有私服,咱們所需的全部構件都須要經過maven的中央倉庫和第三方的Maven倉庫下載到本地,而一個團隊中的全部人都重複的從maven倉庫下 載構件無疑加大了倉庫的負載和浪費了外網帶寬,若是網速慢的話,還會影響項目的進程。不少狀況下項目的開發都是在內網進行的,鏈接不到maven倉庫怎麼 辦呢?開發的公共構件怎麼讓其它項目使用?這個時候咱們不得不爲本身的團隊搭建屬於本身的maven私服,這樣既節省了網絡帶寬也會加速項目搭建的進程, 固然前提條件就是你的私服中擁有項目所需的全部構件。windows
二、Nexus下載 網絡
下載地址:http://www.sonatype.org/nexus/goapp
三、Nexus啓動 maven
我下載的是zip包,解壓後進入\nexus-2.1.2-bundle\nexus-2.1.2\bin\jsw\,根據操做系統類型選擇文件夾,我選的是windows-x86-32文件夾,進入後可看到以下所示bat文件。測試
雙擊console-nexus.bat運行。遊覽器中輸入http://127.0.0.1:8081/nexus/,出現圖(2)所示就表明nexus已經啓動成功。url
8081爲默認的端口號,要修改端口號可進入nexus-2.1.2-bundle\nexus-2.1.2\conf\打開nexus.properties文件,修改application-port屬性值就能夠了。spa
默認的用戶名和密碼:admin/admin123,登陸後看到圖(3)所示:操作系統
圖(3)
四、Nexus倉庫
nexus的倉庫類型分爲如下四種:
group: 倉庫組
hosted:宿主
proxy:代理
virtual:虛擬
首次登錄nexus後能夠看到如下一個倉庫組和多個倉庫。
Public Repositories: 倉庫組
3rd party: 沒法從公共倉庫得到的第三方發佈版本的構件倉庫
Apache Snapshots: 用了代理ApacheMaven倉庫快照版本的構件倉庫
Central: 用來代理maven中央倉庫中發佈版本構件的倉庫
Central M1 shadow: 用於提供中央倉庫中M1格式的發佈版本的構件鏡像倉庫
Codehaus Snapshots: 用來代理CodehausMaven 倉庫的快照版本構件的倉庫
Releases: 用來部署管理內部的發佈版本構件的宿主類型倉庫
Snapshots:用來部署管理內部的快照版本構件的宿主類型倉庫
4.一、建立Nexus宿主倉庫
在Repositories選項頁的菜單欄上點擊Add按鈕會出現以下所示,選擇要添加的倉庫類型。
這裏我點擊添加宿主類型的倉庫,在倉庫列表的下方會出現新增倉庫的配置,以下所示:
點擊save按鈕後就會在倉庫列表中看到剛纔新增的倉庫。
4.二、建立Nexus代理倉庫
點擊菜單欄上的Add按鈕後選擇Proxy Repository,看到以下所示配置界面:
4.三、建立Nexus倉庫組
倉庫組和倉庫關係是一對多的關係,一個倉庫組能夠指向多個倉庫。
點擊菜單欄上的Add按鈕選擇Repository Group就能夠看到倉庫組的配置界面,以下所示:
點擊save後就可在倉庫列表中看到新增的倉庫組了,項目中若是要下載構件的話,配置文件中通常都用倉庫組的URL。
五、修改Maven配置文件從Nexus下載構件
1) 若是想對操做系統的全部用戶統一配置maven,則只需修改maven\conf\setting.xml 文件就能夠了,若是隻想對用戶單獨配置maven,只需將conf\setting.xml文件複製到C:\Documents and Settings\Administrator\.m2文件夾下(我這裏假設系統裝在c盤,用戶爲Administrator)。
2) 打開setting.xml文件,能夠看到以下代碼:
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository
<localRepository></localRepository>
-->
[java] view plaincopy
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository
<localRepository></localRepository>
-->
表示若是不設置localRepository,maven會默認將本地倉庫建到/.m2/repository文件夾下。
設置localRepository以下代碼所示:
<localRepository>F:\myCenterRepository</localRepository>
[html] view plaincopy
<localRepository>F:\myCenterRepository</localRepository>
表示在myCenterRepository文件夾下創建本地倉庫。我的建議不要採用默認的倉庫地址,由於項目若是不少的話,那麼本地倉庫所佔的磁盤空間就比較多了,因此指定倉庫地址到其餘盤符,更方便管理。
5.二、在POM文件中配置Nexus倉庫
在項目的pom文件中添加以下代碼:
<repositories>
<repository>
<id>nexus</id>
<name>my-nexus-repository</name>
<url>http://127.0.0.1:7788/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>my-nexus-repository</name>
<url>http://127.0.0.1:7788/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
[xml] view plaincopy
<repositories>
<repository>
<id>nexus</id>
<name>my-nexus-repository</name>
<url>http://127.0.0.1:7788/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>my-nexus-repository</name>
<url>http://127.0.0.1:7788/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
在pom文件中配置只對當前項目有效,而實際開發中不可能在每一個項目中重複配置信息,因此不建議在pom文件中配置。
5.三、在setting.xml文件中配置Nexus倉庫
1)maven提供了profile來配置倉庫信息,以下所示:
<profiles>
<profile>
<id>myprofile</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
[xml] view plaincopy
<profiles>
<profile>
<id>myprofile</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
2) 激活profile
<activeProfiles>
<activeProfile>myprofile</activeProfile>
</activeProfiles>
[xml] view plaincopy
<activeProfiles>
<activeProfile>myprofile</activeProfile>
</activeProfiles>
3)配置鏡像
<mirrors>
<mirror>
<id>nexus</id>
<url>http://127.0.0.1:7788/nexus/content/groups/public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
[xml] view plaincopy
<mirrors>
<mirror>
<id>nexus</id>
<url>http://127.0.0.1:7788/nexus/content/groups/public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
這裏配置mirrorOf的值爲*,表明maven的全部訪問請求都會指向到Nexus倉庫組。
六、部署構件到Nexus倉庫
6.一、maven部署
1) 修改pom文件
在pom文件中添加以下配置:
<distributionManagement>
<repository>
<id>my-nexus-releases</id>
<url>http://127.0.0.1:7788/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>my-nexus-snapshot</id>
<url>http://127.0.0.1:7788/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
[xml] view plaincopy
<distributionManagement>
<repository>
<id>my-nexus-releases</id>
<url>http://127.0.0.1:7788/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>my-nexus-snapshot</id>
<url>http://127.0.0.1:7788/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
2)在setting.xml文件中添加認證信息:
<servers>
<server>
<id>my-nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>my-nexus-snapshot</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
[xml] view plaincopy
<servers>
<server>
<id>my-nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>my-nexus-snapshot</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
上面的配置中我用的是超級管理員的帳戶,開發項目中能夠改成具備部署構件權限的用戶就能夠了。
3)執行部署
測試的構件項目信息以下:
<groupId>com.ez</groupId>
<artifactId>TestJar</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>TestJar</name>
[xml] view plaincopy
<groupId>com.ez</groupId>
<artifactId>TestJar</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>TestJar</name>
從上面的信息中能夠看出構件爲發佈版本,因此部署構件的話會自動部署至releases倉庫中。
在命令行中執行:mvn clean deploy
若是以前沒用執行過該命令,maven會自動到中央倉庫中下載部署所需的插件。最後在命令行中看到以下所示就表明構件已經部署成功。
到nexus的releases倉庫中查看剛剛部署好的構件信息以下所示:
若是部署失敗的要檢查一下用戶是否有部署的權限,目標倉庫是否容許部署,是否缺乏部署所需的構件。
6.二、手動部署
手動部署構件則更爲簡單了,在nexus的倉庫列表中點擊要部署的目標倉庫,而後點擊Artifact Upload選項卡看到下圖所示:
經過以上配置運用Nexus搭建的私服基本上能夠用了,更多配置需求可參考Nexus book.
Nexus book下載地址:http://download.csdn.net/detail/yaoqinzhou1943/4589583