码迷,mamicode.com
首页 > 其他好文 > 详细

ant中提取properties和xml提高代码复用性

时间:2014-05-24 20:59:04      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:ant   property   xml   target   抽象   

本文接着上文对前面的示例进行优化,在前面的build.xml中,有些代码是重复的,我们可以将其抽象出来拿来共同使用,比如将一些路劲提取出来放入properties,可以达到共用的同时,还可以增强程序的可维护性,以后路劲变了只需改配置文件,不用改动build.xml。properties维护简单,以键值对形式存放;而xml不单可以提取属性,还可以提取target.

比如在上文中可以将src1,src2,src3的路劲提取出来,放入properties,以后要是路劲变了,直接更改properties就是:

all.properties:

src1=C:\\Users\\Administrator\\Desktop\\test\\src1
src2=C:\\Users\\Administrator\\Desktop\\test\\src2
src3=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提高代码复用性,布布扣,bubuko.com

ant中提取properties和xml提高代码复用性

标签:ant   property   xml   target   抽象   

原文地址:http://blog.csdn.net/u010142437/article/details/26602567

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!