有没有办法让ant多次执行多个依赖目标.考虑一下:
<target name="buildall" depends="mycommon,myDAO" />
<target name="myCommon" depends="initCommon, clean, makedir, compile" description="">
<echo> Build completed for myCommon </echo>
</target>
<target name="myDAO" depends="initDAO, clean, makedir, compile" description="">
<echo> Build completed for myDao </echo>
</target>
Run Code Online (Sandbox Code Playgroud)
我想buildAll来调用myCommon,它调用initCommon,clean,makedir,编译,然后调用调用initDAO,clean,makedire,compile的myDAO.
所以我希望多次执行clean,makedir和compile任务.它们是通用的,基于initXXX任务中设置的属性运行.
我试过这个:
<target name="buildall">
<antcall target="myCommon" />
<antcall target="myDao" />
</target>
Run Code Online (Sandbox Code Playgroud)
但每次都会在任务之外运行一切,这不是我想要的.有什么想法吗?
第一:不要使用<antcall/>它通常是你做错了什么的标志.
现在,要了解Ant不是一种编程语言,您可以告诉Ant您想要做什么以及您希望它完成的顺序.Ant是一种矩阵依赖语言.你只是告诉Ant你想要什么(我想建立这个jar),让Ant弄清楚它应该做什么.Ant最好不要多次运行目标.
例如,无论是myCommon和myDAO调用clean目标.Ant正确地指出,两者都需要clean目标,然后clean在它运行两个目标之前只调用一次.这是Ant假设工作的方式.
所以,让Ant做好自己的工作.首先,在正常情况下,你不能打扫.构建是为了最小化重建以加速任务.如果您尚未修改*.java文件,为什么要强制我重建相应的*.class文件?
第二:不要加倍依赖:例如,如果我想构建目标myDAO,我想编译代码(可能构建一个jar或战争).这是我的myDAO目标应该依赖.现在,当我编译时,我可能需要创建我的目录,当我创建我的目录时,我可能需要执行我的init:
<target name="clean">
<echo>Clean up my working directory to be nice and sparkly</echo>
</target>
<target name="initDAO">
<echo>Initialize stuff for my DAO build</echo>
<echo>Maybe setup some properties?</echo>
</target>
<target name="makedir"
depends="initDAO">
<echo>I need my directories for building.</echo>
<echo>But first, I need to setup stuff"</echo>
</target>
<target name="compile"
depends="makedir">
<echo>I need to compile my dao source"</echo>
<echo>But first, I need to make the necessary directories</echo>
<target>
<target name="myDAO"
depends="compile">
<echo>Here's where I package up my DAO</echo>
<echo>But I have to compile stuff before I can package it</echo>
</target>
Run Code Online (Sandbox Code Playgroud)
注意上面的结构.如果我运行的目标myDAO时,Ant会看的依赖,然后运行initDAO,makedir编译,最后myDAO的一切打包.同样,我有一个clean目标,将我的工作空间恢复到pristine(在任何建立之前)条件,但我不会将其称为包的一部分,因为我不想重做工作.
"啊!",你说,"但我必须清理因为myCommon并myDAO使用相同的目录进行建筑和包装."
那不要那样做.相反,请确保您的两个包使用不同的目标目录进行构建和打包.这样,您就不必将混乱清理干净.而且,您可以更改单个源文件,重建,而不必再次重新编译所有内容.
您可以通过定义宏来处理两者之间的共同点,从而避免麻烦.例如,您可以定义一个编译宏,该编译宏将源目录的名称作为其参数,并根据该源目录名称创建一个destdir,并编译您的common和DAOtargets.
所以,让Ant发挥其魔力.使用依赖关系不是告诉Ant如何做某事,而只是告诉Ant特定目标依赖于另一个目标.让Ant弄清楚执行的顺序.另外,不要将任务设置为要求您擦除目录并重新初始化所有内容.您希望Ant不必重建或重新复制未更改的文件,从而帮助最小化构建.
| 归档时间: |
|
| 查看次数: |
1738 次 |
| 最近记录: |