用nexus搭建maven私服 配置

背景:最近項目準備使用maven+nexus來管理構件,也方便項目的構建,所以老大就讓俺研究研究怎麼用nexus搭建maven私服

環境:win7-64、nexus-2.2、maven-3.0.4、jdk1.6.0_10

一、用admin用戶登陸nexus

nexus的下載和安裝都很簡單,網上也有很多介紹,本文就不重複了。主要介紹一下安裝之後的配置

nexus的配置需要用admin角色完成,默認的密碼是admin123,進入nexus首頁之後,點擊右上角,進行登錄



然後就可以在左邊的菜單中進行配置了



二、爲nexus配置代理服務器

由於這臺機器需要通過代理才能訪問外網,所以首先要配置代理服務器,在Administration-->Server中進行配置



配置之後,nexus才能連上central repository,如果私服所在機器可以直接上外網,則可以省略這一步

三、配置repository

在Views/Repositories-->Repositories裏進行配置



nexus裏可以配置3種類型的倉庫,分別是proxy、hosted、group

proxy是遠程倉庫的代理。比如說在nexus中配置了一個central repository的proxy,當用戶向這個proxy請求一個artifact,這個proxy就會先在本地查找,如果找不到的話,就會從遠程倉庫下載,然後返回給用戶,相當於起到一箇中轉的作用

hosted是宿主倉庫,用戶可以把自己的一些構件,deploy到hosted中,也可以手工上傳構件到hosted裏。比如說oracle的驅動程序,ojdbc6.jar,在central repository是獲取不到的,就需要手工上傳到hosted裏

group是倉庫組,在maven裏沒有這個概念,是nexus特有的。目的是將上述多個倉庫聚合,對用戶暴露統一的地址,這樣用戶就不需要在pom中配置多個地址,只要統一配置group的地址就可以了

nexus裝好之後,已經初始化定義了一些repository,我們熟悉之後,就可以自行刪除、新增、編輯

右邊那個Repository Path可以點擊進去,看到倉庫中artifact列表。不過要注意瀏覽器緩存。我今天就發現,明明構件已經更新了,在瀏覽器裏卻看不到,還以爲是BUG,其實是被瀏覽器緩存了

四、配置Central Repository的proxy

最關鍵的一個配置,可能就是Central Repository的proxy配置,因爲大部分的構件,都是要通過這個proxy得到的



在安裝完nexus之後,這個proxy是預置的,需要做的就是把Download Remote Indexes改爲true,這樣nexus纔會從central repository下載索引,才能在nexus中使用artifact search的功能

網絡上有一些其他公開的maven倉庫,可以用同樣的辦法,在nexus中設置proxy,但是並不是所有maven倉庫,都提供了nexus index,這種情況下,就無法建立索引了

五、配置hosted repository

一般會配置3個hosted repository,分別是3rd party、Snapshots、Releases,分別用來保存第三方jar(典型的比如ojdbc6.jar),項目組內部的快照、項目組內部的發佈版



這裏並沒有什麼特別的配置,只是Deployment Policy這個選項,一般Snapshots會配置成允許,而Releases和3rd party會設置爲禁止

六、配置group repository

前面說過,group其實是一個虛擬的倉庫,通過對實體倉庫(proxy、hosted)進行聚合,對外暴露一個統一的地址



這裏要注意的是,放到左邊的倉庫,纔是會被聚合的倉庫。我昨天一直搞錯了,把倉庫都放到右邊,結果group什麼都沒有聚合到,是一個空的倉庫。。。

七、配置用戶密碼

在Security-->Users中配置,在deployment用戶上點擊右鍵,選擇Set Password,然後設置一個密碼,做這個操作是爲了後面提交做準備



八、在用戶機器上配置settings.xml

經過前面的7個步驟,nexus就配置好了,接下來需要在每個開發人員的開發機器上進行配置了

配置文件在%USER_HOME%/.m2/settings.xml

