让Maven项目使用Nexus作为远程仓库的settings.xml配置

让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>   

转载:http://blog.csdn.net/u014527058/article/details/50906343

原文地址:https://www.cnblogs.com/yhcreak/p/6603578.html