使用IDEA打包上傳到Nexus3私服
- 一、Nexus是什么
- 二、使用Nexus3搭建maven私服
- 三、IDEA打包上傳到Nexus3私服
- 1.配置 .m2 下的 settings.xml
- 1.配置 IDEA 專案 下的 pom.xml
- 3.配置上傳地址,地址去私服中copy
- 4.其他配置(可忽略)
- 5.settings.xml完整配置(來源網路,僅供參考)
- 6.IDEA打包上傳
一、Nexus是什么
開始在使用Maven時,總是會聽到nexus這個詞,一會兒maven,一會兒nexus,為什么它總是和maven一起被提到呢?
Maven作為一個優秀的構建工具、依賴管理工具、專案資訊管理工具,在進行依賴管理的時候,通過pom.xml里面的
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
來精準定位找到對應的Java類別庫,在這個程序當中我們需要從倉庫去找到對應的jar包引入到我們的專案當中,由此我們解決了合作開發中依賴增多、版本不一致、版本沖突、依賴臃腫等問題,

Maven有本地倉庫和遠程倉庫兩種,當Maven根據坐標尋找構件時,它首先會查看本地倉庫,如果本地倉庫存在此構件,則直接使用;如果本地倉庫不存在此構件,或者需要查看是否有更新的構件版本,Maven會去遠程倉庫查找,發現需要的構件之后,下載到本地倉庫再使用,

說到此,相信大家也明白了,Nexus是一種遠程倉庫,也是私服的一種,
SNAPSHOT
快照版本,在 maven 中 SNAPSHOT 版本代表正式發布(release)的版本之前的開發版本,在 pom 中用 x.y-SNAPSHOT 表示,
RELEASE
發布版本,穩定版本,在 maven 中 RELEASE 代表著穩定的版本,unchange,不可改變的,在 maven 中 SNAPSHOT 與 RELEASE 版本在策略上是完全不同的方式,SNAPSHOT 會根據你的配置不同,頻繁的從遠程倉庫更新到本地倉庫;而 RELEASE 則只會在第一次下載到本地倉庫,以后則會先直接從本地倉庫中尋找,
二、使用Nexus3搭建maven私服
在網上找到幾個參考:
Windows中使用Nexus3搭建maven私服
maven 私服 nexus3.x 搭建 與使用
Maven私服Nexus3.x環境構建操作記錄
三、IDEA打包上傳到Nexus3私服
1.配置 .m2 下的 settings.xml
首先,這個檔案在系統盤當前設備登錄用戶的.m2檔案下,加入認證機制

