Maven篇----06 Nexus3中RestApi使用

Sonatype Nexus 3.0 版本提供 RestApi,它支持 Groovy API。
    其设计思路是:通过结合 Groovy 脚本来完成相关的功能,简单来说需要用户自行提供groovy脚本,nexus提供上传和运行脚本的接口,虽然有些粗糙,但是大部分功能毕竟留出了一个方式供用户去使用,至于groovy脚本中需要实现什么功能,那就看用户需要。在一定程度上来说,算是一个能够创造API的API。

1. API使用说明

1.1 使用步骤

步骤1: 以json文件为载体,创建可运行的groovy脚本

//脚本名称会成为后续API的一部分,请注意命名
{
  "name": "脚本名称",
  "type": "groovy",
  "content": "groovy语句"
}

步骤2: 上传json文件
将步骤1中准备的文件作为POST请求的内容传入

步骤3: 执行groovy文件
使用接口,执行在步骤2中上传的groovy脚本

1.2 Script相关的接口

这里针对操作 script 的接口先说明一下。在swagger-ui中,包含有Nexus所有接口的请求模板,需要可自行查阅。

// 增加一个脚本 -- Add a new script
curl -X POST -u admin:admin123 --header "Content-Type: application/json" http://10.15.22.172:8081/nexus/service/rest/v1/script -d @rawrepotest1.json

// 列出所有上传的脚本 -- List all stored scripts
curl -X GET -u admin:admin123 "http://10.15.22.172:8081/nexus/service/rest/v1/script" -H "accept: application/json"

// 通过名称读取脚本内容 -- Read stored script by name
curl -X GET -u admin:admin123 "http://10.15.22.172:8081/nexus/service/rest/v1/script/rawrepotest1" -H "accept: application/json"

// 通过名称更新脚本内容 -- Update stored script by name
curl -X PUT -u admin:admin123 "http://10.15.22.172:8081/nexus/service/rest/v1/script/rawrepotest1" -H "accept: application/json" -H "Content-Type: application/json" -d

// 通过名称删除脚本 -- Delete stored script by name
curl -X DELETE -u admin:admin123 "http://10.15.22.172:8081/nexus/service/rest/v1/script/rawrepotest1" -H "accept: application/json"

// 通过名称运行脚本 -- Run stored script by name
curl -X POST -u admin:admin123 --header "Content-Type: text/plain" http://10.15.22.172:8081/nexus/service/rest/v1/script/rawrepotest1/run

2. API使用实例

2.1. 如何在Nexus中创建一个maven2类型的repository。

步骤1: 以json文件为载体,创建可运行的groovy脚本
因为创建repository需要指定blob,所以这里创建如下的json文件:

$ cat maven2test1.json 
{
  "name" : "maven2test1",
  "content" : "def maven2Store = blobStore.createFileBlobStore('maven2test1', 'maven2');repository.createMavenHosted('maven2test1', maven2Store.name);",
  "type" : "groovy"
} 

步骤2: 上传json文件

$ curl -X POST -u admin:admin123 --header "Content-Type: application/json" http://localhost:32004/service/siesta/rest/v1/script -d @maven2test1.json

步骤3: 执行groovy文件

$ curl -X POST -u admin:admin123 --header "Content-Type: text/plain" http://localhost:32004/service/siesta/rest/v1/script/maven2test1/run
//调用成功返回报文
{
  "name" : "maven2test1",
  "result" : "RepositoryImpl$$EnhancerByGuice$$c5f0822b{type=hosted, format=maven2, name='maven2test1'}"
}

参考资料

Nexus基础:使用Api进行操作
https://blog.csdn.net/liumiaocn/article/details/83734437

REST and Integration API
https://help.sonatype.com/repomanager3/rest-and-integration-api

Sonatype Nexus 3.0. Using the new Groovy API
https://technology.amis.nl/2016/10/27/sonatype-nexus-3-0-using-the-new-groovy-api/

How to delete docker images from Nexus Repository Manager
https://support.sonatype.com/hc/en-us/articles/360009696054-How-to-delete-docker-images-from-Nexus-Repository-Manager

原文地址:https://www.cnblogs.com/liuyitan/p/13211126.html