import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FreePatchUtil
{
public static String projectPath = "E:/work/work_spaces/api/src/main/webapp"; //maven web项目的webapp目录
public static String classPath = "E:/work/work_spaces/api/target/classes"; //maven 项目编译后的classes目录
public static String patchFile = "F:/api.patch"; //升级补丁描述文件路径
public static String desPath = "F:/path"; //打包后升级包补丁包存放目录
public static String version = "20140711"; //补丁版本号
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
copyFiles(getPatchFileList());
}
/**
* 分析svn补丁文件
* @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)
{
//eclipse svn create path 创建的补丁包信息
if(line.indexOf("Index:")!=-1)
{
line=line.replaceAll(" ","");
line=line.substring(line.indexOf(":")+1,line.length());
fileList.add(line);
}
//如果是文件夹, 则跳过
else if(line.indexOf(".") == -1)
{
continue;
}
//TortoiseSVN show log 复制的补丁包信息
else if(line.startsWith("/api/"))
{
line = line.substring("/api/".length());
fileList.add(line);
}
}
return fileList;
}
/**
* 根据不同资源, 拷贝到补丁包的不同路径
* @param list
*/
public static void copyFiles(List<String> list)
{
for(String fullFileName:list)
{
//复制classes文件
if(fullFileName.indexOf("src/main/java") != -1)
{
String fileName = fullFileName.replace("src/main/java", "");
fullFileName = classPath + fileName;
if(fileName.endsWith(".java"))
{
fileName = fileName.replace(".java",".class");
fullFileName = fullFileName.replace(".java",".class");
}
String desFilePathStr = desPath + "/" + version + "/WEB-INF/classes" + fileName.substring(0, fileName.lastIndexOf("/"));
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 if(fullFileName.indexOf("src/main/resources") != -1)
{
String fileName = fullFileName.replace("src/main/resources", "");
fullFileName = classPath + fileName;
String desFilePathStr = desPath + "/" + version + "/WEB-INF/classes" + fileName.substring(0, fileName.lastIndexOf("/"));
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+"复制完成");
}
//复制web资源
else if (fullFileName.indexOf("src/main/webapp") != -1)
{
String fileName = fullFileName.replace("src/main/webapp", "");
fullFileName = projectPath + fileName;
String desFilePathStr = desPath + "/" + version + fileName.substring(0, fileName.lastIndexOf("/"));
String desFileNameStr = desPath + "/" + version + fileName;
File desFilePath=new File(desFilePathStr);
if(!desFilePath.exists())
{
desFilePath.mkdirs();
}
copyFile(fullFileName, desFileNameStr);
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();
}
}
}本文出自 “宁静致远” 博客,请务必保留此出处http://lizhuquan0769.blog.51cto.com/2591147/1750790
原文:http://lizhuquan0769.blog.51cto.com/2591147/1750790