通过Java代码的方式将jar包以及快照上传至nexus上
- release库
- snapshots库
与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"; // groupIdString a = "aaa"; // artifactIdString v = "1.0.0"; // versionString 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 clientHttpClient httpClient = HttpClients.createDefault();// Create a HTTP post request with the API URLHttpPost httpPost = new HttpPost(NEXUS_API_URL + snapshotFileName);// Add headershttpPost.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + getEncodedCredentials());httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.MULTIPART_FORM_DATA.toString());// Build a multipart request bodyHttpEntity 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 bodyhttpPost.setEntity(httpEntity);// Send the HTTP post requestHttpResponse response = httpClient.execute(httpPost);// Print the response status code and bodySystem.out.println(response.getStatusLine().getStatusCode());System.out.println(EntityUtils.toString(response.getEntity()));}// Helper method to get the Base64-encoded credentialsprivate 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 arrayprivate static String getMD5(byte[] bytes) throws IOException {return DigestUtils.md5Hex(bytes);}// Helper method to get the SHA-1 hash of a byte arrayprivate static String getSHA1(byte[] bytes) throws IOException {return DigestUtils.sha1Hex(bytes);}}