15

Is there a tutorial on how to run Ant from Java? I got some code from here: Setting JAVA_HOME when running Ant from Java

But haven't been able to make it work. I've been trying to find an example or tutorial on how to actually use it.

Here's what I have so far:

        Project p = new Project();
        p.setUserProperty("ant.file", buildFile.getAbsolutePath());
        p.fireBuildStarted();
        p.init();
        p.executeTarget("default");

But I guess this error:

Exception in thread "main" Target "default" does not exist in the project "null". 
    at org.apache.tools.ant.Project.tsort(Project.java:1912)
    at org.apache.tools.ant.Project.topoSort(Project.java:1820)
    at org.apache.tools.ant.Project.topoSort(Project.java:1783)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
    at com.arthrocare.vss2svn.VSS2SVN.newProcess(VSS2SVN.java:128)
    at com.arthrocare.vss2svn.VSS2SVN.main(VSS2SVN.java:52)
Java Result: 1

I tried specifying the project with:

p.setUserProperty("ant.project.name", "VSS Project");

But no luck.

The ant file specified is correct as it works perfectly from the command line.

UPDATE

After some more searching I got here: http://onjava.com/pub/a/onjava/2002/07/24/antauto.html?page=1

It is a great tutorial.

Here's the code I got a little bit earlier than seeing the code in the answer below:

        Project project = new Project();
        ProjectHelper.configureProject(project, buildFile);
        DefaultLogger consoleLogger = new DefaultLogger();
        consoleLogger.setErrorPrintStream(System.err);
        consoleLogger.setOutputPrintStream(System.out);
        consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
        project.addBuildListener(consoleLogger);
        project.init();
        project.executeTarget(project.getDefaultTarget());

But for some reason the task still fails! I'm using a Visual Source Safe task that needs to read an environment value at runtime but it doesn't see it with this approach. Running the build.xml file manually and with the following code works:

        ProcessBuilder pb = new ProcessBuilder();
        Map env = pb.environment();
        String path = env.get("ANT_HOME");
        System.out.println(path);
        pb.directory(new File(System.getProperty("user.home")));
        pb.command(path + System.getProperty("file.separator")
                + "bin" + System.getProperty("file.separator") + "ant.bat");
        try {
            Process p = pb.start();
        } catch (IOException ex) {
            //
        }
Community
  • 1
  • 1
javydreamercsw
  • 5,363
  • 13
  • 61
  • 106

1 Answers1

19

Is there a tutorial on how to run Ant from Java?

Part of my answer to this question might help:

See this article 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());
Community
  • 1
  • 1
Nivas
  • 18,126
  • 4
  • 62
  • 76
  • As I was going to post the answer to my own question I saw your answer which was a lot like mine. Now I'm getting errors from the task being unable to execute. Running the same task from the command prompt works perfectly. Any idea? – javydreamercsw Jul 18 '11 at 13:49
  • @javydreamercsw, I am not sure I understand. Do you say you get errors when you execute a task from your java code? If so can you post the error? You may want to edit the question and add this... – Nivas Jul 18 '11 at 13:51
  • I added logging based on your link to the other answer and it seems that when I run it from Java it doesn't see an User ariable (windows environment variable) that it sees from the command prompt. – javydreamercsw Jul 18 '11 at 13:52
  • @javydreamercsw can you post the section of the `build.xml` that is *not working* – Nivas Jul 18 '11 at 14:00
  • 1
    I don't think is a build.xml issue is that for some reason the Project approach is not getting the environment variables it needs. I just did the same thing but running from a ProcessBuilder and calling ant from there and it works fine. I'll update the question in a bit. – javydreamercsw Jul 18 '11 at 14:07
  • I'll give you the answer as you pointed me in the right direction. But I couldn't find a way to "see" the environemnt variables from the Project approach. Adding them as properties didn't work either. – javydreamercsw Jul 18 '11 at 14:30
  • 1
    Any way of seeing the environment variables? Using ProcessBuilder I can set those at runtime but couldn't find an equivalent for the ant Project approach. – javydreamercsw Jul 20 '11 at 12:54
  • i'm having the same issue as @javydreamercsw. did you find a solution? – spy Sep 29 '17 at 14:17