Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call Ant or NSIS scripts from Java code?

Tags:

java

ant

nsis

Is it possible to call Ant or NSIS scripts programmatically from Java code at runtime? If so, how?

like image 287
Aerrow Avatar asked Jun 22 '11 13:06

Aerrow


2 Answers

You can call ant scripts from Java code.

See this article (scroll down to the "Running Ant via Java" section) and this article:

   File buildFile = new File("build.xml");
   Project p = new Project();
   p.setUserProperty("ant.file", buildFile.getAbsolutePath());
   p.init();
   ProjectHelper helper = ProjectHelper.getProjectHelper();
   p.addReference("ant.projectHelper", helper);
   helper.parse(p, buildFile);
   p.executeTarget(p.getDefaultTarget());

Update

I tried with the following ant file , it did not "tell" anything (no console output), but it worked: the file was indeed moved

   <project name="testproject" default="test" basedir=".">
      <target name="test">
        <move file="test.txt" tofile="test2.txt" />
      </target>
   </project>

And when I try it again (when there is no test.txt to move(it is already moved)), I got an java.io.FileNotFoundException.

I think this is what you would expect when you run something from Java.

If you want the console output of the ant tasks, you might want to add a Logger as a build listener.

From @Perception's answer below.

   DefaultLogger consoleLogger = new DefaultLogger();
   consoleLogger.setErrorPrintStream(System.err);
   consoleLogger.setOutputPrintStream(System.out);
   consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
   p.addBuildListener(consoleLogger);
like image 61
Nivas Avatar answered Sep 28 '22 02:09

Nivas


Too expand on Nivas' answer - his solution is correct, you are not seeing output from your program because you haven't attached any loggers to your project.

DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);

This is just basic setup, theres alot more you can do with the Ant Java API.

like image 42
Perception Avatar answered Sep 28 '22 01:09

Perception