Maven 灵活使用不同的仓库


Nexus私服让我们可以在企业内部从同一个私服下载Maven仓库里面的dependency和plugin.很方便,不过昨天碰到的一个问题是,有一个仓库加入到Nexus后不能,Maven工程无法通过Nexus私服下载该仓库的dependency。

这个仓库叫做:http://download.osgeo.org/webdav/geotools/

想了一个办法绕过它,通过Maven的~/.m2/settings.xml中配置两个Mirror,在遇到osgeo的时候直接使用该仓库,而在其他情况下用私服。

下面是配置:

  1. <settings>  
  2.   <servers>  
  3.     <server>  
  4.       <id>snapshots</id>  
  5.       <username>admin</username>  
  6.       <password>admin</password>  
  7.     </server>  
  8.   </servers>  
  9.   <pluginGroups>  
  10.     <pluginGroup>org.apache.maven.plugins</pluginGroup>  
  11.   </pluginGroups>   
  12.   <mirrors>  
  13.     <mirror>  
  14.       <!--This sends everything else to /public -->  
  15.       <id>nexus</id>  
  16.       <mirrorOf>external:*,!osgeo</mirrorOf>  
  17.       <url>http://my-proxy:8080/nexus/content/groups/public</url>  
  18.     </mirror>  
  19.     <mirror>  
  20.       <id>osgeo</id>  
  21.       <mirrorOf>osgeo</mirrorOf>  
  22.       <name>Open Source Geospatial Foundation Repository</name>  
  23.       <url>http://download.osgeo.org/webdav/geotools/</url>  
  24.     </mirror>  
  25.   </mirrors>  
  26.   <profiles>  
  27.     <profile>  
  28.       <id>nexus</id>  
  29.       <!--Enable snapshots for the built in central repo to direct -->  
  30.       <!--all requests to nexus via the mirror -->  
  31.       <repositories>  
  32.         <repository>  
  33.           <id>central</id>  
  34.           <url>http://central</url>  
  35.           <releases><enabled>true</enabled></releases>  
  36.           <snapshots><enabled>true</enabled></snapshots>  
  37.         </repository>  
  38.       </repositories>  
  39.       <pluginRepositories>  
  40.         <pluginRepository>  
  41.           <id>central</id>  
  42.           <url>http://central</url>  
  43.           <releases><enabled>true</enabled></releases>  
  44.           <snapshots><enabled>true</enabled></snapshots>  
  45.         </pluginRepository>  
  46.       </pluginRepositories>  
  47.     </profile>  
  48.   </profiles>  
  49.   <activeProfiles>  
  50.     <!--make the profile active all the time -->  
  51.     <activeProfile>nexus</activeProfile>  
  52.   </activeProfiles>  
  53. </settings>  

而在maven工程里面如下配置,注意repository.id必须和mirror.id相等。

  1. <repositories>  
  2.     <repository>  
  3.         <id>local repository</id>  
  4.         <url>http://my-proxy:8080/nexus/content/groups/public/</url>  
  5.     </repository>  
  6.     <repository>  
  7.         <id>osgeo</id>  
  8.         <name>Open Source Geospatial Foundation Repository</name>  
  9.         <url>http://download.osgeo.org/webdav/geotools/</url>  
  10.     </repository>  
  11. </repositories>  

相关内容