nexus与maven集成

我们可以在项目的POM中配置仓库和插件仓库,当然如果我们想要所有的Maven项目都使用私服,可以在settings.xml中配置,但是settings.xml并不直接支持配置repositories和pluginRepositories,但是Maven提供了profiles下的profile可以让我们定义一系列的配置信息,然后指定其激活条件,能够定义在settings.xml中的信息有<repositories>、<pluginRepositories>和<properties>。定义在<properties>里面的键值对可以在pom.xml中使用,这里我们配置远程仓库的地址和插件仓库。
<profiles>  
<profile>
<id>jdk8</id>
<activation>
<activebydefault>true</activebydefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerversion>1.8</maven.compiler.compilerversion>
</properties>
</profile>
<profile>
<id>nexusRep</id>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<!-- snapshot默认是关闭的,需要手动开启 -->
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus pluginRepository</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<!--maven设置的远程中央工厂不能下载snapshots的jar,这里覆盖一下-->
<profile>
<id>centralProfile</id>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<!--mirror设置url,这里不起作用-->
<url>https://repo1.maven.org/maven2/</url>
<layout>default</layout>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>

但是Maven除了从Nexus上下载构件之外,还会时不时从中央仓库下载构件,然而这应该是Nexus的工作,所以我们需要配置一个镜像,用
<mirrorof>*</mirrorof>将Maven任何构件下载的请求都转发到私服中。
<mirrors>  
<mirror>
<id>nexus</id>
<mirrorof>*</mirrorof>
<name>A Local Nexus Server</name>
<url>http://1localhost:8081/nexus/content/groups/public/</url>
</mirror>
</mirrors>

注意:在这里配置了镜像,那在<profiles>里配置的远程仓库的<url>就失效了,有效的是<releases>和<snapshots>元素。
当Maven需要下载发布版或者快照版构件的时候,他会首先检查我们配置的远程仓库中的<releases>和<snapshots>元素,看该类型的构件是否可以下载,如果可以下载,就转到镜像地址去下载。
上面的profile配置以后,还要进行激活,否则不生效
  <!-- 激活 profile 配置 -->
<activeProfiles>
<!--<activeProfile>nexusProfile</activeProfile>-->
<activeProfile>centralProfile</activeProfile>
</activeProfiles>

使用Maven部署构件到Nexus:
在项目正常开发中,快照版本的构件可以部署到Nexus中策略为Snapshot的例如:maven-snapshots宿主仓库中,发布版本可以部署到Nexus中策略为Release的例如:maven-releases宿主仓库中,项目的POM文件配置如下:
	<!-- 发布项目到nexus -->
<distributionManagement>
<repository>
<id>user-release</id>
<name>user release repo</name>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>user-snapshot</id>
<name>user snapshot repo</name>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>

[list]
[*]ID名称必须要与settings.xml中Servers配置的ID名称保持一致。
[*]项目版本号中有SNAPSHOT标识的,会发布到Nexus Snapshots Repository, 否则发布到Nexus Release Repository,并根据ID去匹配授权账号。[/list]
Nexus仓库对于匿名用户是只读的,所以为了能够部署构件,还需要在settings.xml文件中配置认证信息。
	<server>
<id>user-release</id>
<username>admin</username>
<password>admin123</password>
</server>

<server>
<id>user-snapshot</id>
<username>admin</username>
<password>admin123</password>
</server>


[list]
[*]nexus-releases: 用于发布Release版本
[*]nexus-snapshots: 用于发布Snapshot版本[/list]

[list]
[*]在项目POM.xml中设置的版本号添加SNAPSHOT标识的都会发布为SNAPSHOT版本,没有SNAPSHOT标识的都会发布为Release版本。
[*]SNAPSHOT版本会自动加一个时间作为标识,如:4.3.0-SNAPSHOT发布后为变成4.3.0-SNAPSHOT-20160712.114532-1.jar[/list]

参考 [url]https://blog.csdn.net/hellow__world/article/details/71855883[/url]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值