Maven 私服配置
2017-05-31 18:11
197 查看
原文地址:
https://my.oschina.net/heweipo/blog/4810471、配置Nexus为maven的私服
第一种方式:在项目的POM中如下配置
<repositories> <repository> <id>nexus_public</id> <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_public</id> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> <releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories>
注意:这样的配置(第一个仓库是代码库,第二个是插件库)只对当前的Maven项目有效。实际的项目开发中,我们都想着一次配置就能让本机的所有Maven项目都使用Maven私服,那么我们就要用到下面的方法。
第二种方式:在settings.xml中如下配置:
<settings>
...
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus_public</id>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>trueenabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus_public</id>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
...
settings>
Maven提供的profile是一组可选的配置,可以用来设置或者覆盖配置默认值。有了profile,你就可以为不同的环境定制构建。
上面的配置中,使用了一个id为nexus的profile,这个profile包含了相关的仓库配置,同时配置中又使用了activeProfiles元素将nexus这个profile激活,这样当执行Maven构建的时候,激活的profile会将仓库配置应用到项目中去。
通过上面的配置,我们会发现Maven不仅会从Nexus下载构件外还会从中央仓库下载构件构件,为什么呢?因为超级POM文件定义了一个为central的远程仓库,如果这个ID没有被覆盖,那么请求下载构件时还可能会从central标识的远程仓库下载。既然是私服,那么我们就只希望Maven下载请求都仅仅通过Nexus。我们可以通过镜像实现这一需求。可以创建一个匹配任何仓库的镜像,镜像的地址是私服,这样Maven对任何仓库的构件下载请求都会转到私服中。
central的意思其实是重写了超级POM的资源库,那么这里通过重写导致覆盖,重要的是,镜像效果导致直接走镜像,而不会去理会这些资源库配置了,所以基本上url也失去了意义。不过对于快照和发布版本是否进行更新下载还是要靠这个配置来决定的,只有这个配置决定继续更新时才会走镜像下载资源,镜像其实是像在镜像内部寻找资源,如果没有则会在镜像配置的远程仓库下载资源到镜像中。
把上面的配置修改为如下配置:
<settings> ... <mirrors> <mirror> <id>local_mirror</id> <mirrorOf>*</mirrorOf> <name>local_mirror</name> <url>http://localhost:8081/nexus/content/groups/public/</url> </mirror> </mirrors> <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>central</id> <url>http://repo.maven.apache.org/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://repo.maven.apache.org/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> ... </settings>
注意:以上两种方法都是配置下载的地址,也就是说项目需要下载插件时、下载代码时请求的地址。如果想要上传自己的代码,那么还需要如下配置,也就是部署构件到私服。
<servers> <server> <id>local_nexus_releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>local_nexus_snapshots</id> <username>admin</username> <password>admin123</password> </server> </servers>
相关文章推荐
- Linux安装配置maven3.0以及搭建nexus私服
- 对maven私服配置的说明
- Linux 安装配置maven3.0 以及搭建nexus私服
- Maven的使用及Nexus私服配置(一)
- maven仓库--私服(Nexus的配置使用)
- 第一篇:linux下用Artifactory配置Maven的私服
- maven私服nexus的安装、配置、使用
- Nexus2.6(maven私服)安装配置教程
- Maven和 Sonatype Nexus私服的安装、配置及使用入门
- Maven本地仓库配置和archiva私服搭建
- Linux安装配置maven以及搭建nexus私服
- 搭建配置私服-nexus ,Maven中的使用——3
- Linux安装配置maven以及搭建nexus私服(编写启动脚本)
- Maven私服(三) - Nexus的简单配置
- 配置两台电脑maven共用一个nexus私服
- maven私服的配置
- maven项目配置时私服nexus以及仓库repository的一些情况
- Nexus OSS私服仓库的安装和配置以及与Maven整合配置
- Maven私服--Nexus的本地xml配置
- Maven学习(二十六)-Maven私服(三) - Nexus的简单配置