An <import>ed file is (nearly) the same as coded directly inside (at the end of) your main buildfile.
Jan >-----Ursprüngliche Nachricht----- >Von: M. Z. [mailto:[EMAIL PROTECTED] >Gesendet: Freitag, 5. August 2005 05:28 >An: [email protected] >Betreff: How to call imported ant target/task in Java > >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 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 > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
