JAvaインクリメンタル更新パッケージツール

13009 ワード

Javaプログラムの増分更新は骨が折れる仕事で、時間を浪費して言わないで、またファイルを漏らして、ファイルの位置を間違えて置くなどの問題が発生しやすいです.比較的良い管理メカニズムがあれば、これらのことをプログラムに任せて自動的に処理すれば手間が省けます.
ツールはまだ未熟ですが、手動でインクリメンタルファイルを選択するよりも便利です.次は直接コードをつけます.
ツールには、次の3つのツールクラス、1つのプロファイル、1つのプログラム起動クラスがあります.
 
ファイルツールクラス
package cn.lihua.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 *        
 * @author lihua
 * @version V1.0
 * @createDate 2012-10-27
 */
public class FileUtil {
	/**  
     *       
     *   
     * @param srcFile  
     *               File  
     * @param destDir  
     *                File  
     * @param newFileName  
     *                  
     * @return         ,    、     、   null    IO  ,  -1  
     */  
    public static long copyFile1(File srcFile, File destDir, String newFileName) {   
        long copySizes = 0;   
        if (!srcFile.exists()) {   
            System.out.println("      ");   
            copySizes = -1;   
        } else if (!destDir.exists()) {   
            System.out.println("       ");   
            copySizes = -1;   
        } else if (newFileName == null) {   
            System.out.println("    null");   
            copySizes = -1;   
        } else {   
            try {   
                BufferedInputStream bin = new BufferedInputStream(   
                        new FileInputStream(srcFile));   
                BufferedOutputStream bout = new BufferedOutputStream(   
                        new FileOutputStream(new File(destDir, newFileName)));   
                int b = 0, i = 0;   
                while ((b = bin.read()) != -1) {   
                    bout.write(b);   
                    i++;   
                }   
                bout.flush();   
                bin.close();   
                bout.close();   
                copySizes = i;   
  
            } catch (FileNotFoundException e) {   
                e.printStackTrace();   
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
        }   
        return copySizes;   
    }   
  
    /**  
     *     (          )  
     *   
     * @param srcFile  
     *               File  
     * @param destDir  
     *                File  
     * @param newFileName  
     *                  
     * @return         ,    、     、   null    IO  ,  -1  
     */  
    public static long copyFile2(File srcFile, File destDir) {   
        long copySizes = 0;   
        if (!srcFile.exists()) {   
            System.out.println("      ");   
            return -1;   
        }
        try {   
            FileChannel fcin = new FileInputStream(srcFile).getChannel();   
            FileChannel fcout = new FileOutputStream(destDir).getChannel();   
            long size = fcin.size();   
            fcin.transferTo(0, fcin.size(), fcout);   
            fcin.close();   
            fcout.close();   
            copySizes = size;   
        } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        
        return copySizes;   
    } 
    
    
    public static void copyDict(File source, File target,long last,String target_pack) {   
        File[] file = source.listFiles();
        for (int i = 0; i < file.length; i++) {   
        	if(file[i].getName().contains("svn")){
        		continue;
        	}        	
            if (file[i].isFile()) {
            	if(file[i].lastModified()>=last){
            		if((file[i].getName().endsWith(".hbm.xml")||file[i].getName().endsWith(".class"))&&
            			notEmpty(target_pack)&&!source.getAbsolutePath().contains(target_pack)){
            			continue;
            		}
            		System.out.println(source.getAbsolutePath());
            		File sourceDemo = new File(source.getAbsolutePath() + "/"  
            				+ file[i].getName());   
            		File destDemo = new File(target.getAbsolutePath() + "/"  
            				+ file[i].getName());   
            		copyFile2(sourceDemo, destDemo);               		
            	}
            }   
            if (file[i].isDirectory()) {
            	
                File sourceDemo = new File(source.getAbsolutePath() + "/"  
                        + file[i].getName());   
                File destDemo = new File(target.getAbsolutePath() + "/"  
                        + file[i].getName());   
                destDemo.mkdir();
                copyDict(sourceDemo, destDemo,last,target_pack);   
            }   
        }  
        
    }   
    
    public static boolean notEmpty(String str) {
    	return str!=null&&!str.isEmpty();
	}
    /**  
     *              
     * @param dir  
     */  
    public static void deleteEmptyDir(File dir) {   
    	 
        if (dir.isDirectory()) {   
            File[] fs = dir.listFiles();   
            if (fs != null && fs.length > 0) {   
                for (int i = 0; i < fs.length; i++) {   
                    File tmpFile = fs[i];   
                    if (tmpFile.isDirectory()) {   
                        deleteEmptyDir(tmpFile);   
                    }   
                    if (tmpFile.isDirectory() && tmpFile.listFiles().length <= 0) {   
                        tmpFile.delete();   
                    }   
                   
                }   
            }   
            if (dir.isDirectory() && dir.listFiles().length == 0) {   
                dir.delete();   
            }           
          
        }   
    } 
    
    public static void main(String[] args) {
		String [] s = "127.0.0.1 - - [14/Sep/2012:11:27:10 +0800] POST /newkyhb/sys/loginlognews.action HTTP/1.1 200 356 0.032".split(" ");
		for (String string : s) {
			System.out.println(string);
		}
    	
	}

}

 
Propertiesファイルツールクラス
package cn.lihua.util;

import java.io.InputStream;
import java.util.Properties;

/**
 * Properties        
 * @author lihua
 * @version V1.0
 * @createDate 2012-10-27
 */
public class PropertiesUtil {
	//private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
	private static final Properties prop = loadPropertiesFile("setting.properties");