Xml代碼 複製代碼
  1. <?xml version="1.0"encoding="UTF-8"?> 
  2.  
  3. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"  
  4.           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> 
  6.   
  7.   <servers> 
  8.    
  9.     <server> 
  10.         <id>nexus-snapshots</id> 
  11.         <username>deployment</username> 
  12.         <password>deployment</password> 
  13.     </server> 
  14.  
  15.   </servers> 
  16.    
  17.   <mirrors> 
  18.    
  19.     <mirror> 
  20.         <id>nexus</id> 
  21.         <name>internal nexus repository</name> 
  22.         <url>http://10.78.68.122:9090/nexus-2.1.1/content/groups/public/</url> 
  23.         <mirrorOf>central</mirrorOf> 
  24.     </mirror> 
  25.      
  26.   </mirrors> 
  27.    
  28. </settings> 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"   
  4.           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">  
  6.    
  7.   <servers>  
  8.     
  9.     <server>  
  10.         <id>nexus-snapshots</id>  
  11.         <username>deployment</username>  
  12.         <password>deployment</password>  
  13.     </server>  
  14.   
  15.   </servers>  
  16.     
  17.   <mirrors>  
  18.     
  19.     <mirror>  
  20.         <id>nexus</id>  
  21.         <name>internal nexus repository</name>  
  22.         <url>http://10.78.68.122:9090/nexus-2.1.1/content/groups/public/</url>  
  23.         <mirrorOf>central</mirrorOf>  
  24.     </mirror>  
  25.       
  26.   </mirrors>  
  27.     
  28. </settings>  


這裏只配置了2個元素<mirrors>和<servers>

首先這裏配置了一個id爲nexus的鏡像倉庫,地址是前面配置的public group的URL,然後鏡像目標是central

maven裏的超級pom,裏面配置了這樣一段:

Xml代碼 複製代碼 收藏代碼
  1. <repositories> 
  2.     <repository> 
  3.       <id>central</id> 
  4.       <name>Central Repository</name> 
  5.       <url>http://repo.maven.apache.org/maven2</url> 
  6.       <layout>default</layout> 
  7.       <snapshots> 
  8.         <enabled>false</enabled> 
  9.       </snapshots> 
  10.     </repository> 
  11.   </repositories> 
  12.  
  13.   <pluginRepositories> 
  14.     <pluginRepository> 
  15.       <id>central</id> 
  16.       <name>Central Repository</name> 
  17.       <url>http://repo.maven.apache.org/maven2</url> 
  18.       <layout>default</layout> 
  19.       <snapshots> 
  20.         <enabled>false</enabled> 
  21.       </snapshots> 
  22.       <releases> 
  23.         <updatePolicy>never</updatePolicy> 
  24.       </releases> 
  25.     </pluginRepository> 
  26.   </pluginRepositories> 
  27. [html] view plaincopy
    1. <distributionManagement>    
    2.    <!-- 讓Maven知道當我要發佈release版本或者snapshot版本是需要發佈到哪個地址 -->  
    3.    <repository>  
    4.     <id>nexus-releases</id>  
    5.     <name>NexusRelease Repository</name>  
    6.     <url>http://127.0.0.1:8081/nexus/content/repositories/releases/</url>  
    7.   </repository>  
    8.       <snapshotRepository>    
    9.            <id>nexus-snapshots</id>    
    10.            <name>nexus distribution snapshot repository</name>    
    11.            <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url>    
    12.        </snapshotRepository>    
    13.          
    14.    </distributionManagement>  

  1. <repositories>  
  2.     <repository>  
  3.       <id>central</id>  
  4.       <name>Central Repository</name>  
  5.       <url>http://repo.maven.apache.org/maven2</url>  
  6.       <layout>default</layout>  
  7.       <snapshots>  
  8.         <enabled>false</enabled>  
  9.       </snapshots>  
  10.     </repository>  
  11.   </repositories>  
  12.   
  13.   <pluginRepositories>  
  14.     <pluginRepository>  
  15.       <id>central</id>  
  16.       <name>Central Repository</name>  
  17.       <url>http://repo.maven.apache.org/maven2</url>  
  18.       <layout>default</layout>  
  19.       <snapshots>  
  20.         <enabled>false</enabled>  
  21.       </snapshots>  
  22.       <releases>  
  23.         <updatePolicy>never</updatePolicy>  
  24.       </releases>  
  25.     </pluginRepository>  
  26.   </pluginRepositories>  


因此,當本地的maven項目,找不到需要的構件(包括jar包和插件)的時候,默認會到central裏獲取

所以我們剛剛配置的鏡像倉庫,id也是central,這樣本地maven項目對central repository的請求,就會轉到鏡像倉庫上,也就是我們設置的nexus私服上

由於我們在項目的pom裏,不會再配置其他的<repositories>和<pluginRepositories>元素,所以只要配置一個central的mirror,就足以阻止所有的外網訪問。如果pom中還配置了其他的外網倉庫,比如jboss repository等,可以把<mirrorOf>改爲*

