2

i am trying to run ant script from java program.what is the procedure to execute the program How to run build.xml from java program?

here is how iam trying to implement

Process proc = rt.exec("ant -buildfile D:ant\\trail");

regards, techie

Leonard Brünings
  • 12,408
  • 1
  • 46
  • 66
techie
  • 35
  • 1
  • 9

3 Answers3

5

Check here Execute Ant From Your Application and look at this example:

      Project project = new Project();
      project.init();
      DefaultLogger logger = new DefaultLogger();
      logger.setMessageOutputLevel(Project.MSG_INFO);
      logger.setErrorPrintStream(System.err);
      logger.setOutputPrintStream(System.out);
      project.addBuildListener(logger);

      File buildFile = new File("buildhtml.xml");
      ProjectHelper.configureProject(project, buildFile);
      project.setProperty("ant.file", buildFile.getAbsolutePath());
      project.setProperty("item", "ant");
      project.setProperty("inputdir", "src/items/ant");
      project.setProperty("outputdir", "build/items/ant");
      project.setProperty("graphics.prefix", "../../");
      try {
         project.executeTarget("checkifuptodate");
      } catch(Exception e) {System.err.println(e.getMessage());}
      // rest of program goes here

It is a better solution than calling Runtime.exec

zacheusz
  • 8,750
  • 3
  • 36
  • 60
1

Rather than trying to start a windows executable separately, it'd be a more robust and flexible solution to use the Ant API. Docs are included with ant itself, they are not online...

Matthew Gilliard
  • 9,298
  • 3
  • 33
  • 48
-1

Try Runtime.getRuntime().exec("cmd /c start ant.bat"); taken from How do I run a batch file from my Java Application?

Community
  • 1
  • 1
Leonard Brünings
  • 12,408
  • 1
  • 46
  • 66