JR 精品文章 - 使用ANT来打debug和release包(通过 实现条件编译)
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » Java入门 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
使用ANT来打debug和release包(通过 实现条件编译)
xiaoyuer 原创   更新:2008-06-10 17:56:24  版本: 1.0   

  在Java中实现条件编译 中我已经介绍了,如何在Java中实现条件编译了。现在根据这个原理再进一步地使用它。在这里我们用来控件日志的打印及记录。

如果我们写一个日志类:
  1. package com.log;
  2. public class MyLogger{
  3.   public static final boolean DEBUG=true;
  4.   public void logError(String message){
  5.        System.err.println(message);
  6.   }
  7.   public void logDebug(String message){
  8.      if(DEBUG)// 调试开关
  9.        System.out.println(message);
  10.   }
  11. }


通过下面这个类来修改MyLogger中DEBUG的值:

  1. package com.ant;
  2. import java.io.File;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. import com.util.FileUtil;
  6. public class AntTools {
  7.     /**
  8.      * @param args
  9.      * @throws Exception 
  10.      */
  11.     public static void main(String[] args) throws Exception {
  12.         if(null==args || args.length<2){
  13.             throw new Exception("请输入两个参数:如java com.handcn.ant.AntTools ./src/com/log/MyLogger.java on");
  14.         }
  15.         String filePath=args[0];
  16.         boolean debug=true;        
  17.         File file=new File(filePath);
  18.         if(!file.exists()){
  19.             throw new Exception(filePath+" 不存在;");
  20.         }
  21.         if("on".equalsIgnoreCase(args[1])){
  22.             debug=true;
  23.         }else if("off".equalsIgnoreCase(args[1])){
  24.             debug=false;
  25.         }else{
  26.             throw new Exception("第二个参数不对,必须是on或者off,on表示打开调试模式,off为关闭调试模式。");
  27.         }
  28.         byte [] buf=FileUtil.getFileContent(file);
  29.         if(null!=buf){
  30.             String content=new String(buf);
  31.             String regStr="DEBUG\\s*=\\s*.+?\\s*;";
  32.             Pattern ptn=Pattern.compile(regStr,Pattern.CASE_INSENSITIVE);
  33.             Matcher matcher=ptn.matcher(content);
  34.             
  35.             if(matcher.find()){
  36.                 int start=matcher.start();
  37.                 int end=matcher.end();
  38.                 String str2=content.substring(start, end).trim();
  39.                 str2=content.replaceFirst(regStr, "DEBUG="+debug+";");
  40.                 file.setWritable(true);
  41.                 FileUtil.saveDataToFile(str2.getBytes(), file, false);
  42.             }
  43.         }
  44.     }
  45. }

最后build.xml的实现,如下:

<!-- where the project source code is found -->
<property name="src.dir" value="./src/" />
<!-- where compiled class files should be left -->
<property name="classes.dir" value="./bin/" />

<path id="classpath">
        <fileset dir="./lib/" includes="**/*.jar" />
</path>
<target name="jar" depends="init" description="build release jar">
        <javac destdir="${classes.dir}" srcdir="${src.dir}" optimize="true" debug="no">
            <!--在这里 debug="no"-->
            <compilerarg value="-Xlint" />
            <classpath>
                <path refid="classpath" />
            </classpath>
        </javac>
        <!--开始打release包-->
        <java classname="com.ant.AntTools">
            <classpath path="${classes.dir}" />
            <arg file="./src/com/log/MyLogger.java" />
            <arg value="off" />
            <!--关闭调试模式-->
        </java>
        <delete dir="${classes.dir}" quiet="true" />
        <mkdir dir="${classes.dir}" />
        <javac destdir="${classes.dir}" srcdir="${src.dir}" optimize="true" debug="no">
            <!--在这里 debug="no" 关闭调试模式-->
            <compilerarg value="-Xlint" />
            <classpath>
                <path refid="classpath" />
            </classpath>
        </javac>
        <jar destfile="${ant.project.name}-release.jar">
            <fileset dir="${classes.dir}">
                <include name="**/*.class" />
                <exclude name="**/test/**/*.class" />
                <!--排除所有test文件夹下的类-->
            </fileset>
        </jar>
        <!--结束打release包-->
        <!--开始打debug包-->
        <java classname="com.ant.AntTools">
            <classpath path="${classes.dir}" />
            <arg file="./src/com/log/MyLogger.java" />
            <arg value="on" />
            <!--打开调试模式-->
        </java>
        <delete dir="${classes.dir}" quiet="true" />
        <mkdir dir="${classes.dir}" />
        <javac destdir="${classes.dir}" srcdir="${src.dir}" optimize="true" debug="yes">
            <!--在这里 debug="yes" 打开调试模式-->
            <compilerarg value="-Xlint" />
            <classpath>
                <path refid="classpath" />
            </classpath>
        </javac>
        <jar destfile="${ant.project.name}-debug.jar">
            <fileset dir="${classes.dir}">
                <include name="**/*.class" />
                <exclude name="**/test/**/*.class" />
                <!--排除所有test文件夹下的类-->
            </fileset>
        </jar>
        <!--结束打debug包-->
    </target>


    这种通过ANT来改代码的方法,相信会给大家带来很大的方便,这里只是其中一个用途,如果大家想到有别的用途,希望能共享一下。[:)]

版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     14       0
作者其它文章: 作者全部文章
评论人:clice 发表时间: Wed Jun 11 19:54:04 CST 2008
收藏了
评论人:neil.he 发表时间: Thu Jul 24 18:34:59 CST 2008
[good][good]

这个文章共有 2 条评论
主题: 接上次  Tomcat 的数据库连接池设置与应用(Mysql篇)之(实例讲解) 上一篇文章
返回文章列表 返回〔Java入门〕
下一篇文章 主题: 深入浅出Java设计模式之迭代器模式


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号