在打增量包每次都需要将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本地增量打包工具的更多相关文章

  1. java的jar打包工具的使用

    java的jar打包工具的使用 java的jar是一个打包工具,用于将我们编译后的class文件打包起来,这里面主要是举一个例子用来说明这个工具的使用. 在C盘下的temp文件夹下面:         ...

  2. iOS 本地自动打包工具

    1.为什么要自动打包工具? 每修改一个问题,测试都让你打包一个上传fir , 你要clean -> 编译打包 -> 上传fir -> 通知测试.而且打包速度好慢,太浪费时间了.如果有 ...

  3. 增量打包DOC版

    压缩zip的命令有的系统没有的自己去下载一个,否则关闭压缩zip的命令. 有需要的自行更改,这是满足我需求的. 执行 publish.bat 即可,当然需要将文件清单写好放在 resources.tx ...

  4. Java 14 令人期待的 5 大新特性,打包工具终于要来了

    随着新的 Java 发布生命周期的到来,新版本预计将于 2020 年 3 月发布,本文将对其中的 5 个主要特性作些概述. Java 13刚刚发布给开发人员使用不久,最新版本的JDK于2019年9月发 ...

  5. Java&Android反编工具打包

    Java&Android反编工具: 1.Eclipse反编插件:安装到Eclipse后,可以简要的查看jar包中的*.class; 2.DoAPK:反编*.apk为smali和一些资源文件,可 ...

  6. Android自动打包工具aapt详解

    概念 在Android.mk中有LOCAL_AAPT_FLAGS配置项,在gradle中也有aaptOptions,那么aapt到底是干什么的呢? aapt即Android Asset Packagi ...

  7. 产品打包工具的制作,ant,编译源码,打jar包,打tag,打war包,备份release版本等

    1.  在进行打包工具的制作前,需要准备的软件有: svnant-1.3.1 作用是让ant和svn相关联 apache-ant-1.9.7 需要设置ant_home,path,我的配置是: ANT_ ...

  8. 一个灵活的AssetBundle打包工具

      尼尔:机械纪元 上周介绍了Unity项目中的资源配置,今天和大家分享一个AssetBundle打包工具.相信从事Unity开发或多或少都了解过AssetBundle,但简单的接口以及众多的细碎问题 ...

  9. atitit.商业版 源码保护 与 java本地原生代码转换 的方案总结

    atitit.商业版 源码保护 与 java本地原生代码转换 的方案总结 1. 为什么虚拟机语言容易被反编译 1 2. 源码泄露的问题问题 1 3. Excelsior JET 1 4. gcj.的流 ...

  10. Java并发包同步工具之Exchanger

    前言 承接上文Java并发包同步工具之Phaser,讲述了同步工具Phaser之后,搬家博客到博客园了,接着未完成的Java并发包源码探索,接下来是Java并发包提供的最后一个同步工具Exchange ...

随机推荐

  1. 【ASeeker】Android 源码捞针,服务接口扫描神器

    ASeeker是一个Android源码应用系统服务接口扫描工具. 项目已开源: ☞ Github ☜ 如果您也喜欢 ASeeker,别忘了给我们点个星. 说明 ASeeker 项目是我们在做虚拟化分身 ...

  2. jdk17+spring6下打jar包

    由于特定情况,本机下有多个jdk,而JAVA_HOME又只有一个. 本人习惯在命令行下一个命令编译打包程序,如何解决这个问题? 研究了不少时间,得到了两个解决方案: 1.使用bat   --  非常烂 ...

  3. CLR via C# 笔记 -- 托管堆和垃圾回收(21)

    1. 访问一个资源所需的步骤 1). 调用IL指令newobj,为代表资源的类型分配内存(一般使用C# new 操作符来完成). 2). 初始化内存,设置资源的初始状态并使资源可用.类型的实例构造器负 ...

  4. Ubuntu 安装 gitweb + Apache2

    背景 之前已经使用了gerrit进行代码管理,但是在有些代码由于内部技术管理不当而丢失了Review记录. 因此找到了通过gitweb弥补的问题. 做法 安装 sudo apt-get install ...

  5. 基于OMAPL138+FPGA核心板——MCSDK开发入门(下)

    本文测试板卡为创龙科技 SOM-TL138F 是一款基于 TI OMAP-L138(定点/浮点 DSP C674x + ARM9)+ 紫光同创 Logos/Xilinx Spartan-6 低功耗 F ...

  6. 使用sqlcel导入数据时出现“a column named '***' already belongs to this datatable”问题的解决办法

    我修改编码为GBK之后,选择导入部分字段,如下: 这样就不会出现之前的问题了,完美 ----------------------------------------------- 但是出现一个问题,我 ...

  7. 物联网浏览器(IoTBrowser)-基于计算机视觉开发的应用“智慧眼AIEye”

    一.起因 最近毕业在家:),准备筹划社区运营和IoTBrowser升级的事务,遇到了一系列物业管理上的问题,本来出于好心提醒物业人员,结果反被误认为是打广告推销的,当时被激怒一下,后面一想也许这也是一 ...

  8. PowerBuilder现代编程方法X01:PowerPlume的X模式

    临渊羡鱼,不如退而结网. PB现代编程方法X01:PowerPlume的X模式 前言 PowerPlume是PowerBuilder深度创新的扩展开发框架(免费商用). 它不是一个大而全的类库(取决于 ...

  9. 解决方案 | tk.entry数字验证(输入框如何保证只能输入数字)

    from tkinter import * root = Tk() # 创建文本框 entry = Entry(root) entry.pack() # 设置文本框只能输入数字 entry.confi ...

  10. log4cpp的安装及使用

    目录 前言 安装 使用 示例代码 配置文件 编译链接 输出 前言 本文的操作均在ubuntu20.04下进行 安装 本文仅介绍从源码编译安装log4cpp的过程. ①在开始编译前,首先要确保系统中安装 ...