JAVA自动任务上传依赖到NEXUS 仓库

本文介绍了如何使用Java编写一个程序,遍历ApacheMaven仓库中的POM文件,解析POM内容生成Mavendeploy命令,并将这些命令写入一个文本文件。脚本最后展示了一个Windows批处理脚本,用于执行这些生成的命令。
摘要由CSDN通过智能技术生成

java 生成mvn执行命令



import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import lombok.extern.slf4j.Slf4j;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import java.io.FileWriter;
import java.io.IOException;

@Slf4j
public class PomParser {
    public static String localRepoAbsPath = "D:\\apache-maven-3.3.9\\mvnRepositorie-all-PRD";
    public static String commands = "commands.txt";
    public static String commandTemplate = "mvn deploy:deploy-file %s %s -DgroupId=%s -DartifactId=%s -Dversion=%s -Dpackaging=%s -Durl=http://101.240.102.106:8181/repository/myRepo/ -DrepositoryId=myRepo";
    public static int jarFileCount = 0;
    public static int pomFileCount = 0;

    public static void main(String[] args) {
        File directory = new File(localRepoAbsPath);
        FileWriter writer = null;

        try {
            writer = new FileWriter(commands);

            processDirectory(directory, writer);

            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void processDirectory(File directory, FileWriter writer) throws Exception {

        for (File file : directory.listFiles()) {
            if (file.isDirectory()) {
                processDirectory(file, writer);
            } else if (file.getName().endsWith(".pom")) {
                processPomFileLog(file, writer);
            }
        }
    }


    public static void processPomFileLog(File pomFile, FileWriter writer) throws Exception {
        String command = null;

        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(pomFile);
            String pomPath = pomFile.getAbsoluteFile().getAbsolutePath();
            doc.getDocumentElement().normalize();
            String groupId = doc.getElementsByTagName("groupId").item(0).getTextContent();
            String artifactId = doc.getElementsByTagName("artifactId").item(0).getTextContent();
            String version = doc.getElementsByTagName("version").item(0).getTextContent();
            File jarFile = new File(pomFile.getAbsolutePath().replace(".pom", ".jar"));

            String fileParam = jarFile.exists() ? "-Dfile=" + jarFile.getAbsolutePath() : "-Dfile=" + pomPath;
            String pomParam = jarFile.exists() ? "-DpomFile=" + pomPath : "";
            String packaging = jarFile.exists() ? "jar" : "pom";

            if (jarFile.exists()) {
                jarFileCount++;
                log.info("Jar file count: " + jarFileCount);
            } else {
                pomFileCount++;
                log.info("only  Pom file count: " + pomFileCount);
            }

            command = String.format(commandTemplate,
                    fileParam, pomParam, groupId, artifactId, version, packaging);

            writer.write(command + "\n");
        } catch (Exception e) {
            log.error("err command:{}", command);
        }
    }
 
}

Windows 脚本

@echo off
setlocal enabledelayedexpansion

set "file=D:\code\study\hellow-world\commands.txt"
set "total=0"
set "success=0"
set "failed=0"

for /f "delims=" %%a in (%file%) do (
    set /a "total+=1"
    echo Executing: %%a
    %%a
    if errorlevel 1 (
        echo Command failed: %%a
        set /a "failed+=1"
    ) else (
        echo Command succeeded: %%a
        set /a "success+=1"
    )
)

echo Total commands: !total! > batc_upload_res.txt
echo Successful commands: !success! >> batc_upload_res.txt
echo Failed commands: !failed! >> batc_upload_res.txt

说明

先执行java 程序,然后双击bat脚本,注意需修改文件位置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值