package org.jeecg;
/**
* 本地maven 批量推送到私服 nexus上
*
* @Description: LocalMavenBatchPushNexus
* @Date 2025/2/5
* @author: su
*/
import java.io.File;
public class LocalMavenBatchPushNexus {
public static void main(String[] args) {
// 本地 Maven 仓库路径
String localRepoPath = "D:/maven/repo1";
// Nexus 私服的发布仓库地址
String nexusReleasesUrl = "http://10.213.71.19:10004/repository/maven-releases/";
// Nexus 私服的快照仓库地址
String nexusSnapshotsUrl = "http://10.213.71.19:10004/repository/maven-snapshots/";
File localRepoDir = new File(localRepoPath);
if (localRepoDir.exists() && localRepoDir.isDirectory()) {
traverseDirectory(localRepoDir, nexusReleasesUrl, nexusSnapshotsUrl);
}
}
private static void traverseDirectory(File directory, String nexusReleasesUrl, String nexusSnapshotsUrl) {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
traverseDirectory(file, nexusReleasesUrl, nexusSnapshotsUrl);
} else if (file.getName().endsWith(".pom")) {
processPomFile(file, nexusReleasesUrl, nexusSnapshotsUrl);
}
}
}
}
private static void processPomFile(File pomFile, String nexusReleasesUrl, String nexusSnapshotsUrl) {
String version = pomFile.getParentFile().getName();
String groupId = getGroupId(pomFile.getParentFile());
String artifactId = pomFile.getParentFile().getParentFile().getName();
File jarFile = new File(pomFile.getParent(), artifactId + "-" + version + ".jar");
if (jarFile.exists()) {
String serverId;
String nexusUrl;
if (version.contains("-SNAPSHOT")) {
serverId = "nexus-snapshots";
nexusUrl = nexusSnapshotsUrl;
} else {
serverId = "nexus-releases";
nexusUrl = nexusReleasesUrl;
}
String command = String.format("D:\\maven\\apache-maven-3.8.4\\bin\\mvn.cmd deploy:deploy-file -DgroupId=%s -DartifactId=%s -Dversion=%s -Dpackaging=jar -Dfile=%s -DpomFile=%s -Durl=%s -DrepositoryId=%s",
groupId, artifactId, version, jarFile.getAbsolutePath(), pomFile.getAbsolutePath(), nexusUrl, serverId);
try {
System.out.println(command);
//Process process = Runtime.getRuntime().exec(command);
//int exitCode = process.waitFor();
/* if (exitCode == 0) {
System.out.println("Successfully uploaded: " + groupId + ":" + artifactId + ":" + version);
} else {
System.err.println("Failed to upload: " + groupId + ":" + artifactId + ":" + version);
}*/
} catch (Exception e) {
e.printStackTrace();
}
} else {
jarFile = new File(pomFile.getParent(), artifactId + "-" + version + ".pom");
String serverId;
String nexusUrl;
if (version.contains("-SNAPSHOT")) {
serverId = "nexus-snapshots";
nexusUrl = nexusSnapshotsUrl;
} else {
serverId = "nexus-releases";
nexusUrl = nexusReleasesUrl;
}
String command = String.format("D:\\maven\\apache-maven-3.8.4\\bin\\mvn.cmd deploy:deploy-file -DgroupId=%s -DartifactId=%s -Dversion=%s -Dpackaging=pom -Dfile=%s -Durl=%s -DrepositoryId=%s",
groupId, artifactId, version, jarFile.getAbsolutePath(), nexusUrl, serverId);
System.out.println(command);
}
}
private static String getGroupId(File directory) {
StringBuilder groupId = new StringBuilder();
File currentDir = directory.getParentFile();
while (!currentDir.getAbsolutePath().endsWith("\\repo1")) {
if (groupId.length() > 0) {
groupId.insert(0, ".");
}
groupId.insert(0, currentDir.getName());
currentDir = currentDir.getParentFile();
}
return groupId.toString();
}
}
Maven私服搭建| 通过java代码将本地仓库批量到nexus上
于 2025-02-07 08:38:08 首次发布