主机仓库通常是构件的原始存储位置,比如:核心仓库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核心仓库。
企业仓库有需要新增第三方构件需要的时候,可以采用如下命令行完成。
引用
mvn deploy:deploy-file -DgroupId=com.oracle -DartifactId=ojdbc14 \
-Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=ojdbc.jar \
-Durl=http://localhost:7777/nexus/content/repositories/thirdparty \
-DrepositoryId=thirdparty
-Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=ojdbc.jar \
-Durl=http://localhost:7777/nexus/content/repositories/thirdparty \
-DrepositoryId=thirdparty
http://localhost:7777/nexus/content/repositories/thirdparty是Nexus中已经存在的主机仓库。
企业通常有需要在Nexus中建立自己的仓库组和仓库。假如企业增加了一个仓库,其url为
http://localhost:7777/nexus/content/repositories/test。
如果是jar文件,可以采用前面的命令行mvn deploy:deploy-file来完成仓库中构件的添加。
如果是maven项目,可以使用mvn deploy完成。但在pom.xml文件中应该增加发布管理设定。
<distributionManagement> <repository> <id>test</id> <name>ums test</name> <url>http://localhost:7777/nexus/content/repositories/test</url> </repository> </distributionManagement>
企业仓库的使用基本就这些,有新的想法再丰富的。