The approach your taking is kind of out of sync with the Ant object model. A
much easier approach is to construct your project, then parameterize the
project and finally trigger project execution.  The parameterization of the
project could include:

  a) assign a project basedir
  b) assigning a template build file - for example you could declare a build
file
     that declares a bunch of build phases (e.g. init, prepare, build,
package, 
     test, install) - note: these target don't necessarily need to do
anything
  c) add build listeners - you could create any number of build listeners
that 
     listener for build events declared in your template and these listeners
     could be doing the bulk of the work needed to build your project

In the build listeners you could do the sorts of things your describing in
your code:

  MyTask task = new MyTask();
  task.setProject();
  task.init();
  task.setXxxx( whatever );
  Yyy yyy = task.createYyy();
  yyy.setSomething( true );
  task.execute();

This approach kind of ties into the overall assumptions made by ant which in
turn makes things a lot easier. If your interested I can point you to code
that does the above.

Cheers, Steve.


> -----Original Message-----
> From: Dieter Frej [mailto:[EMAIL PROTECTED] 
> Sent: Monday, 2 January 2006 6:45 PM
> To: user@ant.apache.org
> Subject: calling ant API from java instead of using build.xml 
> from console
> 
> Hi,
> 
> I would like to call ant (1.6.5) from java (1.5.0_04-b05), 
> but I am a little puzzled with all createXYZ, addXYZ, init, 
> and execute methods and in which order they should be called. 
> I wrote it the following way:
> 
> 
> 
> Project project = new Project();
> 
> XmlLogger xlog = new XmlLogger();
> project.addBuildListener(xlog);
> project.init();
> 
> Target target = new Target();
> target.setLocation(new Location("somewhere")); 
> target.setName("blubb");
> 
> Mkdir mkdir = new Mkdir();
> mkdir.setProject(project);
> mkdir.setTaskName("init");
> 
> mkdir.init();
> 
> String destDir = "build/classes";
> mkdir.setDir(new File(destDir));
> 
> target.addTask(mkdir);
> 
> Javac javac = new Javac();
> javac.setProject(project);
> javac.setTaskName("compile");
> 
> javac.init();
> 
> String srcDir = "src";
> 
> javac.setDestdir(new File(destDir));
> javac.setSrcdir(new Path(project, srcDir));
> 
> javac.setDebug(true);
> javac.setDeprecation(false);
> javac.setOptimize(false);
> 
> Path classPath = new Path(project);
> 
> String classesDir = destDir;
> Path classesPath = classPath.createPath(); 
> classesPath.setPath(classesDir); classPath.add(classesPath);
> 
> ArrayList<String> libs = new ArrayList<String>(); 
> libs.add("junit3.8.1/junit.jar");
> 
> Iterator it = libs.iterator();
> while(it.hasNext()) {
>      String lib = (String) it.next();
>      FileSet libFile = new FileSet();
>      libFile.setFile(new File(lib));
> 
>      classPath.addFileset(libFile);
> }
> 
> javac.setClasspath(classPath);
> 
> //Javac.ImplementationSpecificArgument compilerArgs = 
> javac.createCompilerArg(); 
> //compilerArgs.setLine("-Xlint:deprecation -Xlint:unchecked");
> 
> target.addTask(javac);
> 
> project.addTarget("bla", target);
> 
> project.executeTarget("bla");
> 
> 
> 
> This more or less works fine, but I am not sure if it is 
> correct, because the XmlLogger gets a NullPointerException 
> that is caused by the fact that never buildStarted() is called...
> 
> I hope someone is able to help me. Thanks in advance!
> 
> -Didi
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED] For 
> additional commands, e-mail: [EMAIL PROTECTED]
> 


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

Reply via email to