首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

《Maven实战》学习札记(3)-仓库配置

2012-07-04 
《Maven实战》学习笔记(3)--仓库配置本地仓库配置本地仓库用于存储从远程仓库下载到的包默认路径~/.m2/repos

《Maven实战》学习笔记(3)--仓库配置

本地仓库配置
本地仓库用于存储从远程仓库下载到的包
默认路径~/.m2/repository/
可以在~/.m2/settings.xml中进行配置
<settings>
??? <localRepository>D:\maven\repository\</localRepository>
</settings>


远程仓库配置
用于从远程下载依赖包
默认使用mavne中央仓库,该配置在$M2_HOME/lib/maven-model-builder-3.0.jar中的org/apache/maven/model/pom-4.0.0.xml中
?<repositories>
??? <repository>
????? <snapshots>
??????? <enabled>false</enabled>:不从仓库下载快照版本
????? </snapshots>
????? <id>central</id>
????? <name>Maven Repository Switchboard</name>
????? <url>http://repo1.maven.org/maven2</url>
??? </repository>
? </repositories>


pom-4.0.0.xml是超级pom,所有pom都会继承。
每次部署编译时,如果在本地仓库中没有找到就会自动从远程仓库下载。

中央仓库不能满足要求时(因为有些包不在中央仓库中),可以配置多个远程仓库,比如JBOSS Repository:
<repositories>
??? ??? <repository>
??? ??? ??? <id>jboss</id>
??? ??? ??? <name>JBOSS Repository</name>
??? ??? ??? <url>http://repository.jboss.org</url>
??? ??? ??? <layout>default</layout>
??? ??? ??? <releases>
??? ??? ??? ??? <enabled>true</enabled>:开启发布版构件下载支持,该例子中只会下载发布版
??? ??? ??? ??? <checksumPolicy>warn</checksumPolicy>:下载校验失败时提示警告,fail:让构建失败 ignore:忽略错误
??? ??? ??? ??? <updatePolicy>always</updatePolicy>:从远程仓库检查更新的频率,这里是每次构建都检查,
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? 默认值是daily,一天一次,never:从不检查,
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? interval:x:每隔x分钟更新一次
??? ??? ??? </releases>
??? ??? ??? <snapshots>
??? ??? ??? ??? <enabled>false</enabled>
??? ??? ??? </snapshots>
??? ??? </repository>
??? </repositories>


镜像仓库
可以在settings.xml中配置镜像仓库,这样可以获得更快的下载速度,比如:
?<mirrors>
??? <mirror>
??? ??? <id>ibiblio.org</id>
??? ??? <name>ibiblio Mirror of http://repo1.maven.org/maven2/</name>
??? ??? <url>http://mirrors.ibiblio.org/pub/mirrors/maven2</url>
??? ??? <mirrorOf>central</mirrorOf>:*:匹配所有远程仓库,
??? ??? ??? ??? ??? ??? ??? external:*:匹配所有不在本机上的远程仓库(localhost和file的不匹配)
??? ??? ??? ??? ??? ??? ??? repo,repo2:匹配两个远程仓库,逗号分隔
??? ??? ??? ??? ??? ??? ??? *,! repo1:匹配所有远程仓库,repo1除外
??? ??? ??? ??? ??? ??? ??? 本例中意为只匹配中央仓库,也就是说,所有向中央仓库的请求都使用该镜像
??? ??? <!-- United States, North Carolina -->
??? </mirror>????
? </mirrors>
从网上找了一些镜像,发现大部分的都无法访问了,所以还是自己搭建私服吧。

仓库搜索服务
可以提供依赖的搜索,可以通过关键字,类名称,坐标等进行检索,找到自己需要的pom依赖配置
Sonatype Nexus:https://repository.sonatype.org/index.html#welcome
Jarvana:http://www.jarvana.com/jarvana/
MVNbrowser:http://www.mvnbrowser.com/index.html
MVNrepository:http://mvnrepository.com/


