在Java中实现条件编译 中我已经介绍了,如何在Java中实现条件编译了。现在根据这个原理再进一步地使用它。在这里我们用来控件日志的打印及记录。
如果我们写一个日志类:
- package com.log;
- public class MyLogger{
- public static final boolean DEBUG=true;
- public void logError(String message){
- System.err.println(message);
- }
- public void logDebug(String message){
- if(DEBUG)// 调试开关
- System.out.println(message);
- }
- }
通过下面这个类来修改MyLogger中DEBUG的值:
- package com.ant;
- import java.io.File;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import com.util.FileUtil;
-
- public class AntTools {
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- if(null==args || args.length<2){
- throw new Exception("请输入两个参数:如java com.handcn.ant.AntTools ./src/com/log/MyLogger.java on");
- }
- String filePath=args[0];
- boolean debug=true;
- File file=new File(filePath);
- if(!file.exists()){
- throw new Exception(filePath+" 不存在;");
- }
- if("on".equalsIgnoreCase(args[1])){
- debug=true;
- }else if("off".equalsIgnoreCase(args[1])){
- debug=false;
- }else{
- throw new Exception("第二个参数不对,必须是on或者off,on表示打开调试模式,off为关闭调试模式。");
- }
- byte [] buf=FileUtil.getFileContent(file);
- if(null!=buf){
- String content=new String(buf);
- String regStr="DEBUG\\s*=\\s*.+?\\s*;";
- Pattern ptn=Pattern.compile(regStr,Pattern.CASE_INSENSITIVE);
- Matcher matcher=ptn.matcher(content);
-
- if(matcher.find()){
- int start=matcher.start();
- int end=matcher.end();
- String str2=content.substring(start, end).trim();
- str2=content.replaceFirst(regStr, "DEBUG="+debug+";");
- file.setWritable(true);
- FileUtil.saveDataToFile(str2.getBytes(), file, false);
- }
- }
- }
- }
最后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来改代码的方法,相信会给大家带来很大的方便,这里只是其中一个用途,如果大家想到有别的用途,希望能共享一下。
|
|