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

4

7

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

Anodize answered 22/6, 2011 at 13:1 Comment(2)
What exactly is it you need to accomplish?Iotacism
I need to call an ANT build XML file while i click the Button in my Java Swing code.Anodize
M
16

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);
Mitis answered 22/6, 2011 at 13:5 Comment(7)
Thanks Nivas, I already used this code in my program, it doesn't workAnodize
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 doAnodize
I added an answer, because I can't link code in comments. Hope it helps.Buckhound
Thanks Nivas,I don't have any idea about Logger, is it compulsory for this execution..Anodize
@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.Mitis
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 problemAnodize
I do not understand. An example would be greatMitis
B
2

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.

Buckhound answered 22/6, 2011 at 13:43 Comment(0)
V
0

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

Veery answered 22/6, 2011 at 13:36 Comment(1)
hi, this is my ant build can explain it with your code.. <project basedir="." default="build_new" name="finalSetup"><target name="build_new"><exec executable="C:\Program Files\\Inno Setup 5\\ISCC.exe"><arg value="${basedir}\finalInnoSetup.iss" /></exec><echo message="finalSetup.exe ready" /></target></project>"Anodize
E
0

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.

Excursion answered 22/6, 2011 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.