先上代码
package com.incon.publicTool; /**
* @描述: TODO方法说明
* @创建: xiao
* @日期: 创建于 9:28 2020/9/1 0001
*/
import com.fh.util.DateUtil;
import com.sun.istack.internal.NotNull;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class CreatePatchIDEA {
//补丁文件,操作步骤:eclipse选中项目右击->team->创建补丁包->保存到文件系统->选择文件路径是D:\\根目录,然后生成。
//一
public static String patchFile = "E:\\qwe.txt";
//项目文件夹路径 (eclipse中工作空间,当前项目的路径,到项目名)
//二
public static String projectPath = "E:\\IdeaProNew\\pjxt_by\\classes\\artifacts\\pjxt_by";
//web应用文件夹名 (没啥好说的,只要存放jsp的上级文件夹叫这个名字就可以)
//三
// public static String WebRoot = "WebRoot";
public static String WebRoot = "";
//class存放路径 (项目的classes文件夹的具体路径)
//四
public static String classPath = "E:\\IdeaProNew\\pjxt_by\\classes\\artifacts\\pjxt_by\\WEB-INF\\classes";
//补丁文件包存放路径 (即导出后的文件路径和文件夹名)
//五
public static String desPath = "E:\\更新包名称" ;
//补丁版本 (里面就是补丁文件包)
//六
public static String version = "pjxt_by";
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
copyFiles(getPatchFileList());
}
/****
* 读取补丁配置文件解析出修改的文件并返回到list集合
* @return
* @throws Exception
*/
public static List<String> getPatchFileList() throws Exception {
List<String> fileList = new ArrayList<String>();
FileInputStream f = new FileInputStream(patchFile);
BufferedReader dr = new BufferedReader(new InputStreamReader(f, "utf-8"));
String line;
while ((line = dr.readLine()) != null) {
if (line.indexOf("Index:") != -1) {
line = line.replaceAll(" ", "");
line = line.substring(line.indexOf(":") + 1, line.length());
fileList.add(line);
}
}
dr.close();
return fileList;
}
/***
*
* @param list 修改的文件
*/
public static void copyFiles(@NotNull List<String> list) {
for (String fullFileName : list) {
if (fullFileName.indexOf("src/") != -1) {//对源文件目录下的文件处理
String fileName = fullFileName.replace("src", "");
fullFileName = classPath + fileName;
if (fileName.endsWith(".java")) {
fileName = fileName.replace(".java", ".class");
fullFileName = fullFileName.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();
}
copyFile(fullFileName, desFileNameStr);
System.out.println(fullFileName + "复制完成");
} else {//对普通目录的处理
String desFileName = fullFileName.replaceAll(WebRoot, "");
fullFileName = projectPath + "/" + fullFileName;//将要复制的文件全路径
String fullDesFileNameStr = desPath + "/" + version + desFileName;
String desFilePathStr = fullDesFileNameStr.substring(0, fullDesFileNameStr.lastIndexOf("/"));
File desFilePath = new File(desFilePathStr);
if (!desFilePath.exists()) {
desFilePath.mkdirs();
}
copyFile(fullFileName, fullDesFileNameStr);
System.out.println(fullFileName + "复制完成");
}
}
}
private static void copyFile(String sourceFileNameStr, String desFileNameStr) {
File srcFile = new File(sourceFileNameStr);
File desFile = new File(desFileNameStr);
try {
copyFile(srcFile, desFile);
} catch (IOException e) {
e.printStackTrace();
}
}
public 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();
}
}
}
首先介绍一下各参数都是干什么用的
代码除了路径需要自己修改外,其它的什么都不需要动
一、生成补丁包用到的路径,下面有介绍哪里会用到
二、项目编译后的路径
三、不用动
四、编译后的Java文件路径
五、生成更新包的路径以及名称
六、项目的名称
当你代码改完后,先别急的提交代码。
第一步
第二步 (选中你要打包的文件,这里最好不要选择配置文件,然后选中Create Patch…)
第三步
第四步 (点击上一步的ok后在E盘就会生成相应的文件)
第五步(打开txt文件,将 “WebRoot/WEB-INF”全部替换为 “/WEB-INF”,这里是针对web项目,因为普通的web项目编译后的java文件以及jsp、html文件再web-inf下,没有webroot这一层文件夹,所以把webroot去掉只保留web-inf。如果是maven项目的话就看具体位置更改具体的值了)
第六步 (打开代码,运行main方法)
第七步 (然后会在你写的 desPath 路径下生成你想要的文件)