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脚本,注意需修改文件位置