maven repository

在settings.xml中配置远程仓库

我们知道了如何在POM中配置远程仓库,但考虑这样的情况:在一个公司内部,同时进行这3个项目,而且以后随着这几个项目的结束,越来越多的项目会开始;同时,公司内部建立一个Maven仓库。我们统一为所有这些项目配置该仓库,于是不得不为每个项目提供同样的配置。问题出现了,这是重复 

其实我们可以做到只配置一次,在哪里配置呢?就是settings.xml。

不过事情没有那么简单,不是简单的将POM中的<repositories>及<pluginRepositories>元素复制到settings.xml中就可以,setting.xml不直接支持 这两个元素。但我们还是有一个并不复杂的解决方案,就是利用profile,如下:

Xml代码 复制代码
  1. <settings>  
  2.   ...   
  3.   <profiles>  
  4.     <profile>  
  5.       <id>dev</id>  
  6.       <!-- repositories and pluginRepositories here-->  
  7.     </profile>  
  8.   </profiles>  
  9.   <activeProfiles>  
  10.     <activeProfile>dev</activeProfile>  
  11.   </activeProfiles>  
  12.   ...   
  13. </settings>  
[xml] view plain copy
 
  1. <settings>  
  2.   ...  
  3.   <profiles>  
  4.     <profile>  
  5.       <id>dev</id>  
  6.       <!-- repositories and pluginRepositories here-->  
  7.     </profile>  
  8.   </profiles>  
  9.   <activeProfiles>  
  10.     <activeProfile>dev</activeProfile>  
  11.   </activeProfiles>  
  12.   ...  
  13. </settings>  

这里我们定义一个id为dev的profile,将所有repositories以及pluginRepositories元素放到这个profile中,然后,使用<activeProfiles>元素自动激活该profile。这样,你就不用再为每个POM重复配置仓库。

使用profile为settings.xml添加仓库提供了一种用户全局范围的仓库配置。

镜像

如果你的地理位置附近有一个速度更快的central镜像,或者你想覆盖central仓库配置,或者你想为所有POM使用唯一的一个远程仓库(这个远程仓库代理的所有必要的其它仓库),你可以使用settings.xml中的mirror配置。

以下的mirror配置用maven.net.cn覆盖了Maven自带的central:

Xml代码 复制代码
  1. <settings>  
  2. ...   
  3.   <mirrors>  
  4.     <mirror>  
  5.       <id>maven-net-cn</id>  
  6.       <name>Maven China Mirror</name>  
  7.       <url>http://maven.net.cn/content/groups/public/</url>  
  8.       <mirrorOf>central</mirrorOf>  
  9.     </mirror>  
  10.   </mirrors>  
  11. ...   
  12. </settings>  
[xml] view plain copy
 
  1. <settings>  
  2. ...  
  3.   <mirrors>  
  4.     <mirror>  
  5.       <id>maven-net-cn</id>  
  6.       <name>Maven China Mirror</name>  
  7.       <url>http://maven.net.cn/content/groups/public/</url>  
  8.       <mirrorOf>central</mirrorOf>  
  9.     </mirror>  
  10.   </mirrors>  
  11. ...  
  12. </settings>  

这里唯一需要解释的是<mirrorOf>,这里我们配置central的镜像,我们也可以配置一个所有仓库的镜像,以保证该镜像是Maven唯一使用的仓库:

Xml代码 复制代码
  1. <settings>  
  2. ...   
  3.   <mirrors>  
  4.     <mirror>  
  5.       <id>my-org-repo</id>  
  6.       <name>Repository in My Orgnization</name>  
  7.       <url>http://192.168.1.100/maven2</url>  
  8.       <mirrorOf>*</mirrorOf>  
  9.     </mirror>  
  10.   </mirrors>  
  11. ...   
  12. </settings>  
[xml] view plain copy
 
  1. <settings>  
  2. ...  
  3.   <mirrors>  
  4.     <mirror>  
  5.       <id>my-org-repo</id>  
  6.       <name>Repository in My Orgnization</name>  
  7.       <url>http://192.168.1.100/maven2</url>  
  8.       <mirrorOf>*</mirrorOf>  
  9.     </mirror>  
  10.   </mirrors>  
  11. ...  
  12. </settings>  

关于更加高级的镜像配置,可以参考:http://maven.apache.org/guides/mini/guide-mirror-settings.html。

http://blog.csdn.net/joewolf/article/details/4876604

原文地址:https://www.cnblogs.com/softidea/p/5914757.html