1. 程式人生 > >deploy和手動上傳jar包至nexus

deploy和手動上傳jar包至nexus

一、nexus的好處

       Maven預設提供的中央倉庫是在遠端網路服務Appache提供的,這對於我們開發時不合理的。如果我們沒網了或者什麼情況,我們怎麼辦?我們需要的jar包中央庫沒有或者是要收費怎麼辦?也就是說我們隊中央倉庫的依賴性太大。而Nexus私服則可以解決我們這個問題。

        這樣就相當於在我們本地的區域網搭建了一個類似中央倉庫的伺服器,我們開始將中央倉庫的一些資料下載到私伺服器上,然後平時我們的maven專案就是直接訪問區域網內的私服即可,既節省了網路頻寬也會加速專案搭建的程序,這樣對我們開發來說,對公司來說都是非常好的選擇。專案內部和專案與專案之間也可以共享jar包,達到程式碼的最大化利用。

一般有兩種情況需要我們上傳jar包至nexus,一個是本地專案生成的jar包,另一個是我們從第三方拿過來的的中央庫中不存在的jar包,前者採用maven工程deploy的方法,後者採用手動上傳的方式。

二、手動上傳jar包

對於一些第三方jar包,在maven中央庫中下載不到的,我們可以手動上傳至nexus庫中。

首先登陸nexus,進入3rd party,選擇Artifact Upload,如圖:

nexus提供了兩種上傳jar到私服的方式From POM和GAV Parameters。
From POM方式使用場景:上傳的jar還依賴別的jar時,如何把jar和他的依賴一起傳到私服。例如:我們想傳a.jar到私服,但是a.jar還依賴b.jar,假如只傳a.jar到私服,在專案中會因為找不到a.jar的依賴(b.jar)而報錯。
使用參考連結:

https://blog.csdn.net/wabiaozia/article/details/76098727

本專案還未使用到From POM的方式,所以只介紹GAV Parameters方式。

示例中使用的jar包為:geogson.jar包,基本資訊:

<dependency>
  <groupId>com.nsn.geogson</groupId>
  <artifactId>geogson</artifactId>
  <version>1.0.0</version>
</dependency>


匯入成功後,search該包顯示如下:

在本地工程中加入本專案依賴,會自動下載jar包至本地maven倉庫資料夾中。

三、deploy jar至nexus庫

工程pom檔案中,配置釋出構件的基本資訊:

 maven中的倉庫分為兩種,snapshot快照倉庫和release釋出倉庫。snapshot快照倉庫用於儲存開發過程中的不穩定版本,release正式倉庫則是用來儲存穩定的發行版本。定義一個元件/模組為快照版本,只需要在pom檔案中在該模組的版本號後加上-SNAPSHOT即可(注意這裡必須是大寫),如下:

<groupId>cc.mzone</groupId>
<artifactId>m1</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>

 maven會根據模組的版本號(pom檔案中的version)中是否帶有-SNAPSHOT來判斷是快照版本還是正式版本。如果是快照版本,那麼在mvn deploy時會自動釋出到快照版本庫中,而使用快照版本的模組,在不更改版本號的情況下,直接編譯打包時,maven會自動從映象伺服器上下載最新的快照版本。如果是正式釋出版本,那麼在mvn deploy時會自動釋出到正式版本庫中,而使用正式版本的模組,在不更改版本號的情況下,編譯打包時如果本地已經存在該版本的模組則不會主動去映象伺服器上下載。

配置deploy的地址:

 <!-- 設定deploy的地址 -->
    <distributionManagement>
        <repository>
            <id>release</id>                    <!--庫的ID-->
            <name>user release resp</name>   <!--庫的名字-->
            <url>http://192.168.1.11:9083/nexus/content/repositories/releases/</url>
        </repository>

        <snapshotRepository>
            <id>snapshots</id>
            <name>user snapshot</name>
            <url>http://192.168.1.11:9083/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

