专栏首页DevOps时代的专栏Nexus 制品库管理:使用 mvn deploy 进行发布

Nexus 制品库管理:使用 mvn deploy 进行发布

Nexus 可以用来管理二进制制品,如果是 Java 的应用,则可以与 Apache Maven 结合使用进行二进制制品的部署,这篇文章使用简单的示例来对使用方法进行说明。

Apache Maven Deploy Plugin

构建结束之后,使用Apache Maven Deploy Plugin可以将构建所生成的二进制制品添加至远端仓库以便其他开发者和项目所共享。这种情况一般有如下的使用场景:

  • 此二进制制品(通常是jar文件)是项目其他部分所依赖的组件
  • 此二进制制品可能是第三方提供的jar文件,为项目所需要使用的组件 由于仓库管理的内容并不仅仅包含二进制制品,同时可能会包含POM文件和metadata等其他信息,而这些都会在Apache Maven Deploy Plugin中得到保证。

使用方式

Apache Maven Deploy Plugin主要有如下两种使用方式:

  • deploy:deploy
  • deploy:deploy-file

Nexus的制品上传

早前的 Nexus3 的版本中并没有提供二进制制品的上传功能,此项功能特性为3.9版本之后添加,如果使用的是诸如3.2.1这样早期的版本,则可以考虑结合mvn deploy来实现二进制制品的上传。

事前准备

代码准备

事前准备spring boot的demo代码,只需要创建相关目录以及2个文件(pom.xml和java文件)

~ # mkdir demo
~ # cd demo
~/demo # vi pom.xml
~/demo # ls
pom.xml
~/demo # mkdir -p src/main/java/com/liumiaocn/springbootdemo
~/demo # cd src/main/java/com/liumiaocn/springbootdemo
~/demo/src/main/java/com/liumiaocn/springbootdemo # vi SpringbootdemoApplication.java
~/demo/src/main/java/com/liumiaocn/springbootdemo # cd ~/demo
~/demo # ls
pom.xml  src
~/demo #

详细参看如下文章:

  • https://blog.csdn.net/liumiaocn/article/details/83548217

Maven环境

设定Maven环境,版本依赖要求Maven版本>3.0.0,此处为3.5.2

~/demo # mvn --version
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T15:58:13+08:00)
Maven home: /usr/local/share/maven
Java version: 1.8.0_121, vendor: Oracle Corporation
Java home: /usr/lib/java-8-oracle/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"
~/demo #

Nexus环境

使用docker镜像方式启动Nexus的3.16.1版本的服务。docker-compose.yml信息如下所示:

version: '2'

services:
  # Repository service: Nexus
  nexus:
    image: sonatype/nexus3:3.16.1
    ports:
      - "32004:8081"
    volumes:
      - /home/local/nexus/data/:/nexus-data
    environment:
      - JAVA_MAX_MEN=512m
      - JAVA_MIN_MEN=128m
    restart: "no"

Maven私库

设定Nexus的hosttyped的Maven私库,详细可参看:

  • https://blog.csdn.net/liumiaocn/article/details/61931847

上述示例使用版本为3.2.1,本文使用示例为3.16.1,但使用方式相差无几

私库设定

连接Nexus私库需要设定用户名和密码,否则会提示Return code is: 401, ReasonPhrase: Unauthorized认证失败的信息。以下为未设定情况下的错误提示信息示例

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy-file (default-cli) on project springbootdemo: Failed to deploy artifacts: Could not transfer artifact com.liumiaocn:springbootdemo:jar:0.0.1-20190522.114757-1 from/to nexus-repo (http://192.168.163.141:32004/repository/mvnhosted/): Failed to transfer file: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.1-SNAPSHOT/springbootdemo-0.0.1-20190522.114757-1.jar. Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]

设定信息:在settings.xml的servers段中添加如下Nexus连接信息

    <server>
      <id>nexus-repo</id>
      <username>admin</username>
      <password>admin123</password>
    </server>

deploy:deploy-file方式

命令行示例: mvn package deploy:deploy-file -DgroupId=com.liumiaocn -DartifactId=springbootdemo -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -Dfile=target/springbootdemo-0.0.1-SNAPSHOT.jar -DgeneratePom=true -DrepositoryId=nexus-repo -Durl=http://192.168.163.141:32004/repository/mvnhosted/

  • 执行日志示例
~/demo # ls
pom.xml  src
~/demo # pwd
/root/demo
~/demo # mvn package deploy:deploy-file -DgroupId=com.liumiaocn \
>     -DartifactId=springbootdemo \
>     -Dversion=0.0.1-SNAPSHOT \
>     -Dpackaging=jar \
>     -Dfile=target/springbootdemo-0.0.1-SNAPSHOT.jar \
>     -DgeneratePom=true \
>     -DrepositoryId=nexus-repo \
>     -Durl=http://192.168.163.141:32004/repository/mvnhosted/
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springbootdemo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ springbootdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/demo/src/main/resources
[INFO] skip non existing resourceDirectory /root/demo/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ springbootdemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /root/demo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ springbootdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/demo/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ springbootdemo ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ springbootdemo ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ springbootdemo ---
[INFO] Building jar: /root/demo/target/springbootdemo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.6.RELEASE:repackage (default) @ springbootdemo ---
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springbootdemo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-deploy-plugin:2.8.2:deploy-file (default-cli) @ springbootdemo ---
Downloading from nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.1-SNAPSHOT/maven-metadata.xml
Uploading to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.1-SNAPSHOT/springbootdemo-0.0.1-20190522.122144-1.jar
Uploaded to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.1-SNAPSHOT/springbootdemo-0.0.1-20190522.122144-1.jar (16 MB at 5.7 MB/s)
Uploading to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.1-SNAPSHOT/springbootdemo-0.0.1-20190522.122144-1.pom
Uploaded to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.1-SNAPSHOT/springbootdemo-0.0.1-20190522.122144-1.pom (409 B at 942 B/s)
Downloading from nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/maven-metadata.xml
Uploading to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.1-SNAPSHOT/maven-metadata.xml
Uploaded to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.1-SNAPSHOT/maven-metadata.xml (777 B at 2.8 kB/s)
Uploading to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/maven-metadata.xml
Uploaded to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/maven-metadata.xml (287 B at 1.3 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.337 s
[INFO] Finished at: 2019-05-22T20:21:48+08:00
[INFO] Final Memory: 29M/111M
[INFO] ------------------------------------------------------------------------
~/demo #

而在Nexus所创建的Maven的仓库中也可以看到刚刚上传的文件, 除了jar文件之外还有sha1和md5的校验信息文件,由于generatePom设定为了true,因此还生成了pom.xml文件和相关的校验文件,详细信息如下图所示。

在实际的情况下,更多则是二进制制品本身需要带pom.xml或者不带的情况,一般也可以使用-DpomFile=pom.xml用于指定特定的pom文件。如果仅仅上传二进制制品的情况下,可根据实际的使用需要进行调整,只需将generatePom设定为false即可。这里为了演示方便,此处将Version修改为0.0.2

~/demo # mvn clean package deploy:deploy-file -DgroupId=com.liumiaocn \
>     -DartifactId=springbootdemo \
>     -Dversion=0.0.2-SNAPSHOT \
>     -Dpackaging=jar \
>     -Dfile=target/springbootdemo-0.0.2-SNAPSHOT.jar \
>     -DgeneratePom=false \
>     -DrepositoryId=nexus-repo \
>     -Durl=http://192.168.163.141:32004/repository/mvnhosted/
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springbootdemo 0.0.2-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ springbootdemo ---
[INFO] Deleting /root/demo/target
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ springbootdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/demo/src/main/resources
[INFO] skip non existing resourceDirectory /root/demo/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ springbootdemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /root/demo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ springbootdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/demo/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ springbootdemo ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ springbootdemo ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ springbootdemo ---
[INFO] Building jar: /root/demo/target/springbootdemo-0.0.2-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.6.RELEASE:repackage (default) @ springbootdemo ---
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springbootdemo 0.0.2-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-deploy-plugin:2.8.2:deploy-file (default-cli) @ springbootdemo ---
Downloading from nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.2-SNAPSHOT/maven-metadata.xml
Uploading to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.2-SNAPSHOT/springbootdemo-0.0.2-20190522.122713-1.jar
Uploaded to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.2-SNAPSHOT/springbootdemo-0.0.2-20190522.122713-1.jar (16 MB at 4.7 MB/s)
Downloading from nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/maven-metadata.xml
Downloaded from nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/maven-metadata.xml (287 B at 1.6 kB/s)
Uploading to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.2-SNAPSHOT/maven-metadata.xml
Uploaded to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.2-SNAPSHOT/maven-metadata.xml (604 B at 5.6 kB/s)
Uploading to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/maven-metadata.xml
Uploaded to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/maven-metadata.xml (327 B at 2.7 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.198 s
[INFO] Finished at: 2019-05-22T20:27:17+08:00
[INFO] Final Memory: 29M/113M
[INFO] ------------------------------------------------------------------------
~/demo #

可以看到并未生成pom.xml和相关的校验文件信息

deploy:deploy方式

事前准备

在此示例的pom.xml文件中添加如下部署管理distributionManagement相关的信息(加入到projects段中),即可直接通过mvn deploy来进行部署。

  <distributionManagement>
    <repository>
      <id>nexus-repo</id>
      <name>Nexus Repository</name>
      <url>http://192.168.163.141:32004/repository/mvnhosted/</url>
    </repository>
  </distributionManagement>

如果不设定此项,执行mvn deploy的时候则会提示repository element was not specified in the POM inside distributionManagement element的错误信息。

此处为了演示方便,同时将Version设定为0.0.3,从如下执行示例日志中可以看到,只需要执行mvn deploy:deploy或者mvn deploy即可完成部署了

~/demo # mvn clean package deploy:deploy
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springbootdemo 0.0.3-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ springbootdemo ---
[INFO] Deleting /root/demo/target
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ springbootdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/demo/src/main/resources
[INFO] skip non existing resourceDirectory /root/demo/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ springbootdemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /root/demo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ springbootdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/demo/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ springbootdemo ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ springbootdemo ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ springbootdemo ---
[INFO] Building jar: /root/demo/target/springbootdemo-0.0.3-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.6.RELEASE:repackage (default) @ springbootdemo ---
[INFO]
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-cli) @ springbootdemo ---
Downloading from nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.3-SNAPSHOT/maven-metadata.xml
Uploading to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.3-SNAPSHOT/springbootdemo-0.0.3-20190522.130006-1.jar
Uploaded to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.3-SNAPSHOT/springbootdemo-0.0.3-20190522.130006-1.jar (16 MB at 6.3 MB/s)
Uploading to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.3-SNAPSHOT/springbootdemo-0.0.3-20190522.130006-1.pom
Uploaded to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.3-SNAPSHOT/springbootdemo-0.0.3-20190522.130006-1.pom (1.6 kB at 5.0 kB/s)
Downloading from nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/maven-metadata.xml
Downloaded from nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/maven-metadata.xml (327 B at 1.9 kB/s)
Uploading to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.3-SNAPSHOT/maven-metadata.xml
Uploaded to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.3-SNAPSHOT/maven-metadata.xml (777 B at 3.9 kB/s)
Uploading to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/maven-metadata.xml
Uploaded to nexus-repo: http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/maven-metadata.xml (367 B at 2.1 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.998 s
[INFO] Finished at: 2019-05-22T21:00:09+08:00
[INFO] Final Memory: 29M/112M
[INFO] ------------------------------------------------------------------------
~/demo #

从Nexus上也可以看到二进制制品上传的结果,详细如下图所示

参考内容

  • http://maven.apache.org/plugins/maven-deploy-plugin/
  • https://support.sonatype.com/hc/en-us/articles/115006744008-How-can-I-programmatically-upload-files-into-Nexus-3-

作者:liumiaocn 原文链接:https://blog.csdn.net/liumiaocn/article/details/90452108

本文分享自微信公众号 - DevOps时代(DevOpsTimes)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-08-19

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Hadoop 安装与启动

    参考博文:[Hadoop]SSH免密码登录以及失败解决方案(http://blog.csdn.net/sunnyyoona/article/details/51...

    smartsi
  • Hadoop MapReduce 工作过程

    一个MapReducer作业经过了input,map,combine,reduce,output五个阶段,其中combine阶段并不一定发生,map输出的中间结...

    smartsi
  • Spring消息监听器无法注入bean的问题

    在activemq的监听器中,通过注解@Autowired或@Resource注入bean时,获取到的bean为null。调用该bean的方法时会报空指针异常。

    架构师小跟班
  • Mybatis逆向工程中的 mybatis-generator:generate 代码生成器的使用

    使用逆向工程可以根据数据库的表名字生成pojo层(实体类),mapper层(dao层,直接与底层的XML中映射相关),XML(映射执SQL语句)

    阮键
  • Maven远程仓库的配置和部署

    Maven 是专门用于构建和管理Java相关项目的工具。Maven 是一个项目管理和整合工具。Maven 为开发者提供了一套完整的构建生命周期框架。开发团队几乎...

    Java周某人
  • Java设计模式|简单工厂模式

    简单工厂模式是类的创建模式,又叫做静态工厂方法模式。简单工厂模式由一个工厂对象决定生产出哪一种产品类的实例。

    技术从心
  • Java开发环境系列:一篇能帮你解决99%线上问题的Tomcat排雷日记

    下载地址:https://archive.apache.org/dist/tomcat/

    架构师小跟班
  • PC微信逆向:分析发送xml名片call

    在发送 xml 名片时,需要先选择一个对象,我们首先要找的是选择的 call。找到了选择的 call,就能确定要发送的对象的微信 ID 的地址了。有了要发送对象...

    信安之路
  • cannot resolve symbol 'XXX'

    原因:出现这种情况的原因是在IDEA或者myeclipse编译器中没有导入相关的jar包(jar包相当于类的集合,可以使用其中的类);

    阮键
  • Java开发环境系列:项目管理利器Maven安装配置及常见问题分析

    下载地址:http://maven.apache.org/download.cgi

    架构师小跟班

扫码关注云+社区

领取腾讯云代金券