Ant 中的任务依赖顺序

Task dependency order in Ant

我正在尝试学习 ant 并在文档中找到 example build file

<project name="MyProject" default="dist" basedir=".">
  <description>
    simple example build file
  </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist" location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source">
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution">
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up">
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

我假设 clean 步骤应该 运行 在 init 步骤之前,但是这两个步骤都不相互依赖。 init 应该依赖于 clean 步骤吗?如果不是,蚂蚁怎么知道正确的顺序?

当这个ant构建文件运行时,clean目标不会被执行。它不在依赖链中。您必须从命令行显式触发它,例如

ant -f _buildFile.xml clean
ant -f _buildFile.xml

我已经在 bash 文件中完成了该操作。不过,这是一个示例文件,因此它不一定是您最终构建系统的工作方式。

也许做 dist 应该先做 clean(看起来很合理),但这应该是 的一部分dist 依赖项,而不是 init 目标。例如,您可能想要编译而不是执行 clean。所以

<target name="dist" depends="clean, compile"...

或者您可以添加一个新目标,例如 clean_dist,并在那里添加依赖项。然后您可以通过在命令行上指定目标来进行快速分发构建和实际分发构建。