起因:
因公司的传统项目上线都是增量更新。每次上线都要手动复制自己修改过的类(svn带有*号的文件),找到其编译的路径,逐个复制到一个包下,压缩发给运维。整个流程下来,修改得不多还很轻松,一旦超过10个,心态爆炸,而且不能保证自己复制有没有 出错。还有上线不一定就打一次包,种种原因,让我主动去网上搜索了svn增量打包更新的方法。果不其然,有很多。结果就用,用了半年也没出事过。没写过内部类,或者一些线程Thread、runable、Timer等一般不会出现内部类。
内部类:
在一个类里如果有内部类,则编译是会产生两个class文件,一个类class 一个是内部类class。
甚至代码有嵌套会生成更多个。
使用(未支持内部类):
- 创建一个java项目:
public class Test{
//补丁文件,由eclipse svn plugin生成
public static String patchFile="C:\\Users\\LinQin\\Desktop\\patch.txt";
//项目文件夹路径
public static String projectPath="D:\\第一工作空间\\xxx项目名";
//web应用文件夹名
public static String webContent="OrderServer";
//class存放路径
public static String classPath="D:\\apache-tomcat-6.0.35\\webapps\\xxx项目名\\WEB-INF\\classes";
//补丁文件包存放路径
public static String desPath="C:\\Users\\LinQin\\Desktop\\xxx项目名";
//补丁版本
public static String version="xxx项目名";
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
copyFiles(getPatchFileList());
}
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);
}
}
return fileList;
}
public static void copyFiles(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(webContent,"");
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(fullDesFileNameStr+"复制完成");
}
}
}
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();
}
}
}
- 创建补丁:
- 配置好自己的实际参数,运行方法,将会打包到指定位置。
生成的文件夹里面包含两个文件夹:
一个是跟项目名字一样的,里面装有修改的class编译文件。
另一个是以webRoot结尾的(没有页面或者静态资源修改则没有该文件夹),里面有修改的页面,静态资源等。
把两个合起来就可以增量更新了。
- 缺点:
不支持内部类。内部类生成的编译文件往往带有$
字符。
个人增强版本:
在copyFiles方法里面增加了内部类判断。执行方法打印为红色的为内部类,自己再检查一下。
//增加判断是否包含内部类判断
// System.err.println("srcFile" + fullFileName);
// System.err.println("desFile" + desFileNameStr);
int src = fullFileName.lastIndexOf("/");
String srcName = fullFileName.substring(src + 1);
String clazzPath = fullFileName.substring(0, src + 1);
int des = desFileNameStr.lastIndexOf("/");
String desPath = desFileNameStr.substring(0, des + 1);
File file = new File(clazzPath);
File[] ls = file.listFiles();
for (int i = 0; i < ls.length; i++) {
File file2 = ls[i];
if (file2.getName().contains("$") && file2.getName().indexOf(srcName.substring(0,srcName.length()-6)) != -1) {
System.err.println(clazzPath + file2.getName());
// System.err.println(desPath + file2.getName());
copyFile(clazzPath + file2.getName(), desPath + file2.getName());
}
}
完整代码文件,点击下载。链接:https://pan.baidu.com/s/1d-porFgTEgsG8-fpIx9HsQ 密码:b5hz