	private static Properties loadPropertiesFile(String filePath) {
		InputStream in;
		try {
			in = PropertiesUtil.class.getClassLoader().getResourceAsStream(filePath);
			Properties p = new Properties();
			p.load(in);
			return p;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getString(String key) {
		if (prop != null)
			return prop.getProperty(key);
		return null;
	}
}

 
 
プロジェクトディレクトリツールクラス
package cn.lihua.util;
import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


/**
 *         
 * @author lihua
 * @version V1.0
 * @createDate 2012-9-13
 */
public class WebRootUtil {

	private static Document doc = null;
	
	/**
	 *      
	 * @param xmlFile
	 * @throws Exception
	 */
	public static void init(String xmlFile) throws Exception {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		doc = db.parse(new File(xmlFile));
	}
	
	/**
	 *   .mymetadata  web root folder   
	 * @param xmlFile
	 * @return
	 * @throws Exception
	 */
	public static String getRootName(String xmlFile) throws Exception {
		String webRoot = "WebRoot";
		init(xmlFile);
		NodeList nodeList = doc.getElementsByTagName("attribute");
		for (int i = 0, len = nodeList.getLength(); i < len; i++) {
			Element attribute = (Element) nodeList.item(i);
			if ("webrootdir".equals(attribute.getAttribute("name"))){
				webRoot = attribute.getAttribute("value");
				break;
			}
		}

		return webRoot;
	}
	
	public static void main(String[] args)throws Exception {		
		System.out.println(getRootName("D:\\workspace\
ewkyhb\\.mymetadata")); } }

 
Propertiesプロファイル
#\u4E0A\u4F20\u6587\u4EF6\u5B58\u653E\u8DEF\u5F84
source_path=D\:\\workspace\
ewkyhb target_path=D\:\\update_version update_date=2012-09-13 target_pack=\\jsp

 
 
 
 
クラスの開始
package cn.lihua;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.lihua.util.FileUtil;
import cn.lihua.util.PropertiesUtil;
import cn.lihua.util.WebRootUtil;
/**
 *      
 * @author lihua
 * @version V1.0
 * @createDate 2012-10-27
 */
public class Startup {

	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	
	private static long packed_date;//        
	private static String source_path;//     
	private static String target_path;//    
	private static String target_pack;//class    
	private static String project_new_name;//       
	private static String meta_data_file =".mymetadata";//       
	private static String root_name;//     
	
	
	/**
	 *      
	 * @throws Exception
	 */
	private static  void init()throws Exception{
		String update_date =PropertiesUtil.getString("update_date");		
		packed_date = sdf.parse(update_date).getTime();
		source_path =PropertiesUtil.getString("source_path");
		String project_name = source_path.substring(source_path.lastIndexOf("\\")+1);//    
		project_new_name = project_name + "(" + sdf.format(new Date()) + ")";
		root_name = WebRootUtil.getRootName(source_path + File.separator + meta_data_file);		
		target_pack =PropertiesUtil.getString("target_pack");
		target_path =PropertiesUtil.getString("target_path");
		File target = new File(target_path + File.separator + project_new_name);		
		if(!target.exists()){
			target.mkdirs();
		}	
	}
	
	/**
	 *       
	 */
	public static void copy(){
		
		/**
		 *         
		 */
		FileUtil.copyDict(
				new File(source_path + File.separator + root_name), 
				new File(target_path + File.separator + project_new_name), 
				packed_date,target_pack);
		/**
		 *       
		 */
		FileUtil.deleteEmptyDir(
				new File(target_path + File.separator + project_new_name));
	}
	
	/**
	 *     
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args)throws Exception {
		System.out.println("======================================");
		System.out.println("         ……");
		init();
		System.out.println("======================================");
		System.out.println("          :");
		copy();		
		System.out.println("======================================");
		System.out.println("    ! ");
	}
	
	
	
	
	
}