至於<servers>元素,是因爲我們把項目內部的構件上傳到nexus的倉庫中時,nexus會進行權限控制,所以這裏需要設置權限相關的信息。注意這裏的<id>nexus-snapshots</id>,和後面maven工程裏的pom設置是一致的

由於我們這裏已經屏蔽了對外網倉庫的請求,所以就不需要配置代理服務器了,如果需要配置代理服務器,可以用<proxies>元素

九、配置maven項目的pom文件

下面是簡化後的pom文件:

Xml代碼 複製代碼 收藏代碼
  1. <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  3.  
  4.     <modelVersion>4.0.0</modelVersion> 
  5.     <groupId>com.huawei.inoc.wfm.task</groupId> 
  6.     <artifactId>task-sla</artifactId> 
  7.     <version>0.0.1-SNAPSHOT</version> 
  8.     <name>task-sla</name> 
  9.  
  10.     <!-- 配置部署的遠程倉庫 --> 
  11.     <distributionManagement> 
  12.         <snapshotRepository> 
  13.             <id>nexus-snapshots</id> 
  14.             <name>nexus distribution snapshot repository</name> 
  15.             <url>http://10.78.68.122:9090/nexus-2.1.1/content/repositories/snapshots/</url> 
  16.         </snapshotRepository> 
  17.     </distributionManagement> 
  18.  
  19. </project> 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   
  4.     <modelVersion>4.0.0</modelVersion>  
  5.     <groupId>com.huawei.inoc.wfm.task</groupId>  
  6.     <artifactId>task-sla</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <name>task-sla</name>  
  9.   
  10.     <!-- 配置部署的遠程倉庫 -->  
  11.     <distributionManagement>  
  12.         <snapshotRepository>  
  13.             <id>nexus-snapshots</id>  
  14.             <name>nexus distribution snapshot repository</name>  
  15.             <url>http://10.78.68.122:9090/nexus-2.1.1/content/repositories/snapshots/</url>  
  16.         </snapshotRepository>  
  17.     </distributionManagement>  
  18.   
  19. </project>  


這裏配置了<distributionManagement>元素,其中的<id>nexus-snapshots</id>,與前面說的settings.xml中的<servers>元素中的配置必須一致

配置這個的目的,是當執行maven deploy時,才知道要將生成的構件部署到哪個遠程倉庫上,注意這裏的URL填的就不是public group的地址:
http://10.78.68.122:9090/nexus-2.1.1/content/groups/public/

而是snapshots的地址:
http://10.78.68.122:9090/nexus-2.1.1/content/repositories/snapshots/

但是在nexus中,snapshots也是聚合到public group裏的,所以開發人員A提交到snapshots的構件,開發人員B也可以從public group裏獲取到

十、eclipse中的設置

經過前面的配置,已經可以通過命令行進行maven操作了。不過實際開發中,一般都是使用eclipse的m2e插件,所以還需要對eclipse進行一些額外的配置

在Preferences-->Maven-->User Settings中,點擊Update Settings,加載剛纔我們對settings.xml的更改



然後在Maven Repositories視圖裏,可以看到倉庫的情況



可以看到,從超級pom繼承來的central被置灰了,不可用,後面的mirrored by nexus表示對該倉庫的所有請求,都會轉到鏡像nexus中

十一、nexus的目錄結構

nexus會安裝在%USER_HOME%/sonatype-work/nexus下,有以下目錄



其中的storage目錄,就是構件實際存放的地址了

 

這聽說是一篇華爲工作的牛人發出來的,藉此來看看,學習下。歡迎大家補充問題and補充答案,如果我自己明白了會附上答案部分。

問題一、當我在nexus UI上傳額外的jar包或者插件時,爲什麼在eclipse裏更新不下來,而要我把之前所有的包刪了,重複更新所有纔可以?

問題二、eclipse更新索引時,提示Unable to update index for nexus|http://127.0.0.1:8081/nexus/content/groups/public/,這又是爲啥?

問題三、超級pom文件裏有這麼一段,id爲cengtral的倉庫中url爲什麼是http://repo.maven.apache.org/maven2,而不是直接私服上這個地址http://127.0.0.1:8081/nexus/content/groups/public

  1. <repository> 
  2.       <id>central</id> 
  3.       <name>Central Repository</name> 
  4.       <url>http://repo.maven.apache.org/maven2</url> 
  5.       <layout>default</layout> 
  6.       <snapshots> 
  7.         <enabled>false</enabled> 
  8.       </snapshots> 
  9.     </repository> 
  10.   </repositories> 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章