tags:

views:

610

answers:

1

I'm using ant 1.6.2 and am trying to set up a task that will compare a source and a target directory, identify all the subdirectories that exist in the source directory and delete liked named subdirectories in the target directory.

So, say the source directory has sub directories sub1, sub2, and sub3 in it and the target directory has sub1, sub2, sub3, and sub4 in it then I'd like to delete sub1, sub2, and sub3 from target dir.

I thought I could do this by using a FileSelector to identify all directories in source that are present in target. However, I can't get the <type> file selector to ever return a match for directories.

Ultimately, I figured I'd do something like:

<fileset id="dirSelector" dir="${install.dir}">
  <type type="dir"/>
  <present targetdir="${dist.dir}"/>
</fileset>

I started by just trying to list the directories present in the source directory and print them out:

<fileset id="dirSelector" dir="${install.dir}">
  <type type="dir"/>
</fileset>
<property name="selected" refid="dirSelector" />
<echo>Selected: ${selected}</echo>

However, I never get anything printed with the type selector set to directory. If I change the type to file I do get files printed out.

Is there a better way to accomplish what I'm trying to do? Am I missing something in my use of the type selector?

A: 

Without writing a custom Ant task, this is going to be a bit messy. The following should do the trick if you're happy to use the ant-contrib library. It's a bit of a hack (especially the way it uses properties) but it seems to work ok.

<project name="stackoverflow" default="delete_target_dirs">

  <taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
      <pathelement location="ant-contrib-1.0b3.jar"/>
    </classpath>
  </taskdef>

  <property name="src.dir" value="src"/>
  <property name="target.dir" value="target"/>

  <target name="delete_target_dirs">

    <for param="file">
      <path>
        <dirset dir="${src.dir}">
          <include name="**"/>
        </dirset>
      </path>

      <sequential>
        <basename property="@{file}_basename" file="@{file}" />
        <available property="@{file}_available" file="${@{file}_basename}" filepath="${target.dir}" />
        <if>
          <equals arg1="${@{file}_available}" arg2="true"/>
          <then>
            <delete verbose="true">
              <dirset dir="${target.dir}" includes="${@{file}_basename}"/>
            </delete>
          </then>
        </if>                
      </sequential>
    </for>

  </target>

</project>
Shane Bell
Thanks for the help. I don't currently use the ant contrib library so I figured I'd try the custom task route you suggested first. That ended up being fairly easy and worked out great. Thanks.
Eric Rosenberg