為什么要使用私庫
maven默認去遠程中央倉庫下載JAR包的,訪問國外網絡相當慢,如果團隊每個人都去下載一遍無疑是網絡的浪費,當然也可以添加國內的鏡像,如阿里的比較穩定,但如果想添加遠程不存在的像第三方公司的JAR包就比較麻煩。
所以,使用私庫,第一,開源包只要有一個人下載過其他人就不需要再下載了,直接從私庫下載即可。第二,可以用來管理第三方公司的或者遠程倉庫不存在的JAR包,或者公司不開源的JAR包。
推薦國內穩定的鏡像,如阿里的
http://maven.aliyun.com/nexus/content/groups/public/
nexus下載安裝
首先去sonatype官網下載nexus包,要下載開源免費版的OSS版,即Open Source Software。
https://www.sonatype.com/nexus-repository-oss
下載最新的3.X的版本,這里以windows為例進行下載。
下載后點擊bin目錄中的啟動文件即可,默認的端口是8081,訪問路徑是/,也可以去配置文件中修改,這里以默認。
啟動后,打開localhost:8081,nexus默認的用戶名是admin/admin123
默認安裝有以下這幾個倉庫,在控制台也可以修改遠程倉庫的地址,第三方倉庫等。
Maven配置
修改maven主目錄conf/setting.xml配置文件。
添加nexus認證的用戶名和密碼配置信息。
<servers>
<server>
<id>nexus-releases</id>
<privateKey>admin</privateKey>
<passphrase>admin123</passphrase>
</server>
<server>
<id>nexus-snapshots</id>
<privateKey>admin</privateKey>
<passphrase>admin123</passphrase>
</server>
</servers>
添加mirror鏡像
<mirrors>
<mirror>
<id>Nexus</id>
<mirrorOf>*</mirrorOf>
<name>Nexus</name>
<url>http://127.0.0.1:8081/repository/maven-public/</url>
</mirror>
</mirrors>
添加私庫
<profiles>
<profile>
<id>Nexus</id>
<repositories>
<repository>
<id>Nexus</id>
<name>Nexus</name>
<url>http://127.0.0.1:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>Nexus</id>
<name>Nexus</name>
<url>http://127.0.0.1:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
激活私庫
<activeProfiles>
<activeProfile>Nexus</activeProfile>
</activeProfiles>
發布到私庫
在pom配置文件中添加
<!-- nexus-releases nexus-snapshots與settings.xml中server下的id對應 -->
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Releases Repository</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshots Repository</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
在項目上使用命令mvn deploy打包就能發布到私庫。
推薦去我的博客閱讀更多:
2.Spring MVC、Spring Boot、Spring Cloud 系列教程
3.Maven、Git、Eclipse、Intellij IDEA 系列工具教程
覺得不錯,別忘了點贊+轉發哦!