【问题标题】:How to force a final ant target to execute regardless of dependencies无论依赖关系如何,如何强制执行最终的 ant 目标
【发布时间】:2011-12-06 16:43:30
【问题描述】:

我有一个基本的 ant 脚本,我在其中将一组文件复制到任何目标之外的目录中。然后,我想在任何/所有目标运行后清理这些文件,而不管依赖关系如何。我遇到的主要问题是目标可以是“编译”或“部署战争”,所以我不能盲目地从“编译”中调用“清理”目标,因为接下来可能会调用“部署战争”。而且我不能盲目地从“部署战争”中调用,因为它可能不会被调用。如何定义在所有其他必要目标完成(失败或成功)后将被调用的目标?下面的“cleanUpLib”目标是我希望在所有/任何任务执行后调用的目标:

<project name="proto" basedir=".." default="deploywar">
...
<copy todir="${web.dir}/WEB-INF/lib">
    <fileset dir="${web.dir}/WEB-INF/lib/common"/>
</copy>
<target name="compile">
    <!-- Uses ${web.dir}/WEB-INF/lib -->
    ....
</target>

<target name="clean" description="Clean output directories">
    <!-- Does not use ${web.dir}/WEB-INF/lib -->
    ....
</target>

<target name="deploywar" depends="compile">
    <!-- Uses ${web.dir}/WEB-INF/lib -->
    ....
</target>

<target name="cleanUpLib">
    <!-- Clean up temporary lib files. -->
    <delete>
        <fileset dir="${web.dir}/WEB-INF/lib">
            <include name="*.jar"/>
        </fileset>
    </delete>
</target>

【问题讨论】:

    标签: ant target


    【解决方案1】:

    要在任何/所有目标之后运行目标而不考虑依赖关系,您可以使用构建侦听器或一些 try/catch/finally 模式,有关详细信息,请参阅:

    【讨论】:

      【解决方案2】:

      Rebse 指出的构建侦听器解决方案看起来很有用 (+1)。

      您可以考虑的另一种选择是“重载”您的目标,如下所示:

      <project default="compile">
      
          <target name="compile" depends="-compile, cleanUpLib" 
              description="compile and cleanup"/>
      
          <target name="-compile">
              <!-- 
                  your original compile target 
              -->
          </target>
      
          <target name="deploywar" depends="-deploywar, cleanUpLib" 
              description="deploywar and cleanup"/>
      
          <target name="-deploywar">
              <!-- 
                  your original deploywar target
              -->
          </target>
      
          <target name="cleanUpLib">
          </target>
      
      </project>
      

      当然,您不能真正在单个 Ant 构建文件中重载,因此目标名称必须不同。

      (我使用了上面的“-”前缀,它是使目标“私有”的一种技巧 - 即,由于 shell 脚本 arg 处理,您不能从命令行调用它们。但当然,您仍然可以加倍- 在 Ant 中成功点击它们)。

      【讨论】:

      • 不起作用。如果 -compile 或 -deploywar 因异常而失败,则永远不会执行 cleanUpLib。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 2018-03-14
      • 2023-02-07
      • 2021-07-01
      • 2020-06-06
      相关资源
      最近更新 更多