備注:搭建nexus私服請參考上一篇文章基於Docker搭建Maven私服Nexus,Nexus詳解
一:將jar發送到nexus私服務器
1.pom.xml文件添加配置
pom.xml文件中的這個版本標簽對應結尾的(RELEASE、SNAPSHOT)將決定分配到私服的那個倉庫<version>1.0.0-RELEASE</version>
<!--配置上傳到私服--> <distributionManagement> <repository> <!--id的名字可以任意取,但是在setting文件中的屬性<server>的ID與這里一致--> <id>releases</id> <!--指向倉庫類型為host(宿主倉庫)的儲存類型為Release的倉庫--> <url>http://116.62.106.24:8081/repository/boris-release/</url> </repository> <snapshotRepository> <id>snapshots</id> <!--指向倉庫類型為host(宿主倉庫)的儲存類型為Snapshot的倉庫--> <url>http://116.62.106.24:8081/repository/boris-snapshot/</url> </snapshotRepository> </distributionManagement>
2.maven settings.xml文件添加配置
<server> <id>releases</id> <username>boris-test-nexus-repository</username> <password>boris</password> </server> <server> <id>snapshots</id> <username>boris-test-nexus-repository</username> <password>boris</password> </server>
然后執行命令:mvn deploy,就成功部署到私服環境了,如下是我測試的一個結果
PS:還有一種上傳jar包的方式,就是直接去nexus管理界面手動上傳
二:從nexus私服下載第三方jar包(有兩種方式)推薦方式二
1.方式一(pom.xml的方式)
<repositories>
<repository>
<id>maven-nexus-group</id>
<url>http://116.62.106.24:8081/repository/boris-group/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven-nexus-group</id>
<url>http://116.62.106.24:8081/repository/boris-group/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
由於nexus私服需要有用戶名和密碼登錄才能訪問,需要在maven settings.xml文件中配置,加入如下內容
<server> <id>maven-nexus-group</id> <username>boris-test-nexus-repository</username> <password>boris</password> </server>
注意上面的server->id的值和pom.xml中repository->id的值一致,通過這個id關聯找到憑證的。
2.方式二(鏡像方式)推薦這種方式
maven settings.xml文件中配置
<server> <id>maven-nexus-group</id> <username>boris-test-nexus-repository</username> <password>boris</password> </server> <profile> <id>nexus</id> <repositories> <repository> <id>maven-nexus-group</id> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>maven-nexus-group</id> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> <!-- 激活 --> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> 注意上面的server->id的值和mirror->id的值需要一致,這樣才能找到對應的憑證。
上面配置會自動從私服中下載構件,但是,如果私服中沒有對應構件,還是會訪問中央倉庫。
所以為了保證只僅僅訪問Nexus,不希望訪問中央倉庫,所以我們還需要配置鏡像,通過鏡像配置,創建一個匹配任何倉庫的鏡像,
鏡像的地址變為私服,交給私服自己訪問中央倉庫緩存,本地只需要從私服上下載構件即可。
<mirror> <id>maven-nexus-group</id> <!-- 此處配置所有的構建均從私有倉庫中下載,*代表所有,也可以寫成central --> <mirrorOf>*</mirrorOf> <name>nexus boris images</name> <url>http://116.62.106.24:8081/repository/boris-group/</url> </mirror>
區別:方式一只針對單個項目有效,方式二針對所有項目有效
關閉匿名訪問,私服我們應該禁止沒有通過用戶名和密碼驗證的用戶從我們的私服下載jar包
阿里雲maven鏡像倉庫
<id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url>