ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "M. Z." <communitysubscript...@yahoo.com>
Subject Help!! How to call imported ant target in Java
Date Mon, 08 Aug 2005 02:09:33 GMT
Hi,

This is to draw your attention to my query, as I
really need help on this. 

I have to call a main build file from java code to do
a task. But the task has to be  defined in another
build file imported to the main file. Then I did a
simple test (as below) to see who to implement it. It
turned out to me that seems the main file with
<import> can't be parsed. I'd like to know how to
correct it.

Any advice would be appreciated!

Thanks,
Amy

--- "M. Z." <communitysubscription@yahoo.com> wrote:

Here's my Java code (I got the sample from this
mailing list, thanks):
---------------------------------------------
import org.apache.tools.ant.*;
import java.io.*;
import java.util.*;
 
public class MyTest
{
    private Project project;
    public void init(String _buildFile, String
_baseDir) throws
Exception
    {
        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[])
      {
      try{
        
        MyTest mytest=new MyTest();         
        mytest.init("c.xml",".");         
        mytest.runTarget(null);
      }catch(Exception e)
      {
        e.printStackTrace();
      }
  }
}
---------------------------------------------
 
Here's my build files:
1.c.xml:
<project basedir="." default="runtests" name="TestC">
<import file="a.xml"/>
</project>
 
2. a.xml:
<project basedir="." default="runtests" name="TestA">
<target name="runtests" depends="">
<mkdir dir="ttt"/>
</target>
</project>
 
--------------------------------------
If I directly set build file "a.xml" in MyTest.java,
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?
 
Thanks,
Amy


		
____________________________________________________
Start your day with Yahoo! - make it your home page 
http://www.yahoo.com/r/hs 
 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message