您的位置:首页 > 其它

配置Maven从Nexus下载构件 ----学习笔记

2016-04-27 19:45 417 查看
配置Maven从Nexus下载构件

当需要为项目添加Nexus私服上的public仓库时,配置如下:

<project>

...

<repositories>

<repository>

<id>nexus</id>

<name>Nexus</name>

<url>http://localhost:8081/nexus/content/groups/public</url>

<releases>

<enabled>true</enabled>

</releases>

<snapshots>

<enabled>true</enabled>

</snapshots>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>nexus</id>

<name>Nexus</name>

<url>http://localhost:8081/nexus/context/groups/public/</url>

<snapshots><enabled>true</enabled></snapshots>

</pluginRepository>

</pluginRepositories>

...

</project>

这样的配置只对当前Maven项目有效,在实际应用中,我们往往想要通过一次配置就能让本机所有的Maven项目都使用自己的Maven私服。

这个时候我们想到settings.xml文件,该文件中的配置对所有本机Maven项目有效,但是settings.xml并不支持直接配置repositories和pluginsRepositories。所幸Maven还提供了Profile机制,能让用户将仓库配置放到settings.xml的profile中,代码如下:

<settings>

...

<profiles>

<profile>

<id>nexus</id>

<repositories>

<id>nexus</id>

<name>Nexus</name>

<url>http://localhost:8081/nexus/content/groups/public/</url>

<releases><enabled>true</enabled></releases>

<snapshots><enabled>true</enabled></releases>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>nexus</id>

<name>Nexus</name>

<url>http://localhost:8081/nexus/content/groups/public/</url>

<releases><enabled>true</enabled></releases>

<snapshots><enabled>true</enabled></releases>

</pluginRepository>

</pluginRepositories>

</profile>

</profiles>

<activeProfiles>

<activeProfile>nexus</activeProfile>

</activeProfiles>

...

</settings>

该配置中使用了一个id为nexus的profile,这个profile包含了相关的仓库配置,同时配置中又使用了activeProfile元素将nexus这个profile激活。这样当执行Maven构建的时候,激活的profile会将仓库配置应用到项目中去。Maven除了从Nexus下载构件之外,还会访问中央仓库central,我们希望的是所有Maven下载请求都仅仅通过Nexus,以全面发挥私服的作用。这需要借助Maven镜像配置了。可以创建一个匹配任何仓库的镜像,镜像地址为私服,这样Maven对任何仓库的构件下载请求都会转到私服中,具体配置如下:

<settings>

...

<mirrors>

<mirror>

<id>nexus</id>

<mirrorOf>*</mirrorOf>

<url>http://localhost:8081/nexus/content/groups/public</url>

</mirror>

</mirrors>

<profiles>

<profile>

<id>nexus</id>

<repositories>

<repository>

<id>central</id>

<url>http://central</url>

<releases><enabled>true</enabled></releases>

<snapshots><enabled>true</enabled></releases>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>central</id>

<url>http://central</url>

<releases><enabled>true</enabled></releases>

<snapshots><enabled>true</enabled></snapshots>

</pluginRepository>

</pluginRepositories>

</profile>

</profiles>

<activeProfiles>

<activeProfile>nexus</activeProfile>

</activeProfiles>

...

</settings>

这里需要解释的是仓库插件及插件仓库配置,他们id都为central,覆盖了超级POM中的中央仓库配置,他们的url已无关紧要��因为所有的请求都会通过镜像访问私服地址。配置仓库及插件仓库的主要目的是开启对快照版本下载的支持。当Maven需要下载发布版本或者快照构件时,首先检查central,看该类型构件是否支持下载,得到正面回答后再根据镜像匹配规则转而访问私服仓库地址。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: