`
征途2010
  • 浏览: 243774 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java本地增量打包工具

    博客分类:
  • java
阅读更多

在打增量包每次都需要将class文件、jsp文件等拷贝到增量包中比较麻烦。所以就写了一个增量打包工具。
工作原理:根据文件的最后修改时间来打增量。
1、查找Java类增量:根据eclipse工程下的.classpath文件中配置的javasrc目录,来查找修改的java文件,然后将其class文件拷贝到增量目录下。
2、查找jsp文件、配置文件,可以自定义配置。
下面为代码:
XmlReadUtil

package com.aspire.bdc.common.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.log4j.Logger;

/**
 * This class is used to parse an xml configuration file and return specified value.
 */
public final class XmlReadUtil {
    
    private static final String KEY_CONNECTOR = ".";
    
    private static final String DATA_FILE_NAME = System.getProperty("user.dir") + "/.classpath";
    
    private static final Logger LOGGER = Logger.getLogger(XmlReadUtil.class);
    
    private static XmlReadUtil instance;
    
    private XMLConfiguration xmlConfig;
    
    private XmlReadUtil() {
        try {
            xmlConfig = new XMLConfiguration(DATA_FILE_NAME);
        } catch (ConfigurationException e) {
            LOGGER.error(e);
            throw new RuntimeException(e);
        }
    }
    

    

    public static XmlReadUtil getInstance() {
        if (instance == null) {
            instance = new XmlReadUtil();
        }
        return instance;
    }
    
    @SuppressWarnings("rawtypes")
    public List<String> getClasspathEntry() {
        List lstSrc = xmlConfig.getList("classpathentry[@kind]");
        List listPath = xmlConfig.getList("classpathentry[@path]");
        List<String> listResult = new ArrayList<String>();
        if (listPath != null && !listPath.isEmpty()) {
            for (int i = 0; i < listPath.size(); i++) {
                if (lstSrc.get(i).equals("src")) {
                    listResult.add(System.getProperty("user.dir") + File.separator + listPath.get(i));
                }
            }
        }
        return listResult;
    }
    
}



IncremenPublish.java

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

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
* 增量打包工具类
* 
* @author lipeng
* @since 1.0
* @version 2014-8-19 lipeng
*/
public class IncremenPublish {
    
    private static final Logger logger=Logger.getLogger(IncremenPublish.class);
    
    private List<String> javaPath;
    
    private Date lastDate;
    
    private String classPath;
    
    private String webPath;
    
    private String configPath;
    
    private String dbScriptPath;
    
    private static final String tempFileDir = "D:\\patch";
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    
    /**
     * 构造函数
     * 
     * @param lastDate
     * @param webDirName 存放web应用的目录名称,比如说webapps、webContent等
     */
    public IncremenPublish(String hour, String webPath, String configPath, String dbScriptPath) {
        this.javaPath = XmlReadUtil.getInstance().getClasspathEntry();
        this.lastDate = parseDate(hour);
        classPath = IncremenPublish.class.getClass().getResource("/").getPath();
        classPath = classPath.substring(1, classPath.length() - 1);
        if (StringUtils.isNotBlank(webPath)) {
            this.webPath = webPath;
        } else {
            this.webPath = classPath.replace("WEB-INF/classes", "");
        }
        if (StringUtils.isNotBlank(configPath)) {
            this.configPath = configPath;
        } else {
            this.configPath = System.getProperty("user.dir") + File.separator + "config";
        }
        if (StringUtils.isNotBlank(dbScriptPath)) {
            this.dbScriptPath = dbScriptPath;
        } else {
            this.configPath = System.getProperty("user.dir") + File.separator + "db_script";
        }
    }
    
    /**
     * 获取增量文件
     * 
     * @Date 2013-11-9
     * @author lipeng
     */
    public void getIncremenPublishClassFile() {
        System.out.println("######################## patch start#####################");
        try {
            File tempFile = new File(tempFileDir);
            FileUtils.deleteDirectory(tempFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        File file = new File(tempFileDir);
        if (!file.exists()) {
            file.mkdirs();
        }
        // 获取class增量
        System.out.println("**********start class increment**********");
        for (String path : javaPath) {
            moveIncremenFile(path, "classes", new String(path));
        }
        System.out.println("**********start jsp increment**********");
        moveIncremenFile(webPath, null, webPath);
        System.out.println("**********start config increment*********");
        moveIncremenFile(configPath, null, configPath);
        
        System.out.println("**********start jsp increment**********");
        moveIncremenFile(dbScriptPath, null, dbScriptPath);
        System.out.println("######################## patch end #####################");
    }
    
    /**
     * 获取增量文件
     */
    public boolean moveIncremenFile(String javaPath, String dirName, String srcPath) {
        try {
            File file = new File(javaPath);
            if(!file.exists()) return false;
            if (!file.isDirectory()&&!file.getAbsolutePath().contains("vssver2")) {
                Date fileDate = new Date(file.lastModified());
                if (fileDate.getTime() > lastDate.getTime()) {
                    copyFile(srcPath, file, dirName);
                }
            } else if (file.isDirectory() && !file.getAbsolutePath().contains("svn")
                && !file.getAbsolutePath().endsWith("WEB-INF")) {
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(javaPath + File.separator + filelist[i]);
                    if (!readfile.isDirectory()&&!file.getAbsolutePath().contains("vssver2")) {
                        Date fileDate = new Date(readfile.lastModified());
                        if (fileDate.getTime() > lastDate.getTime()) {
                            copyFile(srcPath, readfile, dirName);
                        }
                    } else if (readfile.isDirectory()) {
                        moveIncremenFile(javaPath + File.separator + filelist[i], dirName, srcPath);
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("获取增量文件  Exception:" + e.getMessage());
        }
        return true;
    }
    
    public static void main(String[] args) {
        String hour = null;
        String webpath = null;
        String configPath = null;
        String dbScriptPath = null;
        if (args.length > 0) {
            hour = args[0];
            System.out.println("hout:" + hour + "小时");
        }
        if (args.length > 1) {
            webpath = args[1];
            System.out.println("webPath:" + webpath);
        }
        if (args.length > 2) {
            configPath = args[2];
            System.out.println("configPath:" + configPath);
        }
        if (args.length > 1) {
            dbScriptPath = args[3];
            System.out.println("dbScriptPath:" + dbScriptPath);
        }
        IncremenPublish publish = new IncremenPublish(hour, webpath, configPath, dbScriptPath);
        publish.getIncremenPublishClassFile();
    }
    
    /**
     * parseDate
     * 
     * @Date 2013-11-12
     * @author lipeng
     * @param strDate
     * @return
     */
    public static Date parseDate(String hour) {
        Date date = null;
        if (StringUtils.isBlank(hour) || !hour.matches("[1-9]*")) {
            try {
                date = format.parse(format.format(new Date()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            date = new Date(new Date().getTime() - 3600 * Integer.parseInt(hour));
        }
        return date;
    }
    
    /**
     * 移动增量文件
     * 
     * @Date 2013-11-12
     * @author lipeng
     * @param file
     * @param dirName
     */
    private void copyFile(String srcPath, File file, String dirName) {
        if (dirName == null) {
            copyJspFile(file);
        } else {
            copyClassFile(srcPath, file, dirName);
        }
        
    }
    
    /**
     * 迁移class文件
     * 
     * @Date 2013-11-12
     * @author lipeng
     * @param file
     * @param dirName
     */
    private void copyClassFile(String srcPath, File file, String dirName) {
        File tempJava = new File(srcPath);
        String path1 = file.getPath().replace(tempJava.getAbsolutePath(), classPath).replace("java", "class");
        File tempFile = new File(path1);
        String path2 = path1.replace(classPath, tempFileDir + File.separator + dirName);
        File tempFile1 = new File(path2);
        tempFile1.getParentFile().mkdirs();
        try {
            FileUtils.copyFile(tempFile, tempFile1);
        } catch (Exception e) {
            System.out.println("拷贝class文件出错");
        }
        logger.info("path=" + path2);
        System.out.println("path=" + path2);
    }
    
    /**
     * 迁移jsp文件
     * 
     * @Date 2013-11-12
     * @author lipeng
     * @param file
     */
    private void copyJspFile(File file) {
        String path = file.getPath().replace(System.getProperty("user.dir"), tempFileDir);
        File tempFile = new File(path);
        tempFile.getParentFile().mkdirs();
        try {
            FileUtils.copyFile(file, tempFile);
        } catch (Exception e) {
            System.out.println("拷贝jsp文件出错");
        }
        System.out.println("path=" + path);
    }
    
}

 
ant脚本

<?xml version="1.0" encoding="UTF-8"?>
<project name="anttest" basedir="." default="run.test">
<!-- 定义一个属性 classes -->
<property name="classes" value="func_unit_web/uspc/WEB-INF/classes">
</property>
<property name="lib" value="func_unit_web/uspc/WEB-INF/lib">
</property>
<target name="init">
<path id="ant.run.lib.path">
<pathelement path="${classes}" />
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</path>
</target>
<target name="run.test" id="run">
<!--指明要调用的java类的名称 -->
<java classname="com.aspire.bdc.common.utils.IncremenPublish" fork="true" failonerror="true">
<!--指明要调用的java类的class路径 -->
<classpath refid="ant.run.lib.path"></classpath>
<!--时间间隔,如果是3小时,则扫描时只扫描3小时内修改的文件,不配置则扫描当天修改的文件 -->
<arg value="" />
<!--webpath -->
<arg value=""/>
<!--configpath -->
<arg value=""/>
<!--dbscriptpath -->
<arg value=""/>
</java>
</target>
</project>

 

 

分享到:
评论

相关推荐

    JAVA增量包打包工具

    项目增量补丁包神器桌面版:全自动web增量打包发版,支持gitee/git/svn,支持多模块项目

    java增量更新打包工具

    NULL 博文链接:https://hbxflihua.iteye.com/blog/1706487

    java 增量自动打包 增量更新

    eclipse插件 可打包增量文件,可一键将增量文件更新至测试环境且自动重启 一、使用条件 1、eclipse (eclipse 4.0以上) 2、版本管理用的svn 二、使用方法 1、将下载的jar 放入eclipse\dropins下 2、重启eclipse ...

    svn增量打包小工具

    svn增量打包小工具,自行配置svn相关的账号、本地代码编译后的路径以及本地svn项目所在的路径。可根据版本号或提交时间查找对于的增量文件,选择性进行增量打包。

    文件增量打包工具

    文件增量打包工具,用于对发布增量代码文件进行扫描,打包生成补丁文件

    增量打包工具 patch-generator-desk-v2.0.0

    项目增量打包神器:全自动web增量打包发版,支持git/svn,支持多模块项目

    ideploy 打包工具javaweb 增量打包工具

    ideploy 打包工具javaweb 增量打包工具

    增量打包工具

    增量打包工具

    java 写的根据svn信息生成的增量打包

    java 写的根据svn信息生成的增量打包工具类,有部分路径和判断条件需要修改,修改正确后可直接执行main方法运行。

    java工程增量包 打包工具 非常强大,非常好用

    路径扫描,日期扫描,文件过滤,备注填报,多线程处理速度快, 避免了繁琐的手动打包过程...

    工程增量打包工具

    利用网络上一些零散的思想,然后根据自己的项目需求,自己编写了一个项目增量打包工具,他的原理是从svn上下载工程然后在本地编译,并抽取出增量文件,以工程发布目录结构存储,非常好用,详情请看里面的说明书。

    java增量更新打包JS脚本工具

    适用于Windows 平台需要:microsoft windows based script host 的支持 /* * 根据日期文件比较更新,提取更新文件 **/ var fso = new ActiveXObject("Scripting.FileSystemObject"); //工程路径 ...

    增量自动打包工具(学习参考)

    以前要一个一个拉到服务器部署。累趴,还容易搞错。用这个工具简单方便。需要优化的请自行优化,有优化代码后希望大家也共享下。

    windows增量打包工具

    windows 自动打包工具,其中配置txt以后点击打包 直接回生成相应的zip包,把包放到服务器上 直接解压 替换即可

    java使用datax增量同步代码

    java使用datax增量同步代码,直接放到项目可使用,支持增量,全量可配置,同步一张表只需添加一条记录

    java增量升级工具

    将路径复制到txt中,执行工具,自动打升级包

    stdpm增量打包批处理程序

    rem ** - stdpm增量打包批处理程序 ** rem ** 语法:spack.cmd &lt;dist_path&gt; &lt;zip_file&gt; ** rem ** ** rem ** %1 - dist程序路径,必须的参数 ** rem ** %2 - 打包文件名,默认dist.zip,位置在dist的父目录 ** rem **...

    Java打增量补丁包工具

    https://blog.csdn.net/zou_hailin226/article/details/82836328 我已经将描述写成教程,需要的下载

    Jenkins增量更新部署

    Jenkins增量打包配置,可以实现增量部署,只更新变动的文件!

Global site tag (gtag.js) - Google Analytics