讓maven項目使用nexus作為遠程倉庫


讓maven項目使用nexus作為遠程倉庫有兩種方式,第一種是在項目的pom.xml中進行更改,讓單個項目使用nexus倉庫;另一種是通過修改maven的配置文件settings.xml進行更改,讓所有項目都使用nexus倉庫。

進入maven安裝目錄的conf文件夾打開,修改settings.xml文件。

1.服務器配置

[html]  view plain  copy
 
  1.   <server>    
  2.       <id>nexus-releases</id>    
  3.       <username>admin</username>    
  4.       <password>admin123</password>    
  5.     </server>    
  6.     <server>    
  7.       <id>nexus-snapshots</id>    
  8.       <username>admin</username>    
  9.       <password>admin123</password>    
  10.     </server>    
  11.   </servers>   

 

 

id:這是Server的ID(不是登錄進來的user),與Maven想要連接上的repository/mirror中的id元素相匹配。

username,password:這兩個元素成對出現,表示連接這個server需要驗證username和password。在nexus中,默認管理員用戶名為admin,密碼為admin123。

這里使用兩個服務器配置,分別對應release和snapshot。

 

2.鏡像

 

 

[html]  view plain  copy
 
  1. <mirrors>     
  2.     <mirror>     
  3.       <id>nexus-releases</id>     
  4.       <mirrorOf>*</mirrorOf>     
  5.       <url>http://localhost:8081/nexus/content/groups/public</url>     
  6.     </mirror>    
  7.     <mirror>     
  8.       <id>nexus-snapshots</id>     
  9.       <mirrorOf>*</mirrorOf>     
  10.       <url>http://localhost:8081/nexus/content/groups/public-snapshots</url>     
  11.     </mirror>     
  12.   </mirrors>     

 

 

id,name:唯一的鏡像標識和用戶友好的鏡像名稱。id被用來區分mirror元素,並且當連接時候被用來獲得相應的證書。

