【7.2.5】【rule】工具更新

pull/37/head
fengshuonan 2022-09-30 23:19:10 +08:00
parent 47438ce13b
commit ff19e2965b
1 changed files with 345 additions and 0 deletions

View File

@ -0,0 +1,345 @@
package cn.stylefeng.roses.kernel.rule.util;
import cn.hutool.core.io.IoUtil;
import lombok.Getter;
import lombok.Setter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* mvn
* <p>
* 1. jar
* 2. jar
*
* @author fengshuonan
* @date 2022/9/30 21:31
*/
@Getter
@Setter
public class MvnDeployUtil {
/**
*
*/
private final List<File> directories = new ArrayList<>();
/**
* mvn
* <p>
* D:\apache-maven-3.5.4\bin\mvn.cmd
*/
private String mvnExePath;
/**
*
* <p>
* jar
* <p>
* D:\tmp\needToDeploy.zip
*/
private String targetZipPath;
/**
* mavensettings
* <p>
* D:\apache-maven-3.5.4\conf\settings.xml
*/
private String mvnSettingXmlPath;
/**
* id
* <p>
* maven-host-company
*/
private String repositoryId;
/**
* url
* <p>
* http://192.168.1.2:8081/repository/maven-host-company/
*/
private String repositoryUrl;
public static void main(String[] args) {
MvnDeployUtil mvnDeployUtil = new MvnDeployUtil();
mvnDeployUtil.setTargetZipPath("D:\\workspace-guns\\roses\\kernel-a-rule\\target");
mvnDeployUtil.setMvnExePath("D:\\apache-maven-3.5.4\\bin\\mvn.cmd");
mvnDeployUtil.setMvnSettingXmlPath("D:\\apache-maven-3.5.4\\conf\\settings.xml");
mvnDeployUtil.setRepositoryId("company-hosted");
mvnDeployUtil.setRepositoryUrl("http://192.168.31.3:8081/repository/company-hosted/");
mvnDeployUtil.beginDeploy();
}
/**
* pompackagingpom
*
* @author fengshuonan
* @date 2022/9/30 21:45
*/
public static boolean packagingIsPomFlag(File pom) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(pom)));
String line;
while ((line = reader.readLine()) != null) {
if (line.trim().contains("<packaging>pom</packaging>")) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
IoUtil.close(reader);
}
return false;
}
/**
*
*
* @author fengshuonan
* @date 2022/9/30 21:49
*/
private void getAllDirs(String fileDir) {
// 先添加本目录
directories.add(new File(fileDir));
// 递归添加子目录
File file = new File(fileDir);
File[] files = file.listFiles();
if (files == null) {
return;
}
for (File item : files) {
if (item.isDirectory()) {
directories.add(new File(item.getAbsolutePath()));
getAllDirs(item.getAbsolutePath());
}
}
}
/**
* pomjarpom
*
* @author fengshuonan
* @date 2022/9/30 21:49
*/
private DirectoryType getDirectoryType(File directoryPath) {
boolean pom = false;
boolean jar = false;
File[] files = directoryPath.listFiles();
if (files == null) {
return DirectoryType.NONE;
}
for (File file : files) {
if (file.getName().endsWith(".pom")) {
pom = true;
} else if (file.getName().endsWith(".jar")) {
jar = true;
}
}
if (pom && !jar) {
return DirectoryType.POM;
} else if (jar && pom) {
return DirectoryType.JAR_AND_POM;
} else {
return DirectoryType.NONE;
}
}
/**
* pommvn deploy
*
* @author fengshuonan
* @date 2022/9/30 21:55
*/
private void doOnlyPom(File directory) {
File[] files = directory.listFiles();
if (files == null) {
return;
}
File pom = null;
for (File file : files) {
String name = file.getName();
if (name.endsWith(".pom")) {
pom = file;
}
}
String command = buildCommand(FileType.POM, null, pom);
executeCommand(command);
}
/**
* jarpommvn deploy
*
* @author fengshuonan
* @date 2022/9/30 21:58
*/
private void doJarAndPom(File directory) {
File[] files = directory.listFiles();
File pom = null;
File jar = null;
if (files != null) {
for (File file : files) {
String name = file.getName();
if (name.endsWith(".pom")) {
pom = file;
} else if (name.endsWith(".jar")) {
jar = file;
}
}
if (jar != null) {
String command = buildCommand(FileType.JAR, jar, pom);
executeCommand(command);
}
}
}
/**
*
*
* @author fengshuonan
* @date 2022/9/30 21:58
*/
public void beginDeploy() {
//初始化获取所有的目录存到list
this.getAllDirs(targetZipPath);
//遍历所有目录并根据不同类型的目录执行deploy
for (File directory : directories) {
DirectoryType directoryType = getDirectoryType(directory);
if (directoryType.equals(DirectoryType.NONE)) {
continue;
} else if (directoryType.equals(DirectoryType.JAR_AND_POM)) {
doJarAndPom(directory);
} else if (directoryType.equals(DirectoryType.POM)) {
doOnlyPom(directory);
}
}
}
/**
* mvn
*
* @author fengshuonan
* @date 2022/9/30 21:54
*/
private void executeCommand(String command) {
try {
System.out.println("开始执行mvn命令" + command);
Process exec = Runtime.getRuntime().exec(command);
System.out.println(IoUtil.read(exec.getInputStream(), true));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* mvn deploy command
*
* @author fengshuonan
* @date 2022/9/30 21:52
*/
private String buildCommand(FileType fileType, File deployJar, File deployJarPom) {
// 目录下单纯有pom文件并且不是packaging=pom类型的直接略过
if (fileType.equals(FileType.POM)) {
if (!packagingIsPomFlag(deployJarPom)) {
return "";
}
}
// 构建执行推送的命令
String command = this.mvnExePath + " " +
"-s " + this.mvnSettingXmlPath + " " +
"install:install-file " +
"-Durl=" + this.repositoryUrl + " " +
"-DrepositoryId=" + this.repositoryId + " " +
"-DgeneratePom=false ";
// 获取packing
String packing;
if (fileType.equals(FileType.JAR)) {
packing = "-Dpackaging=jar ";
} else {
packing = " ";
}
// 获取pomFile和file
String pomFile = deployJarPom.getAbsolutePath();
String file;
if (fileType.equals(FileType.POM)) {
file = deployJarPom.getAbsolutePath();
} else {
file = deployJar.getAbsolutePath();
}
command += packing;
command += " -Dfile=" + file + " ";
command += " -DpomFile=" + pomFile + " ";
return command;
}
/**
*
* <p>
* jar
*
* @author fengshuonan
* @date 2022/9/30 21:33
*/
private enum DirectoryType {
/**
* jarpom
*/
NONE,
/**
* pom
*/
POM,
/**
* jarpom
*/
JAR_AND_POM
}
/**
* mvn
*
* @author fengshuonan
* @date 2022/9/30 21:34
*/
private enum FileType {
/**
* jar
*/
JAR,
/**
* pom
*/
POM
}
}