Is it possible to call Ant or NSIS scripts programmatically from Java code at runtime? If so, how?
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With