distributionManagement配置參考:     https://blog.csdn.net/yeguxin/article/details/77110622

<distributionManagement> 中的 <repository> && <snapshotsrepository>各自的含義

  • maven倉庫分為兩種 release釋出倉庫(<repository>) 和 snapshot快照倉庫(<snapshotsrepository>)
  • snapshot快照倉庫用於儲存開發過程中的不穩定版本
  • release正式倉庫用來儲存穩定的發行版本
  • 定義一個元件/模組為快照版本  只需要在pom檔案中的該模板的版本號後面加上 -SNAPSHOT就可以了.  注意:必須是大寫
  • maven會根據模組的版本號(pom檔案中的<version>版本號</version>)中是否帶有-SNAPSHOT來判斷這個是快照版本還是正式版本.
  • 如果是快照版本:
  • 在mvn deploy時會自動釋出到快照版本庫中.
  • 而使用快照版本的模組,在不更改版本號的情況下,直接編譯打包時,maven會自動從映象伺服器上下載最新的快照版本
  • 如果是正式釋出版本:

  • 那麼在mvn deploy時會自動釋出到正式版本庫中,
  • 而使用正式版本的模組,在不更改版本號的情況下,編譯打包時,如果本地已經存在該版本的模組則使用本地的而不是主動去映象伺服器上下載

本地maven資料夾conf目錄下,setting.xml檔案中配置nexus的賬號和密碼:

<?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">
<!--設定自己的maven本地倉庫-->
<localRepository>D:\maven\repo</localRepository>

    <pluginGroups/>
    <proxies/>
    <servers> 
      <server> 
        <id>central</id> <!--這個ID要與下面的repository中的ID一致-->
        <username>dev</username> <!--nexus中配置的使用者名稱密碼-->
        <password>dev123</password> 
      </server> 
      <server> 
        <id>snapshots</id> 
        <username>dev</username> 
        <password>dev123</password> 
      </server> 
      <server> 
        <id>releases</id> 
        <username>dev</username> 
        <password>dev123</password> 
      </server> 
    </servers>
    <mirrors><!--從nexus下載依賴地址-->
        <mirror>
            <id>nexus-public</id>
            <mirrorOf>central</mirrorOf>
            <name>central repository</name>
            <url>http://192.168.1.11:9083/nexus/content/groups/public/</url>
        </mirror>
    </mirrors>

    <profiles>
        <profile> 
            <id>nexus</id> 
            <repositories> 
                <repository> 
                    <id>central</id> <!--正式倉庫id-->
                    <!--name隨便-->
                    <name>Nexus Release Snapshot Repository</name> 
                    <!--地址是nexus中repository(Releases/Snapshots)中對應的地址-->
                    <url>http://192.168.1.11:9083/nexus/content/repositories/releases</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases> 
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots> 
                </repository>

                <repository>
                    <id>snapshots</id>
                    <url>http://192.168.1.11:9083/nexus/content/repositories/snapshots</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </repository>

            </repositories> 
            <pluginRepositories> <!--外掛倉庫地址,各節點的含義和上面是一樣的-->
                <pluginRepository> 
                    <id>central</id> 
                    <name>Nexus Release Snapshot Repository</name> 
                    <url>http://192.168.1.11:9083/nexus/content/repositories/releases</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases> 
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository> 
                <pluginRepository> 
                    <id>snapshots</id>
                    <url>http://192.168.1.11:9083/nexus/content/repositories/snapshots</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </pluginRepository> 
            </pluginRepositories> 
        </profile>
       </profiles>
    <!--啟用配置-->
    <activeProfiles>
        <activeProfile>nexus</activeProfile> <!--profile下的id-->
    </activeProfiles>
</settings>

需要注意的是,settings.xml中server元素下id的值必須與POM中repository或snapshotRepository下id的值完全一致。maven在處理髮布時會根據id查詢使用者名稱稱和密碼進行登入和檔案的上傳發布。

最後點選deploy即可釋出。