配置MAVEN远程仓库

在POM中配置远程仓库

前面我们看到超级POM配置了ID为central的远程仓库,我们可以在POM中配置其它的远程仓库。这样做的原因有很多,比如你有一个局域网的远程仓库,使用该仓库能大大提高下载速度,继而提高构建速度,也有可能你依赖的一个jar在central中找不到,它只存在于某个特定的公共仓库,这样你也不得不添加那个远程仓库的配置。

这里我配置一个远程仓库指向中央仓库的中国镜像:

Xml代码

  1. <project>
  2. ...
  3. <repositories>
  4. <repository>
  5. <id>maven-net-cn </id>
  6. <name>Maven China Mirror </name>
  7. <url>http://maven.net.cn/content/groups/public/ </url>
  8. <releases>
  9. <enabled>true </enabled>
  10. </releases>
  11. <snapshots>
  12. <enabled>false </enabled>
  13. </snapshots>
  14. </repository>
  15. </repositories>
  16. <pluginRepositories>
  17. <pluginRepository>
  18. <id>maven-net-cn </id>
  19. <name>Maven China Mirror </name>
  20. <url>http://maven.net.cn/content/groups/public/ </url>
  21. <releases>
  22. <enabled>true </enabled>
  23. </releases>
  24. <snapshots>
  25. <enabled>false </enabled>
  26. </snapshots>
  27. </pluginRepository>
  28. </pluginRepositories>
  29. ...
  30. </project>


我们先看一下的配置,你可以在它下面添加多个,每个都有它唯一的ID,一个描述性的name,以及最重要的,远程仓库的url。此外,true告诉Maven可以从这个仓库下载releases版本的构件,而false告诉Maven不要从这个仓库下载snapshot版本的构件。禁止从公共仓库下载snapshot构件是推荐的做法,因为这些构件不稳定,且不受你控制,你应该避免使用。当然,如果你想使用局域网内组织内部的仓库,你可以激活snapshot的支持。

关于的更详细的配置及相关解释,请参考:http://www.sonatype.com/books/maven-book/reference_zh/apas02s08.html。

至于,这是配置Maven从什么地方下载插件构件(Maven的所有实际行为都由其插件完成)。该元素的内部配置和完全一样,不再解释。

 

在settings.xml中配置远程仓库

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

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

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

Xml代码   
  1. <settings>
  2. ...
  3. <profiles>
  4. <profile>
  5. <id>dev </id>
  6. </profile>
  7. </profiles>
  8. <activeProfiles>
  9. <activeProfile>dev </activeProfile>
  10. </activeProfiles>
  11. ...
  12. </settings>


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

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

 

镜像

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

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

Xml代码   
  1. <settings>  
    ...  
      <mirrors>  
        <mirror>  
          <id>maven-net-cn</id>  
          <name>Maven China Mirror</name>  
          <url>http://maven.net.cn/content/groups/public/</url>  
          <mirrorOf>central</mirrorOf>  
        </mirror>  
      </mirrors>  
    ...  
    </settings>

