很多情况下,项目只是修改个别文件, 需要更新到生产环境, 往往大量的配置文件等信息 需要修改后再打成war包,这样风险很大,而如果每个文件逐个替换会出现把有做修改的文件一个个挑出来,如果有成千上百的文件这样会疯掉,以下方法可以让你得到解救!
利用SVN版本号比较差异实现, 利用"svn diff -r m:n" 对版本m和版本n比较差异,得到版本之间修改的文件集合,将此集合循环解析并且复制拷贝到指定目录
因工作中的项目用到maven聚合,所以会有两个项目,FreePatchUtil中的路径可修改为自己svn对应本地路径
一 创建一个新项目 然后新建三个类
public class OpenCommpant {
public static void main(String[] args) {
try {
// String cmds = "cmd /k start java -cp c://command.jar com.plumnix.cmd.ReadWriteComponent";
Runtime.getRuntime().exec("cmd /k start java -cp " + System.getProperty("java.class.path") + " com.zhongwang.patch.FreePatchUtil");
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class FreePatchUtil {
/****************************************************************************************************************/
public static String webContent = "webapp";// web应用文件夹名
public static String desPath = "D:\\work\\builder\\svn\\patch";// 补丁文件包存放路径
public static String projectPath = "D:\\work\\builder\\svn\\branches\\release\\hrm-web";// 项目存放路径
public static String classPath = "D:\\work\\builder\\svn\\branches\\release\\hrm-web\\target\\classes";// class存放路径
public static String projectPath1 = "D:\\work\\builder\\svn\\branches\\release\\hrm-supports";// 项目存放路径
public static String classPath1 = "D:\\work\\builder\\svn\\branches\\release\\hrm-supports\\target\\classes";// class存放路径
// public static String runDiffCommand = "svn diff -r 19275:19371 --summarize";
public static String releaseFolder = "D:\\work\\builder\\svn\\branches\\release"; // Maven编译文件夹
public static String runDiffCommand = "svn diff -r {0} --summarize"; // svn diff -r m:n 路径 对版本m和版本n比较差异
public static String runLogCommand = "svn log -r {0}";
/****************************************************************************************************************/
private static List hrmWebDetails; //hrm项目集合
private static List hrmSupportDetails; //hrm-support依赖集合
private static List forDelDetails; //删除文件集合
public static String version;// 补丁版本
public static void main(String[] args) throws Exception {
readScreen();
makeVersion();
setPatchDetail();
//循环依赖
for(String detail: hrmSupportDetails) {
if(!isExistInWeb(detail)) {
copyFiles(detail, classPath1, projectPath1);
}
}
//再循环hrm
for(String detail: hrmWebDetails) {
copyFiles(detail, classPath, projectPath);
}
StringBuffer sbf = new StringBuffer();
sbf.append("以下文件需要删除:\r\n");
for(String detail: forDelDetails) {
sbf.append(detail + "\r\n");
}
System.out.println("版本日志执行语句为" + runLogCommand);
System.out.println();
sbf.append("本次更新日志如下:\r\n" + CommandUtil.exeCmd("cmd /c D: && cd " + releaseFolder + " && " + runLogCommand));
Files.write(Paths.get(desPath + "\\" + version + "\\readme.txt"), sbf.toString().getBytes(), StandardOpenOption.CREATE);
System.out.println();
CommandUtil.exeCmd("cmd /c D: && cd " + releaseFolder + " && svn revert --depth=infinity -R *");
System.out.println("本次补丁发布完毕");
Thread.sleep(60000);
}
@SuppressWarnings("resource")
private static void readScreen() {
Scanner s = new Scanner(System.in);
System.out.println("请输入原始基线svn版本号:");
String oldVersion = s.nextLine();
System.out.println(">>>" + oldVersion);
System.out.println("请输入新基线svn版本号:");
String newVersion = s.nextLine();
System.out.println(">>>" + newVersion);
runDiffCommand = MessageFormat.format(runDiffCommand, oldVersion + ":" + newVersion);
runLogCommand = MessageFormat.format(runLogCommand, oldVersion + ":" + newVersion);
}
private static boolean isExistInWeb(String detail) {
if(new File(projectPath + "\\" + detail).exists()) {
return true;
}
return false;
}
private static void makeVersion() {
version = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
}
private static void setPatchDetail() {
hrmWebDetails = new ArrayList();
hrmSupportDetails = new ArrayList();
forDelDetails = new ArrayList();
CommandUtil.exeCmd("cmd /c D: && cd " + releaseFolder + " && mvn clean package -Dmaven.test.skip=true");
String cmdString = CommandUtil.exeCmd("cmd /c D: && cd " + releaseFolder + " && " + runDiffCommand);
for(String line: cmdString.split("\n")) {
if(!"D".equalsIgnoreCase(String.valueOf(line.charAt(0)))) {
if(line.indexOf("\\src\\") != -1) {
if(line.indexOf("hrm-web") != -1) {
line = line.replaceAll(" ", "");
line = line.substring(line.indexOf("\\src\\") + 1, line.length());
hrmWebDetails.add(line);
} else if(line.indexOf("hrm-supports") != -1) {
line = line.replaceAll(" ", "");
line = line.substring(line.indexOf("\\src\\") + 1, line.length());
hrmSupportDetails.add(line);
}
}
} else {
forDelDetails.add(line.substring(line.indexOf("\\src\\") + 1, line.length()));
}
}
}
//拷贝文件
private static void copyFiles(String detail, String classPath, String projectPath) {
if (detail.indexOf("src\\main\\java") != -1) {
String fileName = detail.replace("src\\main\\java", "");
detail = classPath + fileName;
if (fileName.endsWith(".java")) {
fileName = fileName.replace(".java", ".class");
detail = detail.replace(".java", ".class");
}
String tempDesPath = fileName.substring(0, fileName.lastIndexOf("\\"));
String desFilePathStr = desPath + "\\" + version + "\\WEB-INF\\classes" + tempDesPath;
String desFileNameStr = desPath + "\\" + version + "\\WEB-INF\\classes" + fileName;
File desFilePath = new File(desFilePathStr);
if (!desFilePath.exists()) {
desFilePath.mkdirs();
}
if(new File(detail).isDirectory()) {
File fullDesFileNameStrFile = new File(detail);
if (!fullDesFileNameStrFile.exists()) {
fullDesFileNameStrFile.mkdirs();
}
System.out.println(detail + "文件夹创建完成");
} else {
copyClassFile(detail, desFileNameStr);
System.out.println(detail + "复制完成");
}
} else if(detail.indexOf("src\\main\\resources") != -1) {
String fileName = detail.replace("src\\main\\resources", "");
detail = classPath + fileName;
String tempDesPath = fileName.substring(0, fileName.lastIndexOf("\\"));
String desFilePathStr = desPath + "\\" + version + "\\WEB-INF\\classes" + tempDesPath;
String desFileNameStr = desPath + "\\" + version + "\\WEB-INF\\classes" + fileName;
File desFilePath = new File(desFilePathStr);
if (!desFilePath.exists()) {
desFilePath.mkdirs();
}
if(new File(detail).isDirectory()) {
File fullDesFileNameStrFile = new File(detail);
if (!fullDesFileNameStrFile.exists()) {
fullDesFileNameStrFile.mkdirs();
}
System.out.println(detail + "文件夹创建完成");
} else {
copyFile(detail, desFileNameStr);
System.out.println(detail + "复制完成");
}
} else {
String desFileName = detail.replace("src\\main\\webapp", "");
detail = projectPath + "\\" + detail;// 将要复制的文件全路径
String fullDesFileNameStr = desPath + "\\" + version + desFileName;
String desFilePathStr = fullDesFileNameStr.substring(0, fullDesFileNameStr.lastIndexOf("\\"));
File desFilePath = new File(desFilePathStr);
if (!desFilePath.exists()) {
desFilePath.mkdirs();
}
if(new File(detail).isDirectory()) {
File fullDesFileNameStrFile = new File(fullDesFileNameStr);
if (!fullDesFileNameStrFile.exists()) {
fullDesFileNameStrFile.mkdirs();
}
System.out.println(fullDesFileNameStr + "文件夹创建完成");
} else {
copyFile(detail, fullDesFileNameStr);
System.out.println(fullDesFileNameStr + "复制完成");
}
}
}
private static void copyFile(String sourceFileNameStr, String desFileNameStr) {
try {
copyFile(new File(sourceFileNameStr), new File(desFileNameStr));
} catch (IOException e) {
e.printStackTrace();
}
}
private static void copyClassFile(String sourceFileNameStr, String desFileNameStr) {
File srcFile = new File(sourceFileNameStr);
try {
File[] files = srcFile.getParentFile().listFiles();
for(File file: files) {
if(file.getName().equals(srcFile.getName())) {
copyFile(file, new File(desFileNameStr));
} else if(file.getName().startsWith(srcFile.getName().substring(0, srcFile.getName().indexOf(".")) + "$") && file.getName().endsWith(".class")) {
copyFile(file, new File(desFileNameStr.substring(0, desFileNameStr.lastIndexOf("\\")) + "\\" + file.getName()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void copyFile(File sourceFile, File targetFile) throws IOException {
BufferedInputStream inBuff = null;
BufferedOutputStream outBuff = null;
try {
// 新建文件输入流并对它进行缓冲
inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
// 新建文件输出流并对它进行缓冲
outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
// 缓冲数组
byte[] b = new byte[1024 * 5];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
} finally {
// 关闭流
if (inBuff != null)
inBuff.close();
if (outBuff != null)
outBuff.close();
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CommandUtil {
public static String exeCmd(String commandStr) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
try {
Process p = Runtime.getRuntime().exec(commandStr);
br = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
public static void main(String[] args) {
List commands = new ArrayList();
commands.add("cmd.exe /c dir");
// commands.add("cmd /c D:\\work\\builder\\svn\\trunk\\hrm\\hrm-web");
// commands.add("cmd /c D:\\work\\builder\\svn\\trunk\\hrm\\hrm-web");
// commands.add("cmd /c mvn package -DTestSkip");
// CommandUtil.exeCmd((String[])commands.toArray(new String[commands.size()]));
CommandUtil.exeCmd("cmd /c D: && cd D:\\work\\builder\\svn\\trunk\\hrm\\hrm-web && mvn package");
}
}
测试通过后,
二 导出可执行jar
1 右键项目 -> Export
2选择 Java -> JAR file -> Next
3选择导出项目 ,生成位置 -> Next
4 Next
5 选择入口 main方法6 生成后的jar 为可执行