很早以前写过的一个应用,翻出来看看,估计以后还是大有用处的,怕弄丢了,毕竟写这个代码还是花了不少脑细胞的,就再整理整理了
直接贴代码了:
/**
* 使用说明:
* 注意:在使用之前需保证本地和SVN上的数据保持一致。
*
* 使用方法:
* 1. 查看SVN日志,将日志复制到一个文本文件 war.txt 中
* 2. 配置参数,需对以下几个参数进行配置:(对于目录路径可以不用斜杠结尾)
* destPath: 生成的打包文件夹。
* workPath: 服务器下部署目录。
* 3. 执行完成之后,查看日志 log/fatwar.log 如果没有任何异常且没有错误提示信息(在最后),则表示执行成功。
* 如果执行成功,那么在日志中最后会提示生成的打包地址。
*/
public class FatWarMain
{
private final static Logger log = LogManager.getLogger(FatWarMain.class);
public static String destPath = null; // 放生成的WAR路径
public static String txtPath = "war.txt"; // 将SVN信息复制到该文本文件中
public static String workPath = null; // 注意此目录需配置为服务器目录,eclipse工作目录可能不存在JSP文件
public static void main(String[] args)
{
setResourceAttributeValue();
if (workPath == null || "".equals(workPath))
{
log.info("请配置参数,参数值不能为空!");
return;
}
if (!new File(workPath).exists())
{
log.info("指定服务器部署目录不存在,请检查路径是否正确!");
return;
}
log.info("destPath:" + destPath + " workPath:" + workPath);
StringBuffer errinfo = new StringBuffer();
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(txtPath));
String string = null;
while ((string = reader.readLine()) != null)
{
log.info("SVN信息:" + string);
if (string.contains("/ProductCode/"))
{
String filename = string.substring(string.lastIndexOf('/') + 1);
log.info("获取文件名:" + filename);
if (filename.endsWith(".java")) // 编译之后生成class文件,且要考虑内部类的情况
{
final String temp = filename.substring(0, filename.indexOf(".java")); // 去掉扩展名之后的文件名
String classDir = workPath + "/WEB-INF/classes" + string.substring(string.indexOf("/ProductCode/") + 12, string.lastIndexOf('/'));
final String destDir = destPath + "/WEB-INF/classes" + string.substring(string.indexOf("/ProductCode/") + 12, string.lastIndexOf('/'));
File[] files = new File(classDir).listFiles(new FileFilter() {
public boolean accept(File file)
{
if (file.getName().equals(temp + ".class") || file.getName().startsWith(temp + "$"))
{
String dest = destDir + "/" + file.getName();
log.info("原文件路径:" + file.getPath());
log.info("目标文件路径:" + dest);
Handle.copyFile(file, new File(dest));
return true;
}
return false;
}});
if (files.length == 0)
{
log.warn("在路径 " + classDir + " 下找不到编译之后的文件,源文件名:" + filename);
errinfo.append("SVN信息:" + string + "\n在路径 " + classDir + " 下找不到编译之后的文件,源文件名:" + filename + "\n");
}
log.info("该SVN信息处理完成!\n");
}
else // 对于其他类型文件
{
String sourceFile = workPath + "/WEB-INF/classes" + string.substring(string.indexOf("/ProductCode/") + 12);
String destFile = destPath + "/WEB-INF/classes" + string.substring(string.indexOf("/ProductCode/") + 12);
log.info("原文件路径:" + sourceFile);
log.info("目标文件路径:" + destFile);
Handle.copyFile(new File(sourceFile), new File(destFile));
log.info("该SVN信息处理完成!\n");
}
}
else if (string.contains("/WebContent/"))
{
String sourceFile = workPath + string.substring(string.indexOf("/WebContent/") + 11);
String destFile = destPath + string.substring(string.indexOf("/WebContent/") + 11);
log.info("原文件路径:" + sourceFile);
log.info("目标文件路径:" + destFile);
Handle.copyFile(new File(sourceFile), new File(destFile));
log.info("该SVN信息处理完成!\n");
}
else if (string.trim().length() > 0)
{
log.warn("该SVN信息不能被解析!\n");
errinfo.append("\nSVN信息:" + string + "\n该SVN信息不能被解析!\n");
}
}
String errorinfostr = errinfo.toString();
if (errorinfostr.length() > 0)
{
log.error("\n以下是错误信息:\n" +
"-------------------------------------------------------------------------------------------------------------------------" +
errorinfostr);
}
else
{
log.info("运行成功,打包地址:" + destPath);
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (reader != null)
{
reader.close();
}
}
catch (IOException e)
{
}
}
}
public static void setResourceAttributeValue()
{
BufferedReader reader = null;
String string = "";
try
{
reader = new BufferedReader(new FileReader("config/res.txt"));
boolean b = false;
while ((string = reader.readLine()) != null)
{
String key = string.substring(0, string.indexOf('='));
String value = string.substring(string.indexOf('=') + 1).trim();
if (key.equals("destPath"))
{
FatWarMain.destPath = value;
b = true;
}
if (key.equals("workPath"))
{
FatWarMain.workPath = value;
}
}
if (!b)
{
FatWarMain.destPath = String.valueOf(new Date().getTime());
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (reader != null)
{
reader.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
public class Handle
{
/**
* 拷贝文件
* @param source 原文件
* @param dest 目标文件
*/
public static void copyFile(File source, File dest)
{
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try
{
if (!dest.exists())
{
dest.getParentFile().mkdirs();
}
inputStream = new FileInputStream(source);
outputStream = new FileOutputStream(dest);
byte[] bs = new byte[8192];
int size = 0;
while ((size = inputStream.read(bs)) != -1)
{
outputStream.write(bs, 0, size);
}
outputStream.flush();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (outputStream != null)
{
outputStream.close();
}
}
catch (IOException e)
{
}
try
{
if (inputStream != null)
{
inputStream.close();
}
}
catch (IOException e)
{
}
}
}
}
项目里面还得配个LOG4J
# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=info, file, stdout
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%p] %m%n
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
# log4j.appender.logfile.MaxBackupIndex=10
# log4j.appender.logfile.MaxFileSize=102400KB
log4j.appender.file.File=log/fatwar.log
log4j.appender.file.append=false
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%p] %m%n