Show List

Ant Service Types

Ant Service Types (also called Data Types) provide specific features such as ways to group the files, folders so that tasks can be executed on multiple items.  Here are some examples:
  • Description type: Describes a project
  • PatternSet: Groups of patterns
  • DirSet: Groups of directories
  • FileSet: Groups of files
  • FileList: Explicit list of files
  • FileMapper: Translates filenames
  • FilterReader: Custom class that filters out files
  • FilterChain: Series of FilterReaders to further filter files
  • FilterSet: Groups of filters
  • Selectors: Provides more control over file selection
  • ZipFileSet: A set of zip files.

Here is a sample program using description, patternset and fileset.

build.xml
<?xml version="1.0" ?>
<project name="FirstProject" basedir="." default="run">
    <property file="build.properties"/>

    <!-- description type -->
    <description>
    description of the project
    </description>

    <target name="clean">

        <!-- Task to delete directory -->
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile" depends="clean">
        <!-- Task to create directory -->
        <mkdir dir="${classes.dir}"/>
        <!-- Task to compile java classes -->
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>

        <!-- copy all except .java files from source to target directory -->
        <copy todir="${classes.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java"/>
        </copy>


        <!-- copy all .java files from backup to archive directory -->
        <copy todir="${archive.dir}">
            <fileset dir="${backup.dir}" casesensitive="yes">
                <patternset id="non.test.sources">
                    <include name="**/*.java"/>
                    <exclude name="**/*Test*"/>
                </patternset>
            </fileset>
        </copy>

        <!-- copy all .java files from src to backup directory -->
        <copy todir="${backup.dir}">
            <fileset dir="${src.dir}" casesensitive="yes">
                <patternset refid="non.test.sources"/>
            </fileset>
        </copy>

    </target>

    <target name="jar" depends="compile">
        <!-- Task to create directory -->
        <mkdir dir="${jar.dir}"/>
        <!-- Task to create jar file with Main-Class assignment -->
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <!-- Task to run the jar. Fork value true enables class execution in another JVM-->
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
    </target>

</project>

build.properties
src.dir=src
build.dir=build
classes.dir=${build.dir}/classes
jar.dir=${build.dir}/jar
main-class=HelloWorld
archive.dir=archive
backup.dir=backup


Source code:
https://github.com/it-code-lab/ant-service-types

    Leave a Comment


  • captcha text