1.nexus 介紹
是開源的,用該框架架設maven私有服務器
2.nexus私服環境搭建
把nexus.war包放到tomcat的webapps下面
瀏覽且登錄
用戶名:admin
密碼:admin123
3.關於中央倉庫注意事項
地址:目前來說:http:repo1.maven.org/maven2/是正真的maven中央倉庫的地址,該地址內置在maven的源碼中 其他的都是鏡像
索引:中央倉庫帶有索引文件以方便用戶對其搜索,完整的索引文件大小爲60M,索引每週更新一次
黑名單:如果某個IP地址惡意的下載中央倉庫內容,例如全公司100臺機器使用一個IP反覆下載,這個IP會進入黑名單,因此稍有規模的使用maven時,應該使用Nexus架設私服
爲什麼要使用nexus私服
1.開發人員過多,是公司IP地址被記錄黑名單,無法下載
2.有些項目組沒有外網,不能遠程訪問倉庫地址,只能在局域網搭建nexus服務器,開發人員連接這臺服務器
4.nexus倉庫 (私有服務器 maven倉庫)
-hosted :宿主倉庫,該倉庫屬於該公司私有的 內部項目的發佈倉庫
Hosted代表宿主倉庫,用來發布一些第三方不允許的組件,比如oracle驅動、比如商業軟件jar包。
(1)3rd part :第三方依賴的倉庫,這個數據通常是由內部人員自行下載之後發佈上去
(2)snapshot:測試版本、鏡像版本 發佈內部的SNAPSHOT模塊的倉庫
(3)release : 髮型的版本 內部的模塊中release模塊的發佈倉庫
-proxy:代理倉庫 從遠程中央倉庫中尋找數據的倉庫
Proxy 代表代理遠程的倉庫,最典型的就是Maven官方中央倉庫、JBoss倉庫等等。如果構建的Maven項目本地倉庫沒有依賴包,那麼就會去這個代理站點去 下載,那麼如果代理站點也沒有此依賴包,就回去遠程中央倉庫下載依賴,這些中央倉庫就是proxy。代理站點下載成功後再下載至本機。
-group: 倉庫組 : 虛擬的概念 可以包含其它的倉庫 組倉庫用來方便我們開發人員進行設置的倉庫
5.下載Maven倉庫索引 手動下載
首先將索引下載到本地,下載地址:nexus-maven-repository-index.zip http://download.csdn.net/detail/pk490525/6520295
解壓索引壓縮包,將裏面內容全部拷貝
關閉當前Nexus私服,打開Nexus目錄%Nexus_Home%\sonatype-work\nexus\indexer\central-ctx,首先刪除當前目錄裏所有內容,然後粘貼所下載的索引,最後啓動Nexus私服,索引生效。
6.修改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>
-->
表示如果不設置localRepository,maven會默認將本地倉庫建到/.m2/repository文件夾下。
設置localRepository如下代碼所示:<localRepository>F:\myCenterRepository</localRepository>
7.在POM.xml文件中配置Nexus倉庫
在項目的pom文件中添加如下代碼:
<repositories>
<repository>
<id>nexus</id>
<name>my-nexus-repository</name>
<url>http://127.0.0.1:8088/nexus-2.9.0/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:8088/nexus-2.9.0/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
8.在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>
2) 激活profile
<activeProfiles>
<activeProfile>myprofile</activeProfile>
</activeProfiles>
3)配置鏡像
<mirrors>
<mirror>
<id>nexus</id>
<url>http://127.0.0.1:8088/nexus-2.9.0/content/groups/public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
9 部署構件到Nexus倉庫
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>
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>
上面的配置中我用的是超級管理員的賬戶,開發項目中可以改爲具有部署構件權限的用戶就可以了。
3)執行部署
測試的構件項目信息如下:
<groupId>com.ez</groupId>
<artifactId>TestJar</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>TestJar</name>
從上面的信息中可以看出構件爲發佈版本,所以部署構件的話會自動部署至releases倉庫中。
在命令行中執行:mvn clean deploy
如果之前沒用執行過該命令,maven會自動到中央倉庫中下載部署所需的插件。最後在命令行中看到如下所示就代表構件已經部署成功。