核心配置(配置多个中央下载仓库中心)

  1. <mirrors>
  2. <!-- mirror
  3. | Specifies a repository mirror site to use instead of a given repository. The repository that
  4. | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
  5. | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
  6. |
  7. <mirror>
  8. <id>mirrorId</id>
  9. <mirrorOf>repositoryId</mirrorOf>
  10. <name>Human Readable Name for this Mirror.</name>
  11. <url>http://my.repository.com/repo/path</url>
  12. </mirror>
  13. -->
  14. <!--默认的中央仓库-->
  15. <mirror>
  16. <id>mirrorId </id>
  17. <mirrorOf>repositoryId </mirrorOf>
  18. <name>Human Readable Name for this Mirror. </name>
  19. <url>http://my.repository.com/repo/path </url>
  20. </mirror>
  21. <!--自定义添加-->
  22. <mirror>
  23. <id>repo2 </id>
  24. <mirrorOf>central </mirrorOf>
  25. <name>Human Readable Name for this Mirror. </name>
  26. <url>http://repo2.maven.org/maven2/ </url>
  27. </mirror>
  28. <mirror>
  29. <id>ui </id>
  30. <mirrorOf>central </mirrorOf>
  31. <name>Human Readable Name for this Mirror. </name>
  32. <url>http://uk.maven.org/maven2/ </url>
  33. </mirror>
  34. <mirror>
  35. <id>ibiblio </id>
  36. <mirrorOf>central </mirrorOf>
  37. <name>Human Readable Name for this Mirror. </name>
  38. <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/ </url>
  39. </mirror>
  40. <mirror>
  41. <id>jboss-public-repository-group </id>
  42. <mirrorOf>central </mirrorOf>
  43. <name>JBoss Public Repository Group </name>
  44. <url>http://repository.jboss.org/nexus/content/groups/public </url>
  45. </mirror>
  46. <!--访问慢的网址放入到后面-->
  47. <mirror>
  48. <id>CN </id>
  49. <name>OSChina Central </name>
  50. <url>http://maven.oschina.net/content/groups/public/ </url>
  51. <mirrorOf>central </mirrorOf>
  52. </mirror>
  53. <mirror>
  54. <id>net-cn </id>
  55. <mirrorOf>central </mirrorOf>
  56. <name>Human Readable Name for this Mirror. </name>
  57. <url>http://maven.net.cn/content/groups/public/ </url>
  58. </mirror>
  59. <mirror>
  60. <id>JBossJBPM </id>
  61. <mirrorOf>central </mirrorOf>
  62. <name>JBossJBPM Repository </name>
  63. <url>https://repository.jboss.org/nexus/content/repositories/releases/ </url>
  64. </mirror>
  65. </mirrors>


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

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

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

 

分发构件至远程仓库

mvn install 会将项目生成的构件安装到本地Maven仓库,mvndeploy 用来将项目生成的构件分发到远程Maven仓库。本地Maven仓库的构件只能供当前用户使用,在分发到远程Maven仓库之后,所有能访问该仓库的用户都能使用你的构件。

我们需要配置POM的distributionManagement来指定Maven分发构件的位置,如下:

Xml代码   
  1. <</SPAN>project>    
  2.   ...    
  3.   <</SPAN>distributionManagement>    
  4.     <</SPAN>repository>    
  5.       <</SPAN>id>nexus-releases</</SPAN>id>    
  6.       <</SPAN>name>Nexus Release Repository</</SPAN>name>    
  7.       <</SPAN>url>http://127.0.0.1:8080/nexus/content/repositories/releases/</</SPAN>url>    
  8.     </</SPAN>repository>    
  9.     <</SPAN>snapshotRepository>    
  10.       <</SPAN>id>nexus-snapshots</</SPAN>id>    
  11.       <</SPAN>name>Nexus Snapshot Repository</</SPAN>name>    
  12.       <</SPAN>url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</</SPAN>url>    
  13.     </</SPAN>snapshotRepository>    
  14.   </</SPAN>distributionManagement>    
  15.   ...    
  16. </</SPAN>project>    

Maven区别对待release版本的构件和snapshot版本的构件,snapshot为开发过程中的版本,实时,但不稳定,release版本则比较稳定。Maven会根据你项目的版本来判断将构件分发到哪个仓库。

一般来说,分发构件到远程仓库需要认证,如果你没有配置任何认证信息,你往往会得到401错误。这个时候,如下在settings.xml中配置认证信息:

Xml代码   
  1. <</SPAN>settings>    
  2.   ...    
  3.   <</SPAN>servers>    
  4.     <</SPAN>server>    
  5.       <</SPAN>id>nexus-releases</</SPAN>id>    
  6.       <</SPAN>username>admin</</SPAN>username>    
  7.       <</SPAN>password>admin123</</SPAN>password>    
  8.     </</SPAN>server>    
  9.     <</SPAN>server>    
  10.       <</SPAN>id>nexus-snapshots</</SPAN>id>    
  11.       <</SPAN>username>admin</</SPAN>username>    
  12.       <</SPAN>password>admin123</</SPAN>password>    
  13.     </</SPAN>server>      
  14.   </</SPAN>servers>    
  15.   ...    
  16. </</SPAN>settings>  
需要注意的是,settings.xml中server元素下id的值必须与POM中repository或snapshotRepository下id的值完全一致。将认证信息放到settings下而非POM中,是因为POM往往是它人可见的,而settings.xml是本地的
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值