将本地maven仓库上传到私有仓库demo

张开发
2026/4/21 4:17:51 15 分钟阅读

分享文章

将本地maven仓库上传到私有仓库demo
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * 功能生成maven批量安装jar的语句 * author pactera * */ public class Test { //批量安装jar所在目录 public static String filePath D:\\repository; public static ListGroupInfo jarListnew ArrayList(); public static void main(String[] args) { File f new File(filePath); getJarList(f); // jarList.forEach(System.out::println); printFile(); } /** * groupIdcom.alibaba.cloud/groupId * artifactIdspring-cloud-alibaba-dependencies/artifactId * param file */ private static void getJarList(File file) { if(file.isDirectory()){ for (File listFile : file.listFiles()) { getJarList(listFile); } }else if(file.getName().endsWith(.jar)){ GroupInfo groupInfo new GroupInfo(); groupInfo.versionfile.getParentFile().getName(); groupInfo.artifactIdfile.getParentFile().getParentFile().getName(); groupInfo.groupIdgetGroupId(file); groupInfo.filefile; jarList.add(groupInfo); }else if(file.getName().endsWith(.pom)){ if(ifPomDir(file.getParentFile())){ GroupInfo groupInfo new GroupInfo(); groupInfo.versionfile.getParentFile().getName(); groupInfo.artifactIdfile.getParentFile().getParentFile().getName(); groupInfo.groupIdgetGroupId(file); groupInfo.isPomtrue; groupInfo.filefile; jarList.add(groupInfo); } } } private static boolean ifPomDir(File parentFile) { return !Arrays.stream(parentFile.listFiles()).anyMatch(f-f.getName().endsWith(.jar)); } private static String getGroupId(File file) { String replace file.getAbsolutePath().replace(filePath, ); String artifactId file.getParentFile().getParentFile().getName(); return replace.substring(1,replace.indexOf(artifactId)-1).replaceAll(\\\\,.); } /** * 功能批量安装maven的本地的jar */ public static void printFile() { //D:\git\ips-fj\libsmvn install:install-file -Dfileips-data-core-0.0.1-SNAPSHOT.jar -DgroupIdboco -DartifactIdips-data-core -Dversion0.0.1-SNAPSHOT -Dpackagingpom //mvn install:install-file -DgroupIdcom.xxx -DartifactIdxxx -Dversion1.1.1 -Dpackagingpom -Dfilexxx-1.1.1.pom jarList.forEach(jar-{ System.out.println(导入jar到本地仓库jar); String sb mvn install:install-file -Dfilejar.file.getAbsolutePath() -DgroupIdjar.groupId -DartifactIdjar.artifactId -Dversionjar.version -Dpackaging(jar.isPom?pom:jar); System.out.println(sb); execCommandAndGetOutput(sb); }); } public static void execCommandAndGetOutput(String comond) { try { Runtime runtime Runtime.getRuntime(); Process process runtime.exec(cmd.exe /c comond); // 输出结果必须写在 waitFor 之前 String outStr getStreamStr(process.getInputStream()); // 错误结果必须写在 waitFor 之前 String errStr getStreamStr(process.getErrorStream()); int exitValue process.waitFor(); // 退出值 0 为正常其他为异常 System.out.println(exitValue: exitValue); System.out.println(outStr: outStr); System.out.println(errStr: errStr); process.destroy(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } public static String getStreamStr(InputStream is) throws IOException { BufferedReader br new BufferedReader(new InputStreamReader(is, GBK)); StringBuilder sb new StringBuilder(); String line; while ((line br.readLine()) ! null) { sb.append(line); sb.append(\n); } br.close(); return sb.toString(); } static class GroupInfo{ String artifactId; String version; String groupId; Boolean isPomfalse; File file; Override public String toString() { return GroupInfo{ artifactId artifactId \ , version version \ , groupId groupId \ , filename file.getName() \ }; } } }

更多文章