使用nexus搭建maven私服
國內訪問maven倉庫速度渣渣,公司訪問外網速度不快,即使用阿里雲鏡像效果也不佳。在局域網內搭建Maven私服,除了能從私服加速下載jar包,還能將內部通用模塊發佈在私服上供其他同事使用。對內部項目部署很有幫助。
安裝和啓動nexus
下載nexus-2.14.2-01-bundle.tar.gz
https://www.sonatype.com/download-oss-sonatype
# 添加用戶
adduser nexus
passwd nexus
su nexus
cd /home
mkdir nexus
cd nexus
tar xvzf nexus-2.14.2-01-bundle.tar.gz
cd nexus-2.14.2-01/bin
#啓動nexus
./nexus start
瀏覽器中輸入http://127.0.0.1:8081/nexus/
nexus的maven倉庫配置
開啓遠程索引
新搭建的neuxs環境只是一個空的倉庫,需要手動和遠程中心庫進行同步,nexus默認是關閉遠程索引下載,最重要的一件事情就是開啓遠程索引下載。登陸nexus系統,默認用戶名密碼爲admin/admin123
- 點擊左邊Administration菜單下面的Repositories,找到右邊倉庫列表中的Apache Snapshots,Central,然後在configuration下把Download Remote Indexes修改爲true
- 倉庫上分別右鍵,選擇Repari Index,這樣Nexus就會去下載遠程的索引文件。
這樣設置以後, Nexus會自動從遠程中央倉庫下載索引文件, 爲了檢驗索引文件自動下載是否生效,可以卻換到Browse Index查看
增加阿里雲鏡像
Repositories –> Add –>ProxyRepository
阿里的maven鏡像:
http://maven.aliyun.com/nexus/content/groups/public/
Checksum Policy 可以改爲ignore
Repositories –> Public Repositories
將右側欄全部加入左側欄即可
項目maven配置
maven的全局配置文件
修改maven的settings.xml配置文件,如C:\Users\xieyue.m2\settings.xml
<!--deploy發佈項目到私服的用戶密碼-->
<servers>
<server>
<id>releases</id>
<username>deployment</username>
<password>xxx</password>
</server>
<server>
<id>snapshots</id>
<username>deployment</username>
<password>xxx</password>
</server>
</servers>
<!--使用私服倉庫-->
<profiles>
<profile>
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://132.97.8.177:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
項目的POM.xml設置
<!-- mvn deploy的發佈地址 -->
<distributionManagement>
<repository>
<id>releases</id>
<name>Internal Releases</name>
<url>http://132.97.8.177:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Internal Snapshots</name>
<url>http://132.97.8.177:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<!-- 使用本地maven私庫 -->
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://132.97.8.177:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
參考資料
http://books.sonatype.com/nexus-book/reference/installing.html
http://blog.csdn.net/ClementAD/article/details/52670968