首页 / 体育 / 正文

maven repository(聊聊项目打包发布到maven私仓常见的几种方式)

放大字体  缩小字体 来源:南方小年是哪一天 2026-05-19 20:21  浏览次数:19

前言

在早期没有使用maven之前,我们引用一些公有jar或者api jar,我们可能会采用这样的方式,通过手动导入这些jar到项目的classpath路径进行引用。

有了maven后,我们公司内部可能就会搭建maven私仓比如nexus,然后把这些公有jar或者api jar上传到nexus私仓,在pom.xml配置一下这些jar的坐标就可以引用。

今天我们的话题就是来聊聊项目打包发布到maven私仓常见的几种方式

发布到maven私仓的步骤

1.在maven的settings.xml中< servers >节点配置用户名和密码,形如下:

<servers>    <server>      <id>nexus-releases</id>      <username>admin</username>      <password>admin123</password>    </server>    <server>      <id>nexus-snapshots</id>      <username>admin</username>      <password>admin123</password>    </server>    </servers>

注: 其中id可先看做是一个标识。username和password为nexus私仓的用户名和密码

2、指定发布到nexus私仓的url并进行发布

方式二:在maven的settings.xml中< profiles >节点配置< properties >,并在< properties >指定 < altSnapshotDeploymentRepository > 和< altReleaseDeploymentRepository >

形如下:

<profiles>     <profile>	 <id>nexus</id>     <properties>         <altSnapshotDeploymentRepository>             nexus-snapshots::default::http://192.168.0.11:8081/repository/maven-snapshots/         </altSnapshotDeploymentRepository>         <altReleaseDeploymentRepository>            nexus-releases::default::http://192.168.0.11:8081/repository/maven-releases/         </altReleaseDeploymentRepository>     </properties>    </profile>  </profiles>  <activeProfiles>    <activeProfile>nexus</activeProfile>  </activeProfiles>

注:

  • nexus-snapshots和 nexus-releases要和maven的配置文件settings.xml文件中的server的id,两者必须保持一致
  • 属性altSnapshotDeploymentRepository和altReleaseDeploymentRepository是随maven-release-plugin 2.8版一起引入的。低于2.8版本,执行mvn deploy时,则会报如下错误
Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter

解决方案就是在发布的项目中指定一下2.8版本以上的插件,形如下

<build>        <plugins>            <plugin>                <artifactId>maven-deploy-plugin</artifactId>                <version>2.8.2</version>            </plugin>        </plugins>    </build>

最后再执行maven的deploy命令进行发布

方式四:通过nexus的可视化界面进行上传jar发布

如下图

聊聊项目打包发布到maven私仓常见的几种方式nerror="javascript:errorimg.call(this);">

这几种发布方式的选择

方式一,通过distributionManagement这种方式发布,可能是大多数人的选择。但如果要发布的项目很多,我们就可以考虑使用方式二,通过在全局的settings文件配置altSnapshotDeploymentRepository 和altReleaseDeploymentRepository进行发布,只需配置一次,所有项目就都可以发布,无需在多个项目pom指定

方式一和方式二比较适合公司自己内部开发项目,对于一些第三方提供的jar,推荐使用mvn deploy -DrepositoryId=私库id(和settings.xml文件中的server的id保持一致) -Durl=私仓地址的方式或者直接使用nexus可视化界面上传的方式

打赏
0相关评论
热门搜索排行
精彩图片
声明:本站信息均由用户注册后自行发布,本站不承担任何法律责任。如有侵权请告知立立即做删除处理。
违法不良信息举报邮箱:seo100@163.com
头条快讯网 版权所有
中国互联网举报中心