通过Java代码的方式将jar包以及快照上传至nexus上,通过与chatgpt对话的方式完成

通过Java代码的方式将jar包以及快照上传至nexus上


与chatgpt的对话

在这里插入图片描述

release库

release库的直接在nexus页面上找接口就可以,通过restTemplate调用即可成功

import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;

public class testUpload {
    public static void main(String[] args)  {

        String r = "test"; // release 仓库名
        String g = "com"; // groupId
        String a = "aaa"; // artifactId
        String v = "1.0.0"; // version
        String p = "jar"; // 文件类型
        String c = "";
        String e = "jar";
        File file;

        RestTemplate restTemplate = new RestTemplate();
        String username = "your-username";
        String password = "your-password";
        restTemplate.getInterceptors().add(
                new BasicAuthorizationInterceptor(username, password));
        String filename = "D:\\path\\***.jar";
        file = new File(filename);
        FileSystemResource resource = new FileSystemResource(file);
        MultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<>();
        // add的顺序不能乱 否则会报401错误
        requestMap.add("r", r);
        requestMap.add("g", g);
        requestMap.add("a", a);
        requestMap.add("v", v);
        requestMap.add("p", p);
        requestMap.add("c", c);
        requestMap.add("e", e);
        requestMap.add("file", resource);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(requestMap, headers);
        test(restTemplate,requestEntity);

    }

    public static void test(RestTemplate restTemplate ,HttpEntity<MultiValueMap<String, Object>> requestEntity){
    	// nexus/service/local/artifact/maven/content 这是个固定的接口 在nexus上点击上传就是调用的这个接口
        String url = "http://ip:8081/nexus/service/local/artifact/maven/content";
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
    }
}

snapshots库

snapshots库不允许手动上传,所以找不到接口比较难搞,通过对chatGpt提问的方式找到了方法
下面的方法由gpt直接生成,只是改了NEXUS_API_URL 以及username、password一把就跑成功了

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class NexusSnapshotUploader {

    /**
     * NEXUS_API_URL  可以通过在url后拼接的方式添加 groupId artifactId
     */
    private static final String NEXUS_API_URL = "http://ip:8081/nexus/content/repositories/snapshots/test/";
    public static void main(String[] args) throws IOException {
        File snapshotFile = new File("D:\\path\\*****.jar");
        String snapshotFileName = snapshotFile.getName();
        byte[] snapshotBytes = Files.readAllBytes(snapshotFile.toPath());
        String encodedFile = new String(Base64.encodeBase64(snapshotBytes), "UTF-8");
        // Create a HTTP client
        HttpClient httpClient = HttpClients.createDefault();
        // Create a HTTP post request with the API URL
        HttpPost httpPost = new HttpPost(NEXUS_API_URL + snapshotFileName);
        // Add headers
        httpPost.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + getEncodedCredentials());
        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.MULTIPART_FORM_DATA.toString());
        // Build a multipart request body
        HttpEntity httpEntity = MultipartEntityBuilder.create()
                .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                .addBinaryBody("raw.asset1", snapshotBytes, ContentType.DEFAULT_BINARY, snapshotFileName)
                .addTextBody("raw.asset1.filename", snapshotFileName)
                .addTextBody("raw.asset1.extension", "jar")
                .addTextBody("raw.asset1.classifier", "snapshot")
                .addTextBody("raw.asset1.md5", getMD5(snapshotBytes))
                .addTextBody("raw.asset1.sha1", getSHA1(snapshotBytes))
                .build();
        // Set the request body
        httpPost.setEntity(httpEntity);
        // Send the HTTP post request
        HttpResponse response = httpClient.execute(httpPost);
        // Print the response status code and body
        System.out.println(response.getStatusLine().getStatusCode());
        System.out.println(EntityUtils.toString(response.getEntity()));
    }
    // Helper method to get the Base64-encoded credentials
    private static String getEncodedCredentials() {
        String username = "your-username";
        String password = "your-password";
        String credentials = username + ":" + password;
        byte[] encodedCredentials = Base64.encodeBase64(credentials.getBytes());
        return new String(encodedCredentials);
    }
    // Helper method to get the MD5 hash of a byte array
    private static String getMD5(byte[] bytes) throws IOException {
        return DigestUtils.md5Hex(bytes);
    }
    // Helper method to get the SHA-1 hash of a byte array
    private static String getSHA1(byte[] bytes) throws IOException {
        return DigestUtils.sha1Hex(bytes);
    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值