Maven仓库核心配置

来源:互联网 发布:淘宝收藏店铺排行榜 编辑:程序博客网 时间:2024/05/18 21:08

一、首先在IDE中配置maven的localrepository与settings.xml的路径:


  

二、然后看settings.xml的核心配置:


  2.1 地仓库必须配置

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <!-- 设置本地仓库路径 -->  
  2. <localRepository>/Users/user/Work/m3/repository</localRepository>  

  2.2 远程仓库之中央仓库可选择性配置

      因为maven本身已经配置了中央仓库(${M2_HOME}/lib/maven-2.0.10-uber.jar ,打开该文件,能找到超级POM:/org/apache/maven/project/pom-4.0.0.xml ,它是所有MavenPOM的父POM,所有Maven项目继承该配置),但是由于服务器在国外,比较慢,所以我们可以配置一个国内的镜像仓库:

  2.3 远程仓库之私服(nexus必须配置,可以配置在settings.xml文件,也可以配置在项目的pom文件中(一般配在pom中,比较灵活)。至于如何在settings.xml中与pom中配置远程仓库,下面会给出示例。

  注:配置远程仓库都是为了从仓库拉取jar用的,与上传(deploy)无关;而且还要注意如下相关配置,表示是否可以拉去snapshots/releases版本的jar包。

  一般规则为这样:如果是外部远程仓库(例如中央仓库)一般releases配true,snapshots配false,防止由于snapshots版本组件由于不稳定引发问题。如果是本地开发时用的私服,我们都配成true即可,方便开发。需要注意,snapshots默认是为false的,如果开发的时候想下载snapshots类型的组件,必须显式配置为true

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <releases>  
  2.     <enabled>true</enabled>  
  3. </releases>  
  4. <snapshots>  
  5.     <enabled>false</enabled>  
  6. </snapshots>  

  2.4  上面配置的中央仓库还有nexus都是拉取组件用的,如果想上传(deploy组件到远程仓库,需要配置两步:

  (1)在项目的pom中配置distributionManagement节点,如下:

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <distributionManagement>  
  2.     <repository>  
  3.         <id>acfunRepo</id>  
  4.         <url>http://nexus.acfun.tv/nexus/content/repositories/AcFun-Release/</url>  
  5.     </repository>  
  6.     <snapshotRepository>  
  7.         <id>acfunRepo</id>  
  8.         <url>http://nexus.acfun.tv/nexus/content/repositories/AcFun-Snapshot/</url>  
  9.     </snapshotRepository>  
  10. </distributionManagement>  

  (2)在settings.xml文件中配置所上传到的仓库的用户名密码(server节点)

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <servers>  
  2.     <server>  
  3.         <id>acfunRepo</id>  
  4.         <username>acfun</username>  
  5.         <password>acfun123</password>  
  6.     </server>  
  7. </servers>  

  这里需要特别注意:server.id 必须与pom中配置distributionManagement下的repository.id相同,如上面都为acfunRepo

 

三、下面介绍如何在settings.xml中与pom中配置远程仓库【接上面的2.3】

  这里的远程仓库一般指的是中央仓库或者私服,是拉取组件用的仓库。二者都既能配置在settings.xml文件,也可以配置在项目的pom文件中。但是,如果配置中央仓库,则建议配置在settings文件中,方便大家共用;如果配置私服(nexus),则建议配置在项目的pom中,方便保持项目的灵活性。

  下面则以私服(nexus)为例,展示分别在settings.xml中与pom中的配置方法:

  3.1 在pom中配置(比较简单)

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <project>  
  2.     ......  
  3.     <repositories>  
  4.         <repository>  
  5.             <id>acfunnexus</id>  
  6.             <url>http://nexus.acfun.tv/nexus/content/groups/public/</url>  
  7.             <layout>default</layout>  
  8.             <releases>  
  9.                 <enabled>true</enabled>  
  10.             </releases>  
  11.             <snapshots>  
  12.                 <enabled>true</enabled>  
  13.             </snapshots>  
  14.         </repository>  
  15.     </repositories>  
  16.     ......  
  17. </project>  

  3.2 在settings.xml中配置(需要两步)

  它不支持pom中的中的<repositories>及<pluginRepositories>元素,所以我们需要利用profileactiveProfile节点来完成,如下:

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  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>   

四、总结


pom中主要仓库配置如下:

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <project>  
  2.     ......  
  3.     <!-- deploy组件的仓库配置,注意id需要与settings.xml中的server节点的id匹配-->   
  4.     <distributionManagement>  
  5.         <repository>  
  6.             <id>acfunRepo</id>  
  7.             <url>http://nexus.acfun.tv/nexus/content/repositories/AcFun-Release/</url>  
  8.         </repository>  
  9.         <snapshotRepository>  
  10.             <id>acfunRepo</id>  
  11.             <url>http://nexus.acfun.tv/nexus/content/repositories/AcFun-Snapshot/</url>  
  12.         </snapshotRepository>  
  13.     </distributionManagement>  
  14.       
  15.     <!-- 拉去组件的私服(nexus)仓库配置-->   
  16.     <repositories>  
  17.         <repository>  
  18.             <id>acfunnexus</id>  
  19.             <url>http://nexus.acfun.tv/nexus/content/groups/public/</url>  
  20.             <layout>default</layout>  
  21.             <releases>  
  22.                 <enabled>true</enabled>  
  23.             </releases>  
  24.             <snapshots>  
  25.                 <enabled>true</enabled>  
  26.             </snapshots>  
  27.         </repository>  
  28.     </repositories>  
  29.     ......  
  30.   
  31. </project>  


settings.xml中的主要配置:

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <settings>  
  2.   
  3.     ......  
  4.       
  5.     <!-- 本地仓库配置-->   
  6.     <localRepository>/Users/user/Work/m3/repository</localRepository>  
  7.       
  8.     <!-- deploy组件的仓库server的身份验证配置,注意id需要与pom中的distributionManagement节点的repository.id匹配-->   
  9.     <servers>  
  10.         <server>  
  11.             <id>acfunRepo</id>  
  12.             <username>acfun</username>  
  13.             <password>acfun123</password>  
  14.         </server>  
  15.     </servers>  
  16.       
  17.     <!-- 配置国内的中央镜像仓库(可以不配置)-->   
  18.     <profiles>    
  19.         <profile>    
  20.             <id>chinaCentralRepo</id>    
  21.             <repositories>  
  22.                 <repository>  
  23.                     <id>maven-net-cn</id>  
  24.                     <name>MavenChinaMirror</name>  
  25.                     <url>http://repo2.maven.org/maven2/</url>  
  26.                     <releases>  
  27.                         <enabled>true</enabled>  
  28.                     </releases>  
  29.                     <snapshots>  
  30.                         <enabled>false</enabled>  
  31.                     </snapshots>  
  32.                 </repository>  
  33.             </repositories>  
  34.             <pluginRepositories>  
  35.                 <pluginRepository>  
  36.                     <id>maven-net-cn</id>  
  37.                     <name>MavenChinaMirror</name>  
  38.                     <url>http://repo2.maven.org/maven2/</url>  
  39.                     <releases>  
  40.                         <enabled>true</enabled>  
  41.                     </releases>  
  42.                     <snapshots>  
  43.                         <enabled>false</enabled>  
  44.                     </snapshots>  
  45.                 </pluginRepository>  
  46.             </pluginRepositories>  
  47.         </profile>  
  48.     </profiles>  
  49.         
  50.     <activeProfiles>    
  51.         <activeProfile>chinaCentralRepo</activeProfile>    
  52.     </activeProfiles>  
  53.       
  54.     ......  
  55. </settings>  


 


0 0