沒有就去網上copy一個配置好的,自己配置容易出錯
->settings.xml<-
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>maven-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>maven-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>rdc-releases</id>
<username>xxxxxxxxx</username>
<password>xxxxx</password>
</server>
<server>
<id>rdc-snapshots</id>
<username>xxxxxxxxx</username>
<password>xxxx</password>
</server>
</servers>
1.配置 IDEA 專案 下的 pom.xml
<distributionManagement>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://xxxx:port/repository/maven-snapshots/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>maven-snapshots</name>
<url>http://xxxx:port/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
這里標簽中的id對應第一條server中的id ,url取得值可以直接在這里寫,也可以如下圖中settings檔案中的值,name可以自定義
->settings.xml<-
<profile>
<altReleaseDeploymentRepository> http://xxxx:port/repository/maven-snapshots/ </altReleaseDeploymentRepository>
<altSnapshotDeploymentRepository> http://xxxx:port/repository/maven-snapshots/</altSnapshotDeploymentRepository>
</profile>
同樣,上面pom.xml改成如下格式
<distributionManagement>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>${altReleaseDeploymentRepository}</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>maven-snapshots</name>
<url>${altSnapshotDeploymentRepository}</url>
</snapshotRepository>
</distributionManagement>
3.配置上傳地址,地址去私服中copy
<profile>
<!--profile 的 id-->
<id>dev</id>
<repositories>
<repository>
<!--倉庫 id,repositories 可以配置多個倉庫,保證 id 不重復-->
<id>nexus</id>
<!--倉庫地址,即 nexus 倉庫組的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下載 releases 構件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下載 snapshots 構件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件倉庫,maven 的運行依賴插件,也需要從私服下載插件 -->
<pluginRepository>
<!-- 插件倉庫的 id 不允許重復,如果重復后邊配置會覆寫前邊 -->
<id>central</id>
<name>Nexus Plugin Repository</name>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
4.其他配置(可忽略)
比如配置阿里的鏡像,映射阿里中央倉庫(下載jar包快一點)
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
配置本地倉庫(無需聯網使用jar包)
<localRepository>E:\maven_repository</localRepository>
5.settings.xml完整配置(來源網路,僅供參考)
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- 本地倉庫地址 -->
<localRepository>D:\mvn_repo\repository</localRepository>
<!-- 以下配置為上傳jar包配置 -->
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<server>
<!-- id,對應專案里面pom.xml里面distributionManagement配置的id -->
<id>maven-releases</id>
<!-- 登錄nexus的用戶名 -->
<username>admin</username>
<!-- 登錄nexus的密碼 -->
<password>admin123</password>
</server>
<server>
<!-- id,對應專案里面pom.xml里面distributionManagement配置的id -->
<id>maven-snapshots</id>
<!-- 登錄nexus的用戶名 -->
<username>admin</username>
<!-- 登錄nexus的密碼 -->
<password>admin123</password>
</server>
<!-- 配置攔截器mirror登錄的用戶名密碼,他會攔截所有的請求到mirror指定的地址下載jar包 如果只需要去私服下載jar包則只需配置此項 -->
<server>
<!-- id,對應mirror中id -->
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<!-- 以下配置為下載jar包配置 通用 -->
<mirrors>
<!-- 強制讓jar包下載走私服 -->
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://192.168.65.129:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<!-- 對應activeProfiles-activeProfile的內容 -->
<id>nexus</id>
<!-- 倉庫地址 -->
<repositories>
<repository>
<!-- 私服id,覆寫maven-model模塊下的父id,讓maven不走中央倉庫下載,走私服下載 -->
<id>central</id>
<!-- 名字 -->
<name>Nexus</name>
<!-- 私服地址,寫central后,會去mirror里面找 -->
<url>http://central</url>
<!-- 支持releases版本 -->
<releases>
<enabled>true</enabled>
</releases>
<!-- 支持snapshots版本 -->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!-- 插件地址 -->
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Nexus Plugin Repository</name>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<!-- 配置全域的url地址 供上傳jar包時動態獲取 -->
<properties>
<ReleaseRepository>http://192.168.65.129:8081/repository/maven-releases/</ReleaseRepository>
<SnapshotRepository>http://192.168.65.129:8081/repository/maven-snapshots/</SnapshotRepository>
</properties>
</profile>
</profiles>
<!-- 選擇使用的profile -->
<activeProfiles>
<activeProfile>nexus</activeProfile>
<!-- <activeProfile>rdc</activeProfile>-->
</activeProfiles>
</settings>
6.IDEA打包上傳
使用idea打包,打包時會根據pom檔案中version判斷是快照版本還是發布版本,version中0.0.1-SNAPSHOT 帶SNAPSHOT為快照版本,刪掉SNAPSHOT為發布版本,上傳到私服時根據version自動判斷上傳到哪個倉庫
注意,打包時 專案的pom檔案一定要把maven-plugin洗掉掉,不然打包完成后會生成BOOT-INF檔案夾,會造成上傳到私服后,配置依賴后能下載到jar包,但是使用時報包找不到錯誤
<version>1.1-SNAPSHOT</version>
<build>
</build>
然后clean后deploy

看到BUILD SUCCESS的提示則為成功,可以在自己私服上查看

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/316705.html
標籤:其他
上一篇:c語言實作簡單三子棋程式
