Ant - 如何运行来自多个目标的相同依赖项 [英] Ant - How can I run the same depends from multiple targets

查看:24
本文介绍了Ant - 如何运行来自多个目标的相同依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 ant 多次执行多个依赖目标.考虑一下:

Is there a way to get ant to execute multiple depend targets multiple times. Consider this:

<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>

我希望 buildAll 调用 myCommon,后者调用 initCommon、clean、makedir、compile,然后调用 myDAO,后者调用 initDAO、clean、makedire、compile.

I would like buildAll to call myCommon, which calls initCommon, clean, makedir, compile, then call myDAO which calls initDAO, clean, makedire, compile.

所以我希望多次执行 clean、makedir 和 compile 任务.它们是通用的,并基于 initXXX 任务中设置的属性运行.

So I want the clean, makedir and compile tasks to be executed multiple times. They are generic and run based on properties set in the initXXX task.

我试过了:

<target name="buildall">
    <antcall  target="myCommon" />
    <antcall target="myDao" />
</target>

但是每次都运行任务之外的所有内容,这不是我想要的.有什么想法吗?

but that runs everything outside of tasks everytime which is not what I want. Any thoughts?

推荐答案

第一:不要使用 这通常表明你做错了什么.

First: Do not use <antcall/> it's usually a sign you've done something wrong.

现在,请理解 Ant 不是一种编程语言,您可以在其中告诉 Ant 您想要做什么以及您希望它完成的顺序.Ant 是一种矩阵依赖语言.您只需告诉 Ant 您想要什么(我想构建这个 jar),然后让 Ant 弄清楚它应该做什么.Ant 尽最大努力不要多次运行一个目标.

Now, understand that Ant is not a programming language where you tell Ant what you want to do and the order you want it to be done. Ant is a matrix dependency language. You merely tell Ant what you want (I want to build this jar), and let Ant figure out what it should do. Ant does its very best not to run a target multiple times.

例如,myCommonmyDAO 都调用了 clean 目标.Ant 适当地指出,两者都需要 clean 目标,然后在运行两个目标之前调用 clean 一次且仅一次.这就是 Ant 的工作方式.

For example, both myCommon and myDAO call the clean target. Ant duly notes that both require clean target, and then calls clean once and only once before it runs both of your targets. It's the way Ant is suppose to work.

所以,让 Ant 完成它的工作.首先,在正常情况下你不应该打扫.构建应该尽量减少重建以加快任务速度.如果你没有修改过*.java文件,你为什么要强迫我重建对应的*.class文件?

So, let Ant do its job. First, Thou shall not clean under normal circumstances. Builds are suppose to minimize rebuilding in order to speed up a task. If you haven't modified a *.java file, why should you force me to rebuild the corresponding *.class file?

第二:不要加倍依赖:例如,如果我想构建目标myDAO,我想编译代码(可能构建一个jar或war).这就是我的 myDAO 目标应该依赖的全部内容.现在,当我编译时,我可能需要创建我的目录,当我创建我的目录时,我可能需要执行我的初始化:

Second: Don't double up dependencies: For example, if I want to build the target myDAO, I want to compile the code (maybe build a jar or war). That's all my myDAO target should depend upon. Now, when I compile, I might need to make my directory, and when I make my directory, I might need to do my 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>

注意上面的结构.如果我运行目标 myDAO,Ant 将查看依赖项,然后运行 ​​initDAOmakedir、编译,最后 myDAO 打包所有内容.同样,我有一个 clean 目标,它将把我的工作空间恢复到原始(在构建任何东西之前)状态,但我不把它称为包的一部分,因为我不想重做工作.

Note the above structure. If I run the target myDAO, Ant will look at the dependencies and then run initDAO, makedir, compile, and finally myDAO to package everything up. Again, I have a clean target that will restore my working space to pristine (before anything was built) condition, but I don't call it as part of a package because I don't want to redo work.

啊!",你说,但我必须清理,因为 myCommonmyDAO 使用相同的目录来构建和打包."

"Ah!", you say, "But I have to clean up because myCommon and myDAO use the same directories for building and packaging."

不要那样做.相反,请确保您的两个包使用不同的 target 目录进行构建和打包.这样,您就不必从一个到另一个清理烂摊子.而且,您可以更改单个源文件、重新构建,而不必再次重新编译所有内容.

Well don't do that. Instead, make sure your two packages use different target directories for building and packaging. This way, you don't have to clean up the mess from one to another. And, you can change a single source file, rebuild, and not have to recompile everything again.

您可以通过定义来处理两者之间的共同点,从而避免麻烦.例如,您可能能够定义一个编译宏,该宏将源目录的名称作为其参数,它将根据该源目录名称创建一个 destdir,并编译您的公共和 DAO 目标.

You can save yourself from trouble by defining macros to handle stuff in common between the two. For example, you might be able to define a compile macro that takes as its parameters the name of a source directory and it will create a destdir based upon that source directory name and compile your common and DAO targets.

那么,让 Ant 发挥它的魔力吧.使用依赖项不是告诉 Ant 如何做某事,而只是告诉 Ant 一个特定的目标依赖于另一个目标.让 Ant 找出执行顺序.此外,不要将您的任务设置为要求您清理目录并重新初始化所有内容.您希望 Ant 不必重建或重新复制未更改的文件,从而帮助最小化构建.

So, let Ant work its magic. Use dependencies not as a means of telling Ant how to do something, but merely telling Ant that a particular target depends upon another target. Let Ant figure out the order of execution. Also, don't set up your tasks to require you to scrub directories and reinitialize everything. You want Ant to help minimize the build by not having to rebuild or recopy over files that haven't changed.

这篇关于Ant - 如何运行来自多个目标的相同依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