Friday, March 16, 2012

Ant script for create or delete directory

Ant Task for Deleting directories:
Below is an ant task that deletes the directory specified to 'dir' attribute of delete tag.
  1. <target name="delete-dir" >
  2.     <delete dir="build}"/>
  3.     <echo> /* Deleted existing directory */ </echo>
  4. </target>

The delete task deletes 'build' directory including all the files and subdirectories of 'build'.
The echo task just prints the message in console.

  1. <target name="delete-dir" >
  2.     <delete>
  3.         <fileset dir="." includes="**/*.java"/>
  4.     </delete>
  5. </target>

Here the delete task deletes all files with the extension '.java' from the current directory(represented by '.') and any subdirectories of it.

  1. <target name="delete-dir" >
  2.     <delete includeEmptyDirs="true">
  3.         <fileset dir="build"/>
  4.     </delete>
  5. </target>
Here the deletetask deletes all files and subdirectories of 'build', including build itself.

  1. <target name="delete-dir" >
  2.     <delete includeEmptyDirs="true">
  3.         <fileset dir="build" includes="**/*"/>
  4.     </delete>
  5. </target>
Here the delete task deletes all files and subdirectories of build, without build itself.

Ant Task for Making Directory:
Below is an ant task for making required directories specified to 'dir' attribute of 'mkdir' task.
  1. <target name="create-dir" >
  2.     <mkdir dir="build"/>
  3.     <echo> /* Created Directory */ </echo>
  4. </target>

The mkdir task creates a directory 'build' in to the root directory.
Note: mkdir creates the directory if not exists only, if exists it does nothing.

No comments:

Post a Comment