私服
Nexus:http://nexus.sonatype.org/downloads/
bundle版:自带了jetty容器,下载需要的版本即可,每个版本中都包含了所有平台的执行脚本,脚本地址在%nexus%/bin/jsw/下
??? windows?? linux
启动:nexus.bat???? ./nexus console? 默认使用8081端口,如:http://localhost:8081/nexus/? 修改端口:%nexus%/conf/plexus.properties,找到application-port,修噶后重启nexus
停止:Ctrl+C?????????? Ctrl+C
war版:需要部署到容器中,将war包部署到tomcat等容器中即可。
nexus配置方法:
在settings.xml中加入如下配置:
两种方式都可以:
1.镜像
<mirrors>
????? <mirror>
????? <id>nexus</id>:如果不配置镜像,则下载还会不时的访问中央仓库,所以这里使用镜像,*表示所有请求都转到nexus
????? <mirrorOf>*</mirrorOf>
????? <name>nexus mirror.</name>
????? <url>http://192.168.12.115:8081/nexus/content/groups/public</url>
??? </mirror>
? </mirrors>

2.profile
<profile>
??? ??? <id>nexus</id>
??? ??? ??? <repositories>
??? ??? ??? ??? <repository>
??? ??? ??? ??? ??? <id>nexus</id>
??? ??? ??? ??? ??? <url>http://192.168.12.115:8081/nexus/content/groups/public</url>
??? ??? ??? ??? ??? <releases>
??? ??? ??? ??? ??? ??? <enabled>true</enabled>
??? ??? ??? ??? ??? ??? <checksumPolicy>warn</checksumPolicy>
??? ??? ??? ??? ??? ??? <updatePolicy>always</updatePolicy>
??? ??? ??? ??? ??? </releases>
??? ??? ??? ??? ??? <snapshots>
??? ??? ??? ??? ??? ??? <enabled>true</enabled>
??? ??? ??? ??? ??? ??? <checksumPolicy>warn</checksumPolicy>
??? ??? ??? ??? ??? ??? <updatePolicy>always</updatePolicy>
??? ??? ??? ??? ??? </snapshots>
??? ??? ??? ??? </repository>
??? ??? ??? </repositories>
??? ??? ??? <pluginRepositories>
??? ??? ??? ??? <pluginRepository>
??? ??? ??? ??? ??? <id>nexus</id>
??? ??? ??? ??? ??? <url>http://192.168.12.115:8081/nexus/content/groups/public</url>
??? ??? ??? ??? ??? <releases>
??? ??? ??? ??? ??? ??? <enabled>true</enabled>
??? ??? ??? ??? ??? </releases>
??? ??? ??? ??? ??? <snapshots>
??? ??? ??? ??? ??? ??? <enabled>true</enabled>
??? ??? ??? ??? ??? </snapshots>
??? ??? ??? ??? </pluginRepository>
??? ??? ??? </pluginRepositories>
??? ??? </profile>
? <activeProfiles>
??? <activeProfile>nexus</activeProfile>:激活profiles
? </activeProfiles>
不过这种配置偶尔还是会访问中央仓库,所以可以结合镜像使用,让所有访问中央仓库的请求都转到nexus
<mirrors>
??? ??? <mirror>
??? ??? ??? <id>nexus</id>
??? ??? ??? <mirrorOf>central</mirrorOf>
??? ??? ??? <name>nexus mirror.</name>
??? ??? ??? <url>http://192.168.12.115:8081/nexus/content/groups/public</url>
??? ??? </mirror>
??? </mirrors>

将自己的项目部署至远程仓库
比如使用Nexus私服时,可以在项目的pom中这样配置
<distributionManagement>
??? ??? <repository>
??? ??? ??? <id>nexus-release</id>:正式版部署地址
??? ??? ??? <name>Nexus Release Repository</name>
??? ??? ??? <url>http://192.168.12.115:8081/nexus/content/repositories/releases/</url>
??? ??? </repository>
??? ??? <snapshotRepository>
??? ??? ??? <id>nexus-snapshots</id>:快照版部署地址
??? ??? ??? <name>Nexus Snapshots Repository</name>
??? ??? ??? <url>http://192.168.12.115:8081/nexus/content/repositories/snapshots/</url>
??? ??? </snapshotRepository>
??? </distributionManagement>

Nexus对于匿名用户是只读的,所以需要在settings中配置认证信息,如下:
<servers>
? <server>
????? <id>nexus-release</id>
????? <username>admin</username>
????? <password>123456</password>
??? </server>
??? <server>
????? <id>nexus-snapshots</id>
????? <username>admin</username>
????? <password>123456</password>
??? </server>
? </servers>

为了不影响各个项目团队间的发布,推荐为每个项目组建立独立的仓库并分配团队成员独立的权限,
比如nqcp团队,可以使用nqcp-releases仓库作为正式版的发布,使用nqcp-snapshots作为快照版本的发布。


热点排行