背景:现有项目需要更换私服仓库,但新仓库没有旧仓库的依赖,需要将旧仓库的依赖推送到新仓库。
方法如下:
1.代码连接旧仓库将项目编译通过后拉取到所有的依赖到本地。
2.通过代码上传本地依赖到新的私服。
代码如下:
package com.xxxx.cloud.common.utils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.*;
import java.util.Base64;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 本地依赖推送工具 - 推送到 maven.dfwlg.com
* 直接在 IDEA 中右键运行 main 方法
*/
public class LocalRepoPusher {
// ==================== 配置区(修改这里) ====================
/** 本地仓库路径(根据你的 settings.xml) */
private static final String LOCAL_REPO = "D:\\javasoftware\\apache-maven-3.9.11-bin\\maven-repo";
/** 新私服 Releases 地址 */
private static final String RELEASES_URL = "https://maven.dfwlg.com/repository/maven-releases";
/** 新私服 Snapshots 地址 */
private static final String SNAPSHOTS_URL = "https://maven.dfwlg.com/repository/maven-snapshots";
/** 新私服登录用户名 */
private static final String USERNAME = "admin";
/** 新私服登录密码 */
private static final String PASSWORD = "Dfwlg@1234##";
// ==========================================================
private final AtomicInteger success = new AtomicInteger(0);
private final AtomicInteger failed = new AtomicInteger(0);
private final AtomicInteger skipped = new AtomicInteger(0);
// TODO 配置好上面路径及账户密码后运行main方法即可开始推送本地依赖到新私服
public static void main(String[] args) {
new LocalRepoPusher().start();
}
public void start() {
System.out.println("========================================");
System.out.println("本地依赖推送工具");
System.out.println("目标: maven.dfwlg.com");
System.out.println("本地仓库: " + LOCAL_REPO);
System.out.println("========================================\n");
Path repoPath = Paths.get(LOCAL_REPO);
try {
// 遍历本地仓库所有文件
Files.walk(repoPath)
.filter(Files::isRegularFile)
.filter(p -> {
String name = p.toString().toLowerCase();
return name.endsWith(".jar") || name.endsWith(".pom");
})
.forEach(this::pushFile);
} catch (IOException e) {
System.err.println("遍历仓库失败: " + e.getMessage());
}
System.out.println("\n========================================");
System.out.println("推送完成:");
System.out.println(" ✓ 成功: " + success.get());
System.out.println(" ✗ 失败: " + failed.get());
System.out.println(" ⏭ 跳过(已存在): " + skipped.get());
System.out.println("========================================");
}
private void pushFile(Path file) {
String relPath = Paths.get(LOCAL_REPO).relativize(file).toString().replace("\\", "/");
// 确定目标 URL
String targetUrl = relPath.contains("SNAPSHOT")
? SNAPSHOTS_URL + "/" + relPath
: RELEASES_URL + "/" + relPath;
try {
// 检查是否已存在
if (exists(targetUrl)) {
System.out.println("[SKIP] " + relPath);
skipped.incrementAndGet();
return;
}
// 上传
if (upload(file, targetUrl)) {
System.out.println("[OK] " + relPath);
success.incrementAndGet();
} else {
System.err.println("[FAIL] " + relPath);
failed.incrementAndGet();
}
} catch (Exception e) {
System.err.println("[ERROR] " + relPath + " - " + e.getMessage());
failed.incrementAndGet();
}
}
private boolean exists(String url) {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("HEAD");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
// 添加认证
String auth = Base64.getEncoder().encodeToString((USERNAME + ":" + PASSWORD).getBytes());
conn.setRequestProperty("Authorization", "Basic " + auth);
return conn.getResponseCode() == 200;
} catch (Exception e) {
return false;
}
}
private boolean upload(Path file, String targetUrl) {
int maxRetries = 3;
for (int i = 0; i < maxRetries; i++) {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) new URL(targetUrl).openConnection();
conn.setRequestMethod("PUT");
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setReadTimeout(60000);
// 认证
String auth = Base64.getEncoder().encodeToString((USERNAME + ":" + PASSWORD).getBytes());
conn.setRequestProperty("Authorization", "Basic " + auth);
// 内容类型
String contentType = file.toString().endsWith(".pom")
? "text/xml"
: "application/java-archive";
conn.setRequestProperty("Content-Type", contentType);
// 上传
try (OutputStream out = conn.getOutputStream();
InputStream in = Files.newInputStream(file)) {
byte[] buffer = new byte[8192];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
}
int code = conn.getResponseCode();
if (code == 200 || code == 201) {
return true;
}
if (i < maxRetries - 1) {
System.out.println(" 重试 " + (i + 1) + "/" + maxRetries + "...");
Thread.sleep(1000);
}
} catch (Exception e) {
if (i == maxRetries - 1) {
return false;
}
} finally {
if (conn != null) conn.disconnect();
}
}
return false;
}
}

2141

被折叠的 条评论
为什么被折叠?



