You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Frank Harnack <ha...@mail05.harlau.de> on 2005/09/07 21:46:39 UTC

Re: Calling Ant from Java program - Help with ProjectHelper class!]

Hi Stephen,

I use another way to call ant from within a java program and it seems to 
be working.
I call Launcher.main(args) (ant 1.6.4).
I don't know, if this way is  recommended. It is only the result of some 
"experiments".
Maybe your problem doesn't appear, if you use this calling mechanism.

Here is a code example:

######################
..

import org.apache.tools.ant.launch.Launcher;

..

// set new security manger to catch System.exit in
//org.apache.tools.ant.Main

// backup current security manager
SecurityManager osm = System.getSecurityManager();

// set special security manager
NoSystemExitSecurityManager nsm = new NoSystemExitSecurityManager();
System.setSecurityManager(nsm);

// start ant launcher
String args[] = {"-buildfile", buildFile,
                  "-logfile", antLogFile,
                  "startTarget"};

Launcher.main(args);
retVal = 
Integer.parseInt(System.getProperty(NoSystemExitSecurityManager.exitStatusSystemProperty));

// restore security manager
System.setSecurityManager(osm);
######################

The special security manger is necessary to prevent, that the calling 
java program exit then ant have finished its work.
I am no expert in using security managers and I don't know if restoring 
it after calling ant is really necessary.
Maybe there is a nicer way to pass the return value too.
Nevertheless here is the code of NoSystemExitSecurityManager:

##############
import java.security.Permission;

public class NoSystemExitSecurityManager extends SecurityManager{

     public static final String exitStatusSystemProperty = "exitStatus";

     /** If a System.exit occurs, this security manger throws a 
SecurityException and
         stores the exit status into a system property 
(->exitStatusSystemProperty). **/

     public void checkExit(int status) {
 
System.setProperty(exitStatusSystemProperty,Integer.toString(status));
         throw new SecurityException("Please ignore this exception!");
     }

     public void checkPermission(Permission perm) {

     }
}
################

Regards

Frank

-- 
Frank Harnack
47269 Duisburg
Deutschland (Germany)

-----Ursprüngliche Nachricht-----
Von: Stephen Nesbitt [mailto:snesbitt@cobaltgroup.com]
Gesendet: Mittwoch, 7. September 2005 04:25
An: user@ant.apache.org
Betreff: Calling Ant from Java program - Help with ProjectHelper class!


All:

I am trying to invoke an Ant build from within a java program. The build
system itself includes both:
- a namespace declarations (<project 
xmlns:ivy="antlib:fr.jayasoft.ivy.ant") -
- an <import task.

>>From what I can gather, the basic sequence for invoking ant is the following:
project = new Project()
project.init()
helper = ProjectHelperxxx()
helper.parse( ant, File(<buildfile>))

The API docs reference 2 helpers - ProjectHelper2 and ProjectHelperImpl. 
I can't get either to work.

If I use ProjectHelperImpl the parse fails with an "Unexpected attribute
"xmlns:ivy" error. If I remove the remove the namespace declaration the 
parse
suceeds

If I use ProjectHelper2 then I avoid the namespace error, but the import 
fails
with a null pointer exception and an unknown element error.

Anybody have any clues/hints on what I am missing?

Thanks!

-steve
-- 


Stephen Nesbitt
Senior Configuration Management Engineer
The Cobalt Group
snesbitt@cobaltgroup.com
x8271



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