一键生成jar和pom
背景
内网开发,有些jar找不到,需要在外网将对应的jar下载并上传到内网的maven私库中。
一键上传工具需要将jar和pom文件放在同一个文件夹中。因此借助AI生成了一个工具。
使用方式
-
将JarPomCoper.java放在自己的项目中(*)
主要是项目的pom文件中包含需要的jar。尽量和真实项目的pom一致。
-
修改jarInstallPath变量
-
修改sourcePomDir变量
-
直接运行main
tip:代码中的TODO部分
以下为源码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.DirectoryStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;
/**
* @author adminCx
* @version 1.0
* @description: 根据maven命令生成jar,再根据jar名称去本地仓库中复制出对应的pom文件。
*/
public class JarPomCoper {
public static void main(String[] args) {
//TODO jar和pom需要生成的文件目录
String jarInstallPath = "d:\\xxx";
boolean mvnSucc = execMvnCommand(jarInstallPath);
if (!mvnSucc){
System.err.println("mvn 命令执行失败,需要确保Maven已经被安装在系统上!");
return;
}
// Path sourceJarDir = Paths.get(targetPath);
//TODO 需要搜索的目录,一般是自己本地的maven仓库地址
Path sourcePomDir = Paths.get("C:\\.m2\\maven\\repository");
Path targetDir = Paths.get(jarInstallPath);
// 创建一个Map来存储POM文件的路径
Map<String, Path> pomPaths = new HashMap<>();
try {
// 递归遍历源POM目录,收集所有POM文件的路径
Files.walkFileTree(sourcePomDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.toString().endsWith(".pom")) {
pomPaths.put(extractJarName(file), file);
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
// 遍历源JAR目录中的所有JAR文件
try (DirectoryStream<Path> stream = Files.newDirectoryStream(targetDir, "*.jar")) {
for (Path jarPath : stream) {
String jarNameWithoutExtension = extractJarName(jarPath);
Path pomPath = pomPaths.get(jarNameWithoutExtension);
if (pomPath != null) {
Path targetPath = targetDir.resolve(pomPath.getFileName());
Files.copy(pomPath, targetPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Copied " + pomPath + " to " + targetPath);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 从文件路径中提取JAR名称,不包含扩展名
private static String extractJarName(Path path) {
String fileName = path.getFileName().toString();
return fileName.substring(0, fileName.length() - 4);
}
/**
* 执行mvn 命令
* @param targetPath 生成的文件目标目录
*/
private static boolean execMvnCommand(String targetPath) {
// Maven命令
String command = "mvn dependency:copy-dependencies -DoutputDirectory="+targetPath;
// 将命令分解为可执行的命令数组
// 注意:在Windows系统中,使用"cmd"和"/c"来执行命令
String[] commandArray = {"cmd", "/c", command};
try {
// 创建ProcessBuilder实例
ProcessBuilder processBuilder = new ProcessBuilder(commandArray);
// 启动进程
Process process = processBuilder.start();
// 读取命令输出
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
// 读取错误输出(如果有)
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String errorLine;
while ((errorLine = errorReader.readLine()) != null) {
System.err.println(errorLine);
}
// 等待进程结束
int exitCode = process.waitFor();
System.out.println("Process exited with code " + exitCode);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return false;
}
return true;
}
}
上传工具,参考:批量上传 Jar 包到 Maven 私服的工具_批量上传jar且自动生成pom文件-CSDN博客
但是该工具经本人验证,贴上来的代码在批量上传那里需要调整。
//这个代码片段需要移动for中
if(pom != null){
if(jar != null){
deploy(pom, jar, source, javadoc);
} else if(packingIsPom(pom)){
deployPom(pom);
}
}