views:

20

answers:

2

I need to copy a jar file from one directory to another when my project is built. Placing the statement:

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

works fine when the project is built alone, but if I clean and build the project I get a warning informing me that the ${test.dir}/ directory hasn't been created yet. If I'm understanding properly I should be able to create a 'target' and specify a dependency for this operation but I'm unsure of what dependency to specify. What series of statements do I need to use to ensure this copy will occur whether I clean and build or just build the project?

Please let me know if any further clarification is needed.

FYI I am using Netbeans 6.8 to build my project.

+1  A: 

Assuming you have build, dist and javadoc folders do this in the 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>

When you run ant docopy It will run init first and then the docopy task. You can create other tasks that have a depends="docopy" in it or move the copy file task to the init itself.

Romain Hippeau
+1  A: 

If I'm understanding properly I should be able to create a 'target' and specify a dependency for this operation but I'm unsure of what dependency to specify.

Well, either add a dependency to the target that actually creates ${share.path} if that makes sense or introduce a new target to create the directory if it doesn't exists. This is typically done is some kind of init target. Then, add the dependency like this:

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

You could also simply try to create the directory before to copy the library:

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