Maven私服搭建| 通过java代码将本地仓库批量到nexus上

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一介草民丶

谢谢老板的一分钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值