【问题标题】:copying a jar file in Apache Ant在 Apache Ant 中复制 jar 文件
【发布时间】:2010-04-13 09:40:28
【问题描述】:

构建项目时,我需要将一个 jar 文件从一个目录复制到另一个目录。放置语句:

<copy file="${test.dir}/MyFirstTest.jar" todir="${share.path}"/>

单独构建项目时工作正常,但如果我清理并构建项目,我会收到警告,通知我 ${test.dir}/ 目录尚未创建。如果我理解正确,我应该能够创建一个“目标”并为此操作指定一个依赖项,但我不确定要指定什么依赖项。无论是清理并构建还是仅构建项目,我都需要使用哪些语句来确保生成此副本?

如果需要进一步说明,请告诉我。

仅供参考,我正在使用 Netbeans 6.8 构建我的项目。

【问题讨论】:

    标签: netbeans6.8 ant


    【解决方案1】:

    假设您有 build、dist 和 javadoc 文件夹,请在 clean 中执行此操作。

      <!-- Remove all output generated from this build script -->
      <target name="clean" description="Clean project">
            <delete dir="${build}" />
            <delete dir="${dist}" />
            <delete dir="${javadoc}" />
      </target>
    
      <!-- Initialize all elements needed for the Build -->
      <target name="init">
            <!-- Create the time stamp -->
            <tstamp />
            <!-- Create the build directory structure used by compile
            and copy the deployment descriptors into it-->
            <mkdir dir="${build}/classes" />
            <mkdir dir="${dist}" />
            <mkdir dir="${javadoc}" />
      </target>
    
     <!-- Write a target such as this -->
      <target name="docopy" depends="init" description="do the copy"> 
             <copy file="${test.dir}/MyFirstTest.jar" todir="${dist}"/>
      </target>
    

    当你运行 ant docopy 时,它会先运行 init,然后再运行 docopy 任务。 您可以创建其他包含depends="docopy" 的任务或将复制文件任务移动到init 本身。

    【讨论】:

      【解决方案2】:

      如果我理解正确,我应该能够创建一个“目标”并为此操作指定一个依赖项,但我不确定要指定什么依赖项。

      好吧,如果有意义的话,要么向实际创建${share.path} 的目标添加一个依赖项,要么如果它不存在,则引入一个新目标来创建目录。这通常是通过某种init 目标完成的。然后,像这样添加依赖:

      <target name="copy-jar" depends="target-a, target-b">
          <copy file="${test.dir}/MyFirstTest.jar" todir="${share.path}"/>
      </target>
      

      您也可以简单地尝试在复制库之前创建目录:

      <mkdir dir="${share.path}" failonerror="false">
      <copy file="${test.dir}/MyFirstTest.jar" todir="${share.path}"/>
      

      【讨论】:

        猜你喜欢
        • 2018-02-15
        • 2011-04-19
        • 2014-06-24
        • 2010-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-15
        • 1970-01-01
        相关资源
        最近更新 更多