一、前言
关于项目发布包含以项目war/zip的方式发布整体升级更新、局部功能组件以jar的方式升级更新等场景,下面通过UPGrade类代码完整实现示例分别实现warUpgrade/jarUpgrade/zipUpgrade的升级发布,详情参见示例部分。
二、代码示例
import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileNotFoundException;@b@import java.io.FileOutputStream;@b@import java.io.FilenameFilter;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.util.zip.ZipEntry;@b@import java.util.zip.ZipException;@b@import java.util.zip.ZipFile;@b@import java.util.zip.ZipInputStream;@b@@b@ @b@public class UPGrade {@b@ @b@ private static String project = "D:\\tomcat\\webapps\\myproject";@b@ @b@ public static void main(String[] args) {@b@ @b@ if(args.length != 2){@b@ log("please specify the upgrade dir or file and the dest project path!");@b@ return;@b@ }@b@ @b@ File upfile = new File(args[0]);@b@ project = args[1];@b@ if(!upfile.exists()){@b@ log(upfile.getAbsolutePath() + " file not exist, pls check it !");@b@ return;@b@ }@b@ @b@ File[] wars = null;@b@ @b@ if(upfile.isFile()){@b@ wars = new File[]{upfile};@b@ } else if(upfile.isDirectory()){@b@ wars = upfile.listFiles(new FilenameFilter() {@b@ @Override@b@ public boolean accept(File dir, String name) {@b@ if(name.endsWith(".war") || name.endsWith(".jar") || name.endsWith(".zip"))@b@ return true;@b@ return false;@b@ }@b@ });@b@ }@b@ @b@ if(wars != null && wars.length > 0){@b@ @b@ for(File war : wars){@b@ if(war.getName().endsWith(".war"))@b@ warUpgrade(war);@b@ else if(war.getName().endsWith(".jar"))@b@ jarUpgrade(war);@b@ else if(war.getName().endsWith(".zip"))@b@ zipUpgrade(war); @b@ }@b@ @b@ } @b@ }@b@ @b@ private static void warUpgrade(File war){@b@ ZipFile zipfile = null;@b@ ZipInputStream zipis = null;@b@ try {@b@ zipfile = new ZipFile(war);@b@ zipis = new ZipInputStream(new FileInputStream(war));@b@ @b@ ZipEntry entry = null;@b@ while ((entry = zipis.getNextEntry())!= null) {@b@ if(compare(entry.getName())){@b@ writefile(zipfile.getInputStream(entry), entry.getName());@b@ }@b@ }@b@ return;@b@ } catch (ZipException e) {@b@ e.printStackTrace();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ } finally {@b@ try {@b@ zipis.close();@b@ zipfile.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@ }@b@ @b@ private static void writefile(InputStream input, String entryName) throws IOException{@b@ @b@ File outFile = new File(project + "/" + entryName);@b@ @b@ outFile.getParentFile().mkdir();@b@ @b@ if(!outFile.exists()) outFile.createNewFile();@b@ if(!outFile.canWrite())@b@ throw new IOException(outFile.getAbsolutePath() + " can not be write!");@b@ @b@ OutputStream output = new FileOutputStream(outFile);@b@ @b@ byte[] buf = new byte[4096];@b@ int cnt = 0;@b@ while ((cnt = input.read(buf)) > 0) {@b@ output.write(buf, 0, cnt);@b@ }@b@ log(entryName + " is upgraded");@b@ output.flush();@b@ output.close();@b@ input.close();@b@ @b@ }@b@ @b@ private static boolean compare(String name){@b@ if(name.indexOf(".") > 0 @b@ && !name.endsWith("web.xml")@b@ && !name.startsWith("META-INF"))@b@ return true;@b@ return false;@b@ }@b@ @b@ private static void jarUpgrade(File war){@b@ try {@b@ @b@ writefile(new FileInputStream(war), "WEB-INF/lib/" + war.getName());@b@ @b@ } catch (FileNotFoundException e) {@b@ e.printStackTrace();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@ @b@ private static void zipUpgrade(File war){@b@ warUpgrade(war);@b@ }@b@ @b@ private static void log(String msg){@b@ System.out.println(msg);@b@ }@b@}