Maven jar包Nexus私服 Maven发布项目 (jar包) 到Nexus私服中的操作

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Maven jar包Nexus私服 Maven发布项目 (jar包) 到Nexus私服中的操作

瘦风的南墙   2021-03-15 我要评论
想了解Maven发布项目 (jar包) 到Nexus私服中的操作的相关内容吗,瘦风的南墙在本文为您仔细讲解Maven jar包Nexus私服的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Maven,jar包,Nexus私服,下面大家一起来学习吧。

1 需求说明

开发完项目后, 将项目版本发布到Nexus私服中.

2 实现步骤

2.1 Maven服务的setting.xml文件

(1) 如果本机安装了Maven服务, 可在${MAVEN_HOME}/conf/setting.xml中指定私服相关的配置:

  <!-- 在servers标签下配置server, 包括: 私服的用户名和密码, 在deploy项目时需要用到 -->
  <server>
    <id>releases</id>
    <username>admin</username>
    <password>admin123</password>
  </server>
  <server>
    <id>snapshots</id>
    <username>admin</username>
    <password>admin123</password>
  </server>

  <!-- 在profiles标签下配置profile, 包括: 私服所配的仓库、各个插件的仓库地址 -->
  <profile>
   <!-- profile的id -->
   <id>dev</id>
   <repositories>
    <repository>
     <!-- 仓库id, Repositories可以配置多个仓库, 要确保id不重复 -->
     <id>nexus</id>
     <!-- 仓库地址, 即nexus仓库组的地址 -->
     <url>http://ip:port/nexus/content/groups/public/</url>
     <!-- 是否下载Releases构件 -->
     <releases>
      <enabled>true</enabled>
     </releases>
     <!-- 是否下载Snapshots构件 -->
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
    </repository>
   </repositories>

   <pluginRepositories>
    <!-- 插件仓库, Maven的运行依赖插件, 也需要从私服下载插件 -->
    <pluginRepository>
     <!-- 插件仓库的id不允许重复, 如果重复, 后配置的优先 -->
     <id>public</id>
     <name>Public Repositories</name>
     <url>http://ip:port/nexus/content/groups/public/</url>
    </pluginRepository>
   </pluginRepositories>
  </profile>
  
  <!-- 还需指定联网仓库, 保证本私服中没有相关jar包或插件时可联网获取 -->
  <profile>
   <id>internet</id>
   <repositories>
    <repository>
     <id>nexus-aliyun</id>
     <name>Nexus aliyun</name>
     <layout>default</layout>
     <!-- 这里配置阿里云的仓库 -->
     <url>http://maven.aliyun.com/nexus/content/groups/public</url>
     <snapshots>
      <enabled>false</enabled>
     </snapshots>
     <releases>
      <enabled>true</enabled>
     </releases>
    </repository>
   </repositories>
  </profile>

(2) 如果本机没有安装Maven服务, 可在IDEA或Eclipse等开发环境默认使用的Maven配置中修改, 修改内容同上.

2.2 项目的pom.xml文件

在项目的pom.xml中的一级标签project下添加如下内容:

  <!-- 发布选项: id必须与setting.xml文件中server的id相同 -->
  <distributionManagement>
    <repository>
      <id>releases</id>
      <name>display</name>
      <url>http://ip:port/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
      <id>snapshots</id>
      <name>display</name>
      <url>http://ip:port/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
  </distributionManagement>

2.3 发布项目

以IDEA为例, 选中项目, 右键 -> Run Maven -> deploy,

或者在右边栏选中Maven栏目, 点击项目 -> Lifecycle -> deploy, 执行即可将项目发布到仓库中去.

注意:仓库中不能存在与当前项目名称+版本号相同的项目, 否则将导致出错: Bad Request: 400.

补充知识:maven上传jar包到nexus私服后的存放路径 以及 使用IDEA上传jar包的步骤

maven上传jar包到nexus私服的方法,网上大神详解很多,那么上传后的jar包存放到哪里了呢?

在下使用nexus3.2.1版本,在本地搭建了私服,使用maven上传jar包。最后结果如下:

点进去后展示的是:

这让我一度以为是以jar包的形式保存在本地,但事实证明,保存在本地的最终是一个 .bytes 类型的文件,它的默认路径在\nexus-3.2.1-01-win64\sonatype-work\nexus3\blobs\default\content下面

即使jar包是同样的,但是deploy了两次,那么就会展示两次

nexus设置自定义路径时,要设置Blob Stores,默认只有default一个,新建一个路径的话就可以自己指定了

简单说下在搭建好nexus私服以后,将jar包上传到私服的步骤

场景:使用IDEA,maven项目打jar包后上传

在pom.xml文件中配置

<distributionManagement>
  <repository>
    <id>nexus</id>
    <name>maven-releases</name>
    <url>http://localhost:8081/repository/maven-releases/</url>
  </repository>
  <snapshotRepository>
    <id>nexus</id>
    <name>maven-snapshots</name>
    <url>http://localhost:8081/repository/maven-snapshots/</url>
  </snapshotRepository>
</distributionManagement>

<id>标签对应着 maven的配置文件setting.xml中<server>的设置,如下:

<servers>
  <server>
   <id>nexus</id>
   <username>admin</username>
   <password>admin123</password>
  </server>
</servers>

最后使用deploy操作,将打好的jar包上传到nexus私服上

以上这篇Maven发布项目 (jar包) 到Nexus私服中的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们