mirrorOf:鏡像所包含的倉庫的Id。例如,指向Maven central倉庫的鏡像(http://repo1.maven.org/maven2/),設置這個元素為central。更多的高級映射例如repo1,repo2 或者*,!inhouse都是可以的。沒必要一定和mirror的id相匹配。在這里mirrorOf項當然應該使用*,以表明是所有倉庫都會被鏡像到指定的地址。

url:鏡像基本的URL,構建系統將使用這個URL來連接倉庫。這里應該添nexus倉庫的地址,地址可以在nexus倉庫頁面中找到。

 

3.配置

 

[html]  view plain  copy
 
  1. <profiles>    
  2.    <profile>    
  3.       <id>nexus</id>    
  4.       <repositories>    
  5.         <repository>    
  6.           <id>nexus-releases</id>    
  7.           <url>http://nexus-releases</url>    
  8.           <releases><enabled>true</enabled></releases>    
  9.           <snapshots><enabled>true</enabled></snapshots>    
  10.         </repository>    
  11.         <repository>    
  12.           <id>nexus-snapshots</id>    
  13.           <url>http://nexus-snapshots</url>    
  14.           <releases><enabled>true</enabled></releases>    
  15.           <snapshots><enabled>true</enabled></snapshots>    
  16.         </repository>    
  17.       </repositories>    
  18.       <pluginRepositories>    
  19.          <pluginRepository>    
  20.                 <id>nexus-releases</id>    
  21.                  <url>http://nexus-releases</url>    
  22.                  <releases><enabled>true</enabled></releases>    
  23.                  <snapshots><enabled>true</enabled></snapshots>    
  24.                </pluginRepository>    
  25.                <pluginRepository>    
  26.                  <id>nexus-snapshots</id>    
  27.                   <url>http://nexus-snapshots</url>    
  28.                 <releases><enabled>true</enabled></releases>    
  29.                  <snapshots><enabled>true</enabled></snapshots>    
  30.              </pluginRepository>    
  31.          </pluginRepositories>    
  32.     </profile>    
  33.   </profiles>    

 

 

profile項代表maven的基本配置。按照maven的一貫尿性,很多xml的配置項都會有一個配置項的復數形式作為父節點,以保證該配置項可以配置多個。在profiles項中,當然也可以配置多個profile,不過在這里配一個就夠了。下面介紹profile項的各個子節點。

id:用來確定該profile的唯一標識。

repositories/repository:用以規定依賴包倉庫的相關信息。在下屬節點中,id就不用多說了;URL是指倉庫地址,這里使用偽造的地址,否則即使設置了mirror,maven也有可能會直接從中央倉庫下載包;releases和snapshots放在一塊說吧,這兩個節點下屬的enable節點用以規定對應的依賴包是否對當前策略有效,假如將snapshot的enable項設為disable,則不會下載snapshot包,這兩個節點還有updatePolicy,checksumPolicy和layout屬性,這里就不多介紹了,有興趣的查查文檔吧。

pluginRepositories/pluginRepository:用以規定插件倉庫的相關信息。其下屬節點與repository的相同,不多說了。

4.當前啟用配置

[html]  view plain  copy
 
  1. <activeProfiles>    
  2.       <activeProfile>nexus</activeProfile>    
  3.   </activeProfiles>    

用以規定當前啟用的配置,將對應profile的ID加入到這一項即可使profile生效。
 

5.完整配置

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"     
  3.           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  4.           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">    
  5.     
  6.   <pluginGroups></pluginGroups>    
  7.   <proxies></proxies>    
  8.     
  9.   <servers>    
  10.       <server>    
  11.       <id>nexus-releases</id>    
  12.       <username>admin</username>    
  13.       <password>admin123</password>    
  14.     </server>    
  15.     <server>    
  16.       <id>nexus-snapshots</id>    
  17.       <username>admin</username>    
  18.       <password>admin123</password>    
  19.     </server>    
  20.   </servers>    
  21.     
  22.   <mirrors>     
  23.     <mirror>     
  24.       <id>nexus-releases</id>     
  25.       <mirrorOf>*</mirrorOf>     
  26.       <url>http://localhost:8081/nexus/content/groups/public</url>     
  27.     </mirror>    
  28.     <mirror>     
  29.       <id>nexus-snapshots</id>     
  30.       <mirrorOf>*</mirrorOf>     
  31.       <url>http://localhost:8081/nexus/content/groups/public-snapshots</url>     
  32.     </mirror>     
  33.   </mirrors>     
  34.      
  35.   <profiles>    
  36.    <profile>    
  37.       <id>nexus</id>    
  38.       <repositories>    
  39.         <repository>    
  40.           <id>nexus-releases</id>    
  41.           <url>http://nexus-releases</url>    
  42.           <releases><enabled>true</enabled></releases>    
  43.           <snapshots><enabled>true</enabled></snapshots>    
  44.         </repository>    
  45.         <repository>    
  46.           <id>nexus-snapshots</id>    
  47.           <url>http://nexus-snapshots</url>    
  48.           <releases><enabled>true</enabled></releases>    
  49.           <snapshots><enabled>true</enabled></snapshots>    
  50.         </repository>    
  51.       </repositories>    
  52.       <pluginRepositories>    
  53.          <pluginRepository>    
  54.                 <id>nexus-releases</id>    
  55.                  <url>http://nexus-releases</url>    
  56.                  <releases><enabled>true</enabled></releases>    
  57.                  <snapshots><enabled>true</enabled></snapshots>    
  58.                </pluginRepository>    
  59.                <pluginRepository>    
  60.                  <id>nexus-snapshots</id>    
  61.                   <url>http://nexus-snapshots</url>    
  62.                 <releases><enabled>true</enabled></releases>    
  63.                  <snapshots><enabled>true</enabled></snapshots>    
  64.              </pluginRepository>    
  65.          </pluginRepositories>    
  66.     </profile>    
  67.   </profiles>    
  68.     
  69.   <activeProfiles>    
  70.       <activeProfile>nexus</activeProfile>    
  71.   </activeProfiles>    
  72.      
  73. </settings>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM