pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.nihaorz</groupId> <artifactId>nexus-component-manager</artifactId> <version>1.0-SNAPSHOT</version> <properties> <!--指定Java版本--> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.konghq</groupId> <artifactId>unirest-java</artifactId> <version>3.7.02</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency> </dependencies> </project>
Component.java
package com.nihaorz.common; import lombok.Data; import java.util.ArrayList; import java.util.List; /** * Maven组件 */ @Data public class Component implements Comparable<Component> { private String id; private String repository; private String group; private String name; private String version; private String format; private List<Asset> assets = new ArrayList<>(); @Override public int compareTo(Component o) { return this.version.compareTo(o.getVersion()); } }
Asset.java
package com.nihaorz.common; import lombok.Data; @Data public class Asset { private String id; private String repository; private String path; private String downloadUrl; private Checksum checksum; private String format; }
Checksum.java
package com.nihaorz.common; import lombok.Data; @Data public class Checksum { private String sha1; private String md5; }
NexusComponentManager.java 程序入口
package com.nihaorz; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.reflect.TypeToken; import com.nihaorz.common.Component; import kong.unirest.HttpResponse; import kong.unirest.Unirest; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * nexus组件管理,目前主要用来实现SNAPSHOT组件自动删除,在 Nexus Repository Manager OSS 3.9.0-01 版本测试通过 */ public class NexusComponentManager { /** * nexus访问地址 */ private static final String NEXUS_URL = "http://192.168.100.154:8081"; /** * nexus账号 */ private static final String NEXUS_USERNAME = "admin"; /** * nexus密码 */ private static final String NEXUS_PASSWORD = "admin123"; /** * 成功删除的组件数量 */ private static int successDeleteTotal; /** * 根据仓库名称、groupId和artifactId搜索组件并删除,保留最后一个版本 * * @param args */ public static void main(String[] args) { long start1 = System.currentTimeMillis(); List<Component> list = search("geostack-snapshots", "geostack", "arcgis-service"); if (list.size() > 1) { Collections.sort(list); list.remove(list.size() - 1); System.out.println(String.format("本次合计将删除%d个组件", list.size())); for (int i = 0; i < list.size(); i++) { Component component = list.get(i); long start2 = System.currentTimeMillis(); delete(component); long end2 = System.currentTimeMillis(); System.out.println(String.format("[%d/%d] 删除%s仓库中的组件【%s-%s-%s】耗时%d毫秒", i + 1, list.size(), component.getRepository(), component.getGroup(), component.getName(), component.getVersion(), end2 - start2)); } long end1 = System.currentTimeMillis(); System.out.println(String.format("成功删除%d个组件,总计耗时:%d毫秒", successDeleteTotal, end1 - start1)); } else { System.out.println(String.format("本次查询匹配的组件数量为%d个,不做删除", list.size())); } } /** * 删除组件 * * @param component 组件对象 */ public static void delete(Component component) { String url = String.format("%s/service/rest/beta/components/%s", NEXUS_URL, component.getId()); HttpResponse response = Unirest.delete(url).basicAuth(NEXUS_USERNAME, NEXUS_PASSWORD).asEmpty(); int status = response.getStatus(); if (status == 204) { successDeleteTotal++; } else { System.out.println(String.format("组件【%s-%s-%s】删除失败,http响应代码:%d", component.getRepository(), component.getGroup(), component.getName(), component.getVersion(), status)); } } /** * 搜索组件 * * @param repository 仓库名称 * @param groupId maven中的groupId * @param artifactId maven中的artifactId * @return */ public static List<Component> search(String repository, String groupId, String artifactId) { final List<Component> list = new ArrayList<>(); search(repository, groupId, artifactId, list, null); return list; } /** * 搜索组件 * <p> * 搜索组件时API会对匹配结果进行分页,所以这里采用递归搜索 * * @param repository 仓库名称 * @param groupId maven中的groupId * @param artifactId maven中的artifactId * @param list 存放查询结果的集合 * @param token token,由上一页查询结果中获取,第一次查询传null */ private static void search(String repository, String groupId, String artifactId, List<Component> list, String token) { String url = String.format("%s/service/rest/beta/search?repository=%s&maven.groupId=%s&maven.artifactId=%s", NEXUS_URL, repository, groupId, artifactId); if (token != null) { url += "&continuationToken=" + token; } HttpResponse<String> response = Unirest.get(url).basicAuth(NEXUS_USERNAME, NEXUS_PASSWORD).asString(); if (response.getStatus() == 200) { JsonObject jsonObject = JsonParser.parseString(response.getBody()).getAsJsonObject(); List<Component> temp = new Gson().fromJson(jsonObject.getAsJsonArray("items"), new TypeToken<List<Component>>() { }.getType()); list.addAll(temp); if (!jsonObject.get("continuationToken").isJsonNull()) { token = jsonObject.get("continuationToken").getAsString(); search(repository, groupId, artifactId, list, token); } } else { System.out.println(String.format("组件搜索出错,http响应代码:%d,错误信息:%s", response.getStatus(), response.getStatusText())); } } }
标签:xml,java,String,nexus,component,list,private,组件,import From: https://www.cnblogs.com/lvjinlin/p/18128728