一键生成jar和pom

一键生成jar和pom

背景

内网开发,有些jar找不到,需要在外网将对应的jar下载并上传到内网的maven私库中。

一键上传工具需要将jar和pom文件放在同一个文件夹中。因此借助AI生成了一个工具。

使用方式

  1. 将JarPomCoper.java放在自己的项目中(*)

    主要是项目的pom文件中包含需要的jar。尽量和真实项目的pom一致。

  2. 修改jarInstallPath变量

  3. 修改sourcePomDir变量

  4. 直接运行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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值