maven私服nexus配置 說明

Nexus是經常使用的私用Maven服務器,通常是公司內部使用。html

Nexus安裝設置

#1.Nexus下載java

下載地址:http://www.sonatype.org/nexus/go/spring

下載後的文件:nexus-2.11.4-01-bundle.zipapache

安裝:直接解壓到某個目錄便可服務器

解壓後,會有兩個目錄:nexus-2.11.4-01app

sonatype-work :私服的默認倉庫maven

1.1 將bin目錄添加到環境變量中ui

D:\JavaDev\nexus-2.11.4\nexus-2.11.4-01\binthis

1.2 配置java文件的路徑url

打開D:\JavaDev\nexus-2.11.4\nexus-2.11.4-01\bin\jsw\conf\wrapper.conf文件

修改wrapper.java.command=java爲你的java.exe文件的路徑

例如:

wrapper.java.command=D:\Program Files\Java\jdk1.7.0_07\bin\java

1.3 啓動nexus

先安裝:nexus install

而後啓動:nexus start

C:\Users\Administrator>nexus install

wrapper | nexus installed.

C:\Users\Administrator>nexus start

wrapper | Starting the nexus service...

wrapper | Waiting to start...

wrapper | nexus started.

C:\Users\Administrator>

Nexus經常使用功能就是:指定私服的中央地址、將本身的Maven項目指定到私服地址、從私服下載中央庫的項目索引、從私服倉庫下載依賴組件、將第三方項目jar上傳到私服供其餘項目組使用。 開啓Nexus服務後訪問url地址http://localhost:8081/nexus/(推薦使用本身的ip地址),以後登陸系統,用戶名密碼分別是:admin/admin123.

2.配置mexus

nexus默認是關閉遠程索引下載功能的。開啓的方式: 點擊Administration菜單下面的Repositories,將這三個倉庫Apache Snapshots,Codehaus Snapshots,Maven Central的 Download Remote Indexes修改成true。而後在這三個倉庫上分別右鍵,選擇Re-index,這樣Nexus就會去下載遠程的索引文件。

3.倉庫管理

https://static.oschina.net/uploads/img/201611/01134218_LIat.jpg

以管理員用戶登錄而後點擊左邊導航菜單Administration下面的Repositories。Nexus提供了三種不一樣的倉庫。 (1)代理倉庫 一個代理倉庫是對遠程倉庫的一個代理。默認狀況下,Nexus自帶了以下配置的代理倉庫: Apache Snapshots 這個倉庫包含了來自於Apache軟件基金會的快照版本。http://people.apache.org/repo/m2-snapshot-repository Codehaus Snapshots 這個倉庫包含了來自於Codehaus的快照版本。 http://snapshots.repository.codehaus.org/ Central Maven Repository 這是中央Maven倉庫(發佈版本)。 http://repo1.maven.org/maven2/ (2)宿主倉庫 一個宿主倉庫是由Nexus託管的倉庫。Maven自帶了以下配置的宿主倉庫。 3rd Party 這個宿主倉庫應該用來存儲在公共Maven倉庫中找不到的第三方依賴。這種依賴的樣例有:你組織使用的,商業的,私有的類庫如Oracle JDBC驅動。 Releases 這個宿主倉庫是你組織公佈內部發布版本的地方。 Snapshots 這個宿主倉庫是你組織發佈內部快照版本的地方。 (3)虛擬倉庫 一個虛擬倉庫做爲Maven 1的適配器存在。Nexus自帶了一個central-m1虛擬倉庫

##4. 管理組 組是Nexus一個強大的特性,它容許你在一個單獨的URL中組合多個倉庫。Nexus自帶了兩個組:public和public-snapshots。public組中組合了三個宿主倉庫:3rd Party, Releases, 和Snapshots,還有中央Maven倉庫。而public-snapshots組中組合了Apache Snapshots和Codehaus Snapshots倉庫。

##5.下載Maven項目索引 項目索引是爲了使用者可以在私服站點查找依賴使用的功能

https://static.oschina.net/uploads/img/201611/01134855_3QMk.jpg

保存後後臺會運行一個任務,點擊菜單欄的Scheduled Tasks選項便可看到有個任務在RUNNING。 下載完成後,Maven索引就能夠使用了,在搜索欄輸入要搜索的項,就能夠查到相關的信息。例如spring-core
就能夠檢索出它的相關信息,包括怎麼配置依賴信息。

https://static.oschina.net/uploads/img/201611/01135002_094T.jpg

只有指定本項目纔在私服下載組件的配置

##1.指定倉庫

<repositories>  
        <repository>  
            <id>nexus</id>  
            <name>nexus</name>  
            <url>http://192.168.1.103:8081/nexus/content/groups/public/</url>  
            <releases>  
                <enabled>true</enabled>  
            </releases>  
            <snapshots>  
                <enabled>true</enabled>  
            </snapshots>  
        </repository>  
    </repositories>

##2.指定插件倉庫

<pluginRepositories>  
        <pluginRepository>  
            <id>nexus</id>  
            <name>nexus</name>  
            <url>http://192.168.1.103:8081/nexus/content/groups/public/</url>  
            <releases>  
                <enabled>true</enabled>  
            </releases>  
            <snapshots>  
                <enabled>true</enabled>  
            </snapshots>  
        </pluginRepository>  
    </pluginRepositories>

若是但願Nexus相對Maven的其餘項目也生效的話。須要修改全局的settings.xml文件

//用戶登陸必填
<server>  
  <id>releases</id>  **//id要與pom.xml中關聯私服的id相同**
  <username>admin</username>  
  <password>admin123</password>  
</server>  
<server>  
  <id>Snapshots</id>  
  <username>admin</username>  
  <password>admin123</password>  
