·  ANT – Execute ANT task from JAVA

 

- Info:

  - This tutorial shows how to execute ANT task from JAVA.

 

- JARs:

  - ant.jar

  - ant-launcher.jar

 

- Example:

  - Create ANT Script Test.xml

  - Create JAVA Console Application D:\Temp\ANT\Test.java

 

    Test.java

    import java.io.File;

    import org.apache.tools.ant.Project;

    import org.apache.tools.ant.ProjectHelper;

    import org.apache.tools.ant.helper.ProjectHelperImpl;

    

    public class Test {

    

      public static void main(String[] args) {

        

        Project       ant    = new Project();   

                      ant.init();

                      

        ProjectHelper helper = new ProjectHelperImpl();

                      helper.parse(ant, new File("Test.xml"));

    

                      ant.executeTarget("default");

                      

      }

    

    }

 

    Test.xml

    <?xml version="1.0" encoding="UTF-8"?>

    

    <project name="MyAntProject" default="default" basedir=".">

          

      <target name="default">

        <mkdir dir = "WEB-INF"/> 

      </target>    

      

    </project>

 

- Test:

  - Start MSDOS

  - cd D:\Temp\ANT

  - javac -cp D:\Installed\Programming\apache-ant-1.7.1\lib\ant.jar Test.java

  - java  -cp .;D:\Installed\Programming\apache-ant-1.7.1\lib\ant.jar;D:\Installed\Programming\apache-ant-1.7.1\lib\ant-launcher.jar Test