Hi,

I'm not sure if I should post my query here, but I
really need help and coundn't get a clear answer from
user's alias or other places. 

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 how to implement it. It
turned out to me that the main file with
<import> can't be parsed. I'd like to know how to
correct it.

Here's my Java code:
---------------------------------------------
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 are 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 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.

Any advice would be appreciated!

Thanks,
Amy

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to