读书人

MAVEN2入门学习心得(二)-仓库相关

发布时间: 2012-08-13 13:21:53 作者: rapoo

MAVEN2入门学习心得(2)-仓库相关
MAVEN2的仓库基本可以分为主机仓库、代理仓库、本地仓库。

主机仓库通常是构件的原始存储位置,比如:核心仓库central、Nexus建立的host仓库。

代理仓库通常是主机仓库的中间代理,比如:Nexus中建立的proxy仓库。

本地仓库通常是构件的最终需求位置,一般是用户代码构建的地方。用户在安装完MAVEN2的时候可以改变本地仓库的位置。

我们可以通过如下命令:mvn help:effective-pom 查看最简单的pom.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>


可以看到,构建过程中引用到的第三方构件仅会从核心仓库中查找并下载。

每次构建都从http://repo1.maven.org/maven2核心仓库下载是不适宜的。当然,如果引用构件已经存在于本地仓库到是不会重新下载。

我们还有调用企业内部的一些第三方构件的需要,这些都要求我们寻求搭建代理仓库。

Nexus是比较不错的代理仓库。如何搭建网上已经有不少资料,这里就不详细叙述了。

Nexus中已经存在了两个仓库组:Public Repositories、Public Snapshot Repositories。查看组中的仓库包括了主

机仓库(Central)的代理仓库。

我们接下来要做的是让本地安装的MAVEN在构建项目时查找Nexus仓库。

通常,在settings.xml文件中采用Profile来设置是比较方便的,如下:

<profiles><profile>       <id>nexus</id>       <repositories>         <repository>             <id>nexus</id>             <name>local private nexus</name>             <url>http://localhost:7777/nexus/content/groups/public</url>         </repository>       </repositories>     </profile>     <profile>       <id>nexus-snapshots</id>       <repositories>         <repository>             <id>nexus-snapshots</id>             <name>local private nexus snapshots</name>             <url>http://localhost:7777/nexus/content/groups/public-snapshots</url>         </repository>       </repositories>     </profile></profiles> 

当然,还要进行激活设定。

<activeProfiles>      <activeProfile>nexus</activeProfile>      <activeProfile>nexus-snapshots</activeProfile>   </activeProfiles>


这时,如果你细心的话会调用命令:mvn help:effective-pom 查看pom.xml的实际设置仓库情况,如下:

  <repositories>    <repository>      <id>nexus-snapshots</id>      <name>local private nexus snapshots</name>      <url>http://localhost:7777/nexus/content/groups/public-snapshots</url>    </repository>    <repository>      <id>nexus</id>      <name>local private nexus</name>      <url>http://localhost:7777/nexus/content/groups/public</url>    </repository>    <repository>      <snapshots>        <enabled>false</enabled>      </snapshots>      <id>central</id>      <name>Maven Repository Switchboard</name>      <url>http://repo1.maven.org/maven2</url>    </repository>  </repositories>


个人建议将central的直接请求映射到Nexus相应的仓库组比较合适。在settings.xml文件中增加如下设定就满足了需要。

<mirror>      <id>NexusMirror</id>      <name>Nexus Public Mirror</name>      <url>http://localhost:7777/nexus/content/groups/public</url>      <mirrorOf>central</mirrorOf></mirror>


这样基本完成了企业仓库的搭建,也让企业内部员工都能访问企业仓库而不是直接访问MAVEN核心仓库。

企业仓库有需要新增第三方构件需要的时候,可以采用如下命令行完成。

<distributionManagement> <repository> <id>test</id> <name>ums test</name> <url>http://localhost:7777/nexus/content/repositories/test</url> </repository></distributionManagement>

企业仓库的使用基本就这些,有新的想法再丰富的。

读书人网 >软件架构设计

热点推荐