ant中提取properties和xml提高代码复用性
来源:互联网 发布:长得帅什么体验知乎 编辑:程序博客网 时间:2024/06/01 07:49
本文接着上文对前面的示例进行优化,在前面的build.xml中,有些代码是重复的,我们可以将其抽象出来拿来共同使用,比如将一些路劲提取出来放入properties,可以达到共用的同时,还可以增强程序的可维护性,以后路劲变了只需改配置文件,不用改动build.xml。properties维护简单,以键值对形式存放;而xml不单可以提取属性,还可以提取target.
比如在上文中可以将src1,src2,src3的路劲提取出来,放入properties,以后要是路劲变了,直接更改properties就是:
all.properties:
src1=C:\\Users\\Administrator\\Desktop\\test\\src1src2=C:\\Users\\Administrator\\Desktop\\test\\src2src3=C:\\Users\\Administrator\\Desktop\\test\\src3
这样build.xml就变为:
<?xml version="1.0" encoding="UTF-8"?><project name="main" default="build" basedir="."><property name="bin" value="${basedir}\bin"/><property file="all.properties" /><target name ="init"><mkdir dir="${bin}"/></target><target name="run"> <ant dir="${src1}" target="run"/><ant dir="${src2}" target="run"/><ant dir="${src3}" target="run"/></target><target name="clean"><ant dir="${src1}" target="clean"/><ant dir="${src2}" target="clean"/><ant dir="${src3}" target="clean"/></target><target name="build" depends="init,run"><copy todir="${bin}"><fileset dir="${src1}"><include name="*.jar"/></fileset><fileset dir="${src2}"><include name="*.jar"/></fileset><fileset dir="${src3}"><include name="*.jar"/></fileset></copy></target><target name ="rebuild" depends="build,clean"><ant target="clean" /><ant target="build" /></target></project>
其实就是将之前定义src1,src2,src3的那三句话替换成<property file="all.properties" />,即在配置文件中去引用,很容易理解。
下面介绍xml的抽象:
在三个build.xml中都有定义src和dest的代码:<property name ="src" value="src"/> <property name ="dest" value="classes"/>,这样我们可以将其抽象出来,放在一个include.xml中,然后各自引用这个include.xml就代表具有上面两句话了,另外想在各自build中定义共同的target的话,也可以将其定义在include.xml中。如下:
include.xml:
<?xml version="1.0" encoding="UTF-8"?><property name="src" value="src"/><property name="dest" value="classes"/><target name="test"><ant target="run"/></target>
然后各自build中去引用,举一个例子,其他类似:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE project[<!ENTITY share-variable SYSTEM "file:../include.xml">]><project name="HelloWorld1" default="run" basedir=".">&share-variable;<property name ="hello1_jar" value="hello1.jar"/><target name ="init"><mkdir dir="${dest}"/></target><target name="compile" depends="init"><javac srcdir="${src}" destdir="${dest}"/></target><target name="build" depends="compile"><jar jarfile="${hello1_jar}" basedir="${dest}"/></target><target name="run" depends="build"><java classname="test.ant.HelloWorld1" classpath="${hello1_jar}" /></target><target name="clean"><delete dir="${dest}"/><delete file="${hello1_jar}"/></target><target name="rerun" depends="clean,run"><ant target="clean"/><ant target="run"/></target></project>
注意对比变化。
另外,三个build.xml也具有了test这个target。那么我们就可以在整合build.xml中去调用了:
<target name="test"><ant dir="${src1}" target="test" /><ant dir="${src2}" target="test" /><ant dir="${src3}" target="test" /></target>
在本例中实际上就是去分别执行三个build中的run target
上面实际上可以说是利用了抽象的编程思想,达到代码复用和模块分离的效果。
- ant中提取properties和xml提高代码复用性
- Spring配置 在xml和java代码中读取properties文件
- 根据svnant包中的buil.xml和build.properties进行cruisecontrol与ant的集成
- 数据库和XML __ 从数据库中提取xml
- ant 中build.xml
- Spring 解析xml代码提取
- Properties读取Properties和xml文件
- XML的数据提取和保存到数据库中
- 使用ANT构建项目实例2步骤2:properties.xml
- 使用ANT构建项目实例2步骤3:properties.xml
- ant中build.xml使用
- ant build.xml中classpath
- struts.xml和struts.properties
- struts.xml和struts.properties
- struts.xml和struts.properties
- struts.xml和default.properties
- struts.xml和struts.properties
- xml用于提高代码可读性
- DRA成国家标准 音视频步入中国时代
- unity3d Pathfinding插件使用
- 黄玉灵猫你们知道是什么猫吗?
- asp.net mvc 小问题以及 3个retun的不同,和sqlserver语句(将表中的自动增长列清零,复制语句)
- HttpClient 教程
- ant中提取properties和xml提高代码复用性
- 百度之星 另解
- C/C++标准资料
- 镶牙
- Unity中国技术总监刘钢:如何优化基于Unity开发的3D移动游戏
- java ProtocolBuffer 三种修饰符的意义及缺失问题
- 创建快捷方式
- c# 获取xml文件中某些节点下的标签节点
- java数组操作 数组去除重复 移除数组元素