Is it possible to call Ant or NSIS scripts programmatically from Java code at runtime? If so, how?
4 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);

- 18,126
- 4
- 62
- 76
-
-
While I calling my ant build by using the above code, there is no error and there is no expected output.Simply no response what i do – Aerrow Jun 22 '11 at 13:32
-
I added an answer, because I can't link code in comments. Hope it helps. – Perception Jun 22 '11 at 13:44
-
Thanks Nivas,I don't have any idea about Logger, is it compulsory for this execution.. – Aerrow Jun 22 '11 at 13:52
-
@BEN Ten: You are welcome. _compulsory_? Your call. Most likely not because what you want in the first place is a Java wrapper of the ant process to call from a Swing UI. Then you are better off with the exceptions. Loggers do help in debugging, of course. – Nivas Jun 22 '11 at 13:54
-
Hi Nivas, i added the Logger code also, and i use Eclipse IDE, When i run the program there is no desired output. There is no Response. this is my problem – Aerrow Jun 22 '11 at 14:08
-
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.

- 79,279
- 19
- 185
- 195
Yes, and Nivas gave the most specific answer and probably the best answer too, but call me old fashioned, I still prefer just doing:
Process child = Runtime.getRuntime().exec(command);
where command is a String with your ant commands or a shell script to your ant commands;
If you don't need all the "bells and whistles", which are pretty cool I admit, then it is less code to manage later. You don't get to step debug the ant calls, my way. The trade off is that your files need to be in special places with special names, but Java is already a little OCD in that regard anyway. (Don't flame me. ;) Java is my favorite language too, but come on, we're talking deployment options here and you know it's a little OCD in the deployment options area, right?) I guess all I'm really suggesting is "It may be over kill to use a commando knife to butter toast." and to consider that as well. ;)
* self edited for preachiness

- 76
- 1
- 2
user804965's solution is one I've implemented before. I had to run some terminal commands from Java, and used the Process and Runtime Java objects. The same idea can be applied to running ant commands.
For example:
ProcessBuilder pb = new ProcessBuilder("path/to/my/script/script.sh");
Map<String, String> env = pb.environment();
Process p = pb.start();
p.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = "";
while ((line = buf.readLine()) != null) {
System.out.println(line);
}
This will run a script file from Java and print the output to the console. Inside script.sh you can do something along the lines of...
cd /path/to/my/build.xml/file/; ant -p
Or call whatever ant script you need to call (ant clean, for example).
You can also likely do this without having the additional sh file, and just call the terminal command from Java. I have not tried this approach though.

- 2,412
- 27
- 31