</server>  

<profiles>  
   <profile>  
     <id>nexus</id>  
     <repositories>  
       <repository>  
           <id>nexus</id>  
           <name>local private nexus</name>  
           <url>http://localhost:8081/nexus/content/groups/public</url>  
       </repository>  
     </repositories>  
   </profile>  
   <profile>  
     <id>nexus-snapshots</id>  
     <repositories>  
       <repository>  
           <id>nexus-snapshots</id>  
           <name>local private nexus snapshots</name>  
           <url>http://localhost:8081/nexus/content/groups/public-snapshots</url>  
       </repository>  
     </repositories>  
   </profile>  
 </profiles>  
  
 <activeProfiles>  //激活profile
    <activeProfile>nexus</activeProfile>  
    <activeProfile>nexus-snapshots</activeProfile>  
 </activeProfiles>

##3.部署構件至Nexus

要部署構件至Nexus,在distributionManagement中提供倉庫URL,而後運行mvn deploy。Maven會經過一個簡單的HTTP PUT將項目POM和構件推入至你的Nexus安裝。須要配置你項目POM中distributionManagement部分的repository。

<distributionManagement>  
  <repository>  
    <id>releases</id> ** //要與maven   settings.xml中設置權限的id相同**
    <name>Internal Releases</name>  
    <url>http://localhost:8081/nexus/content/repositories/releases</url>  
  </repository>  
  <snapshotRepository>  
    <id>Snapshots</id>  
    <name>Internal Snapshots</name>  
    <url>http://localhost:8081/nexus/content/repositories/snapshots</url>  
  </snapshotRepository>  
</distributionManagement>

##4.發佈項目

clean deploy

在控制檯發佈成功

而後進入到私服上的倉庫中,看一下確實存在剛剛發佈的項目

https://static.oschina.net/uploads/img/201611/01141850_UAmX.jpg

##5.上傳第三方jar

https://static.oschina.net/uploads/img/201611/01141934_foYl.jpg

#maven之Nexus的配置【setting.xml配置鏡像<mirror>】

啓動了nexus服務後,本地倉庫下載jar包都是從nexus裏下載,若是nexus裏沒有,nexus會與maven的中央倉庫打交道,而後下載對應的依賴包。當關閉了nexus服務後,本地倉庫就會跳過nexus,直接去maven中央倉庫下載依賴包了。

若是咱們不但願本地倉庫直接去maven中央倉庫下載,而必需要從nexus裏下載依賴包,若是nexus裏沒有對應的依賴包,就下載不了。

要實現這種效果,須要在setting裏配置鏡像(<mirror>),讓全部的倉庫都從這個鏡像的url(倉庫)中下載依賴。

setting裏配置了鏡像之後,本地倉庫就再也不與nexus打交道了,而是直接與鏡像中配置的的url(倉庫)進行打交道。

這裏說明一下,無論是在pom.xml裏配置了<repositories>去指向nexus倉庫,仍是在setting.xml裏配置<profile>去指向nexus倉庫,當從本地倉庫去下載依賴的時候,若是nexus裏找不到對應的依賴包,會默認的去maven倉庫裏下載。即便是nexus服務關閉了,本地倉庫仍是會去maven中央倉庫下載對應依賴包。這是maven裏面的默認配置(maven-model-builder-3.3.3.jar裏pom-4.0.0.xml文件配置了id爲central的中央倉庫)。

配置鏡像後,下載依賴包的流程爲: 若是沒有把默認的central倉庫配置到鏡像裏,

<mirror>  
      <id>mirrorId</id>  
      <!--表示訪問哪些工廠時須要使用鏡像-->  
      <mirrorOf>xxx</mirrorOf>  
      <name>Human Readable Name for this Mirror.</name>  
      <url>http://localhost:8081/nexus/content/groups/public/</url>  
    </mirror>

流程以下:

(1)配置了鏡像後,當要下載依賴時,第一步:找到setting.xml中激活的profile下repository裏id爲xxx的配置,而xxx已經配置在裏鏡像裏

(2)這時會去到到鏡像裏的url(倉庫)裏下載依賴

(3)當發現鏡像裏配置的url(倉庫)裏下載不到對應的依賴時,會自動去找到maven中默認的id爲central,url爲中央倉庫地址的repository配置,由於central沒有配置在鏡像中,因此此時能夠直接去到maven中央倉庫下載依賴包。 結果以下圖所示:

https://static.oschina.net/uploads/img/201611/01143742_7mnk.png

若是已經把默認的central倉庫配置到鏡像裏,

</pre><pre name="code" class="html"><mirror>  
      <id>mirrorId</id>  
      <!--表示訪問哪些工廠時須要使用鏡像-->  
      <!--<mirrorOf>xxx,central</mirrorOf> -->  
      <mirrorOf>*</mirrorOf> <!--通常用*號-->  
      <name>Human Readable Name for this Mirror.</name>  
      <url>http://localhost:8081/nexus/content/groups/public/</url>  
    </mirror>

流程以下:

(1)配置了鏡像後,當要下載依賴時,第一步:找到setting.xml中激活的profile下repository裏id爲xxx的配置,而xxx已經配置在裏鏡像裏

(2)這個時候會去到到鏡像裏的url(倉庫)裏下載依賴 (3)當發現鏡像裏配置的url(倉庫)裏下載不到對應的依賴時,會自動去找到maven中默認的id爲central,url爲中央倉庫地址的repository配置,

(4)此時central配置在鏡像中,因此此次是去到到鏡像裏的url(倉庫)裏下載依賴了。而不會去訪問maven中央倉庫了。

https://static.oschina.net/uploads/img/201611/01143353_6C8U.png

相關文章
相關標籤/搜索