持续集成(1)--配置maven使用nexus

本文介绍如何配置Maven使用本地Nexus服务器,包括设置repository和distributionManagement,以提高构建效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)在默认情况下,Maven依赖于中央仓库,这是为了能让Maven开箱即用,但仅仅这么做明显是错误的,这会造成大量的时间及带宽的浪费。既然文章的前面已经介绍了如何安装和配置Nexus,现在我们就要配置Maven来使用本地的Nexus,以节省时间和带宽资源。

(2)我们可以将Repository配置到POM中,但一般来说这不是很好的做法,原因很简单,你需要为所有的Maven项目重复该配置。因此,这里我将Repository的配置放到$user_home/.m2/settings.xml中:

<profile>
<id>hitvdev</id>
<repository>
<id>local-nexus</id>
<url>http://10.10.30.230:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>

</profile>

(3)由于我们不能直接在settings.xml中插入<repositories>元素,这里我们需要编写一个profile,并添加了一个profile并使用<activeProfile>元素自动将这个profile激活。这里的local-nexus仓库指向了刚才我们配置的Nexus中“Public Repositories”仓库组,也就是说,所有该仓库组包含的仓库都能供我们使用。此外,我们通过<releases>和<snapshots>元素激活了Maven对于仓库所有类型构件下载的支持,当然你也可以调节该配置,比如说禁止Maven从Nexus下载snapshot构件。

<activeProfiles>

<activeProfile>hitvdev</activeProfile>

</activeProfiles>

(4)通过Maven进行部署,更常见的场景是:团队在开发一个项目的各个模块,为了让自己开发的模块能够快速让其他人使用,你会想要将snapshot版本的构件部署到Maven仓库中,其他人只需要在POM添加一个对于你开发模块的依赖,就能随时拿到最新的snapshot。

以下的pom.xml配置和settings.xml能让你通过Maven自动化部署构件:

<project>
...
<distributionManagement>
<repository>
<id> nexus-releases </id>
<name> NexusReleaseRepository </name>
<url> http://127.0.0.1:8080/nexus/content/repositories/releases/ </url>
</repository>
<snapshotRepository>
<id> nexus-snapshots </id>
<name> NexusSnapshotRepository </name>
<url> http://127.0.0.1:8080/nexus/content/repositories/snapshots/ </url>
</snapshotRepository>
</distributionManagement>
...

</project>

(5)修改setting文件

  1. <settings>
  2. ...
  3. <servers>
  4. <server>
  5. <id>nexus-releases</id>
  6. <username>admin</username>
  7. <password>admin123</password>
  8. </server>
  9. <server>
  10. <id>nexus-snapshots</id>
  11. <username>admin</username>
  12. <password>admin123</password>
  13. </server>
  14. </servers>
  15. ...
  16. </settings>

(6)这里我们配置所有的snapshot版本构件部署到Nexus的Snapshots仓库中, 所有的release构件部署到Nexus的Releases仓库中。由于部署需要登陆,因为我们在settings.xml中配置对应Repository id的用户名和密码。

然后,在项目目录中执行mvn deploy ,你会看到maven将项目构件部署到Nexus中,浏览Nexus对应的仓库,就可以看到刚才部署的构件。当其他人构建其项目时,Maven就会从Nexus寻找依赖并下载。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值