javaanttaskdef

How to execute taskdef actions based on requirement among the list of taskdef actions defined in the build.xml?


I have a build.xml which has different taskdef actions.

while running from the command line i want to invoke the taskdef actions based on the requirement like we can do for ant targets.

My question is how to run the taskdef actions from the command line. Attaching the sample code here i want to run only the first taskdef helloworld only from the command line.

<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="MyTask" basedir="." default="use">
        <taskdef name="helloworld" classname="HelloWorld" classpath="${ant.project.name}.jar"/>
        <helloworld/>
<taskdef name="helloworld1" classname="HelloWorld1" classpath="${ant.project.name}.jar"/>
        <helloworld1/>
<taskdef name="helloworld2" classname="HelloWorld2" classpath="${ant.project.name}.jar"/>
        <helloworld2/>
</project>

Solution

  • Create a separate target for each task as follows. Note how the default "use" target will run all three tasks:

    <project name="MyTask" basedir="." default="use">
    
        <target name="use" depends="helloworld,helloworld1,helloworld2"/>
    
        <target name="helloworld">
            <taskdef name="helloworld" classname="HelloWorld" classpath="${ant.project.name}.jar"/>
            <helloworld/>
        </target>
    
        <target name="helloworld1">
            <taskdef name="helloworld1" classname="HelloWorld1" classpath="${ant.project.name}.jar"/>
            <helloworld1/>
        </target>
    
        <target name="helloworld2">
            <taskdef name="helloworld2" classname="HelloWorld2" classpath="${ant.project.name}.jar"/>
            <helloworld2/>
        </target>
    </project>