ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "M. Z." <>
Subject How to call imported ant target/task in Java
Date Fri, 05 Aug 2005 03:28:01 GMT
Hi there,
Can anybody advice me how to call an ant target/task, which is "<import/>"ed from another
build file, from within Java code?
Here's my Java code (I got the sample from this mailing list, thanks):
import java.util.*;
public class MyTest
    private Project project;
    public void init(String _buildFile, String _baseDir) throws
        project = new Project();
        try { project.init(); }
        catch (BuildException e)
            { throw new Exception("The default task list could not be loaded."
); }
        // Set the base directory. If none is given, "." is used.
        if (_baseDir == null) _baseDir=new String(".");
        try { project.setBasedir(_baseDir); }
        catch (BuildException e)
            { throw new Exception("The given basedir doesn't exist, or isn't a directory.");
        if (_buildFile == null) _buildFile=new String("a.xml");
        try { ProjectHelper.getProjectHelper().parse(project, new File(_buildFile)); }
        catch (BuildException e)
            { throw new Exception("Configuration file "+_buildFile+" is invalid, or cannot
be read."); }

    public void runTarget(String _target) throws Exception
        // Test if the project exists
        if (project == null) throw new Exception("No target can be launched because the project
has not been initialized. Please call the 'init' method first !");
        // If no target is specified, run the default one.
        if (_target == null) _target = project.getDefaultTarget();
        // Run the target
        try { project.executeTarget(_target);  }
        catch (Exception e)
        { throw new Exception(e.getMessage()); }
        public static void main(String args[])
        MyTest mytest=new MyTest();         
      }catch(Exception e)
Here's my build files:
<project basedir="." default="runtests" name="TestC">
<import file="a.xml"/>
2. a.xml:
<project basedir="." default="runtests" name="TestA">
<target name="runtests" depends="">
<mkdir dir="ttt"/>
If I directly set build file "a.xml" in, everthing works well. However, if I set
"c.xml" then do task "<mkdir dir="ttt"/>" defined in "a.xml" which was imported by "c.xml",
when running, I got an exception. Looks like it can't parse my c.xml which include "import".
BTW, I'm using ant 1.6.2, and running "ant -f c.xml" was ok.
Did I miss any classpath or something? Any idea?

 Start your day with Yahoo! - make it your home page 
  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message