您的位置:首页 > 其它

ant 执行多个构建文件

2012-04-10 17:38 435 查看
如果需要在一个构件文件中通过Ant命令去执行另一个Ant项目,或者说要执行本项目的子项目的构件文件。这时Ant这个核心任务就派上用场了。简单地说Ant任务的作用就相当于在命令行通过Ant命令执行构件文件。值得注意的是Ant任务依赖于target元素,需要在某个target元素中使用,不能在target元素外独立使用。

Ant任务还可以实现类似Java程序间的继承关系,不过对于这个核心任务来说只继承property元素和reference属性。举个例子说,项目A中包含通过Ant任务去执行的项目B,而项目B中需要用到项目A中的属性C,那么可以通过设置的Ant任务中的inheritAll属性,使得在B项目中可以使用项目A中的属性C。从另一方面看,这也符合XP(极限)编程中的细分功能的思想,可以把大的项目切分为几分小项目,然后通过Ant工具进行整合。

项目结构:

project

-build.xml

-build1.xml

-build2.xml

-build3.xml

-file.properties

build.xml:

<?xml version="1.0"?>

<project default="run" basedir="." name="MyProject">

<property file="user.properties"/>

<target name="run">
<ant antfile="${file1}" dir="." output="file1.log" target="call"/>
<ant antfile="${file2}" dir="." />
<ant antfile="${file3}" dir="." />
</target>

</project>


build1.xml

<?xml version="1.0"?>

<project default="init" basedir="." name="MyProject1">

<target name="init">
<echo message="This is in Project1's init target."/>
</target>

<target name="call">
<echo message="The call in Project1"/>
</target>

</project>


build2.xml

<?xml version="1.0"?>

<project default="call" basedir="." name="MyProject2">

<target name="init">
<echo message="This is in Project2's init target."/>
</target>

<target name="call" depends="init">
<echo message="The call in Project2"/>
</target>

</project>


build3.xml

<?xml version="1.0"?>

<project default="call" basedir="." name="MyProject3">

<target name="init">
<echo message="This is in Project3's init target."/>
</target>

<target name="call">
<echo message="The call in Project3"/>
</target>

</project>


file.properties

file1=build1.xml
file2=build2.xml
file3=build3.xml


运行结果:

Buildfile: C:\hxw\workspace\hibernate\build.xml
run:
call:
[echo] The call in Project1
init:
[echo] This is in Project2's init target.
call:
[echo] The call in Project2
call:
[echo] The call in Project3
BUILD SUCCESSFUL
Total time: 78 milliseconds


本文出自 “坠落凡间的天使” 博客,请务必保留此出处/article/7377284.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: