Nexus制品库管理:二进制制品管理:上传

    xiaoxiao2022-07-12  185

    使用Nexus作为二进制制品管理仓库,二进制制品的上传主要可以通过三种方式来进行,结合Apache Maven Deploy Plugin进行操作、通过Nexus提供的UI界面进行或者通过Nexus提供的API进行操作,这篇文章对二进制制品上传至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"

    方式1: 使用Apache Maven Deploy Plugin

    Apache Maven提供了相关的插件对部署至远方仓库进行操作,这里以一个spring boot生成的jar文件为例,介绍了使用Apache Maven Deploy Plugin进行二进制制品上传的方法以及相关注意事项,详细请参看如下文章:

    https://liumiaocn.blog.csdn.net/article/details/90452108

    方式2: 通过Nexus UI界面进行操作

    详细spring boot应用的文件请参看上述链接,此处将版本设定为0.0.4重新生成jar文件,利用Nexus UI进行上传进行演示。

    ~/demo # mvn package [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building springbootdemo 0.0.4-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.4-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.0.6.RELEASE:repackage (default) @ springbootdemo --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 10.396 s [INFO] Finished at: 2019-05-22T21:21:04+08:00 [INFO] Final Memory: 29M/111M [INFO] ------------------------------------------------------------------------ ~/demo #

    上述生成的jar文件通过UI则可进行上传 上传之后结果信息如下所示: 而具体的文件信息,除了jar文件之外,还会自动生成md5相关校验信息

    方式3: 通过API进行上传

    使用–upload-file进行上传: 示例:curl -v -u admin:admin123 --upload-file target/springbootdemo-0.0.5-SNAPSHOT.jar http://192.168.163.141:32004/repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.5-SNAPSHOT/springbootdemo-0.0.5-SNAPSHOT.jar

    执行示例日志

    ~/demo # curl -v -u admin:admin123 --upload-file target/springbootdemo-0.0.5-SNAPSHOT.jar http://192.168.163.141:32004/repository/mvnhos ted/com/liumiaocn/springbootdemo/0.0.5-SNAPSHOT/springbootdemo-0.0.5-SNAPSHOT.jar * Trying 192.168.163.141... * TCP_NODELAY set * Connected to 192.168.163.141 (192.168.163.141) port 32004 (#0) * Server auth using Basic with user 'admin' > PUT /repository/mvnhosted/com/liumiaocn/springbootdemo/0.0.5-SNAPSHOT/springbootdemo-0.0.5-SNAPSHOT.jar HTTP/1.1 > Host: 192.168.163.141:32004 > Authorization: Basic YWRtaW46YWRtaW4xMjM= > User-Agent: curl/7.52.1 > Accept: */* > Content-Length: 16236571 > Expect: 100-continue > < HTTP/1.1 100 Continue * We are completely uploaded and fine < HTTP/1.1 201 Created < Date: Wed, 22 May 2019 13:45:37 GMT < Server: Nexus/3.16.1-02 (OSS) < X-Content-Type-Options: nosniff < Content-Security-Policy: sandbox allow-forms allow-modals allow-popups allow-presentation allow-scripts allow-top-navigation < X-XSS-Protection: 1; mode=block < Content-Length: 0 < * Curl_http_done: called premature == 0 * Connection #0 to host 192.168.163.141 left intact ~/demo #

    而具体上传的内容,也只有jar文件本身 此API从Nexus较早版本即可开始使用,另外除此之外还可以使用如下的API,此处不再赘述

    APIHTTP 方法Nexus版本要求/service/rest/beta/componentsPOSTNexus 3.9.0 - 3.13.0/service/rest/v1/componentsPOST>=Nexus 3.13.0

    参考文章

    https://support.sonatype.com/hc/en-us/articles/115006744008-How-can-I-programmatically-upload-files-into-Nexus-3-

    淼叔 认证博客专家 神经网络 TensorFlow NLP 资深架构师,PMP、OCP、CSM、HPE University讲师,EXIN DevOps Professional与DevOps Master认证讲师,曾担任HPE GD China DevOps & Agile Leader,帮助企业级客户提供DevOps咨询培训以及实施指导。熟悉通信和金融领域,有超过十年金融外汇行业的架构设计、开发、维护经验,在十几年的IT从业生涯中拥有了软件开发设计领域接近全生命周期的经验和知识积累,著有企业级DevOps技术与工具实战。
    最新回复(0)