分享

maven仓库配置

 shiguoyiLove 2015-04-07

1.两类仓库

1.1本地仓库(local repository)

可以自定义本地仓库的位置,修改${user.home}/.m2/settings.xml :

Xml代码 
  1. <settings>  
  2.   ...  
  3.   <localRepository>D:\java\repository</localRepository>  
  4.   ...  
  5. </settings>  

你还可以在运行时指定本地仓库位置:

mvn clean install -Dmaven.repo.local=/home/juven/myrepo/



1.2远程仓库(Remote repositories)

可以使用访问协议 such as file:// and http://运行maven所需jar包都是从本地仓库,引用在本地仓库没有时将触发从远处次仓库下载,并保存到本地。


2.Maven仓库概览

 

2.1 没有Maven仓库下的开发环境


 

2.2 具有团队内部远程仓库的开发环境

 

下面将就图2.2构建一个团队共享的maven repository

3.Maven仓库工具---Artifactory

3.1下载最新Artifactory,下面就我下载的Artifactory2.6.1配置作简单介绍。

1.双击artifactory.bat即可启动artifactory服务。访问地址http://localhost:8081/artifactory/可以看到服务的管理界面。

 2.也可以在Tomcat部署Artifactory. 

    Startup the Servlet Container VM with -Dartifactory.home=$ARTIFACTORY_HOME, pointing to the location of your Artifactory home folder (If you do not specify this property it will default to${user.home}/.artifactory).

Alternatively, you can also set an ARTIFACTORY_HOME environment variable to point to your Artifactory home folder.
      Make Sure the folder is writable  by the user running the Servlet Container.
Artifactory will try to create the folder on startup if it does not exist.

Make sure the Artifactory configuration file $ARTIFACTORY_HOME/etc/artifactory.config.xml and the log4j configuration file $ARTIFACTORY_HOME/etc/log4j.properties exist in their respective locations.

Deploy the artifactory.war file into the container.



3.2.maven的远程仓库地址设置

默认的远程仓库

我安装了maven-2.0.10,我可以找到这个文件:${M2_HOME}/lib/maven-2.0.10-uber.jar ,打开该文件,能找到超级POM:\org\apache\maven\project\pom-4.0.0.xml ,它是所有Maven POM的父POM,所有Maven项目继承该配置,你可以在这个POM中发现如下配置:

Xml代码  收藏代码
  1. <repositories>  
  2.   <repository>  
  3.     <id>central</id>  
  4.     <name>Maven Repository Switchboard</name>  
  5.     <layout>default</layout>  
  6.     <url>http://repo1./maven2</url>  
  7.     <snapshots>  
  8.       <enabled>false</enabled>  
  9.     </snapshots>  
  10.   </repository>  
  11. </repositories>  

上为超级POM配置了ID为central的远程仓库,如果pom.xml中未配置仓库,默认的将使用这个central的超级仓库。

3.2.1在POM中配置远程仓库

使用artifactory在POM中配置其它的远程仓库。


  1. <project>  
  2.   .....  
  3. <repositories>  
  4.     <repository>  
  5.       <id>my-repo</id>  
  6.       <name>my repository</name>  
  7.       <url>http://localhost:8080/artifactory/my-repo/</url>  
  8.       <releases>  
  9.         <enabled>true</enabled>  
  10.       </releases>  
  11.       <snapshots>  
  12.         <enabled>true</enabled>  
  13.       </snapshots>  
  14.     </repository>  
  15.   </repositories>  
  16.   
  17.   <pluginRepositories>  
  18.     <pluginRepository>  
  19.       <id>my-repo</id>  
  20.       <name>my repository</name>  
  21.       <url>http://localhost:8080/artifactory/my-repo/</url>  
  22.       <releases>  
  23.         <enabled>true</enabled>  
  24.       </releases>  
  25.       <snapshots>  
  26.         <enabled>true</enabled>  
  27.       </snapshots>  
  28.     </pluginRepository>  
  29.     <pluginRepository>  
  30.       <name>oss.</name>  
  31.       <id>oss.</id>  
  32.       <url>http://oss./content/groups/public</url>  
  33.     </pluginRepository>  
  34.   </pluginRepositories>  
  35.   ...  
  36. </project>  



3.2.2在settings.xml中配置远程仓库

除了在pox.xml配置仓库,setting文件可以作为一个全局的配置,但并不是简单的将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>  

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



3.3上传构件至远程仓库

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

Xml代码  收藏代码
  1. <project>    
  2.   ...    
  3.   <distributionManagement>    
  4.     <repository>    
  5.       <id>my-repo</id>    
  6.       <name>Release Repository</name>    
  7.       <url>http://localhost:8081/artifactory/my-repo</url>    
  8.     </repository>    
  9.    
  10.   ...    
  11. </project>    


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

Xml代码  收藏代码
  1. <settings>    
  2.   ...    
  3.   <servers>    
  4.     <server>    
  5.       <id>my-repo</id>    
  6.       <username>admin</username>    
  7.       <password>password</password>    
  8.     </server>       
  9.   ...    
  10. </settings>  

需要注意的是,settings.xml中server元素下id的值必须与POM中 repository或snapshotRepository下id的值完全一致。将认证信息放到settings下而非POM中,是因为POM往往是它 人可见的,而settings.xml是本地的。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多