Introduction
I’ve been working with the ANT API whilst putting together a build tool (long story) recently. There isn’t a whole lot of documentation available on the ‘net (in fact, there’s explicitly little documentation in cases), so here are two things that took me a moment or two to work out.
Issues
Non-standard build.xml
For various reasons, our build XML is called builder.xml – this worked fine up until the first javac call, following which ANT began looking for build.xml .
To correct this, you can set the ant.file property to correctly specify your build file.
1 2 |
File buildFile = new File("builder.xml"); p.setUserProperty("ant.file", buildFile.getAbsolutePath()); |
JAVA_HOME not set
Next, the ANT Java API insisted that my JAVA_HOME wasn’t set. I was fairly certain that it was.
1 |
Unable to find a javac compiler; com.sun.tools.javac.Main is not on the classpath. Perhaps JAVA_HOME does not point to the JDK |
After reading the message with somewhat more comprehension, I realized that I just needed to include tools.jar on the command line. Issue sorted.
Build Code
For anyone looking for the actual build code,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
File buildFile = new File("projectName + "\\builder.xml"); Project p = new Project(); ByteArrayOutputStream standardOutput = new ByteArrayOutputStream(); ByteArrayOutputStream errorOutput = new ByteArrayOutputStream(); DefaultLogger consoleLogger = new DefaultLogger(); consoleLogger.setErrorPrintStream(getPrintStream(errorOutput)); consoleLogger.setOutputPrintStream(getPrintStream(standardOutput)); consoleLogger.setMessageOutputLevel(Project.MSG_INFO); p.addBuildListener(consoleLogger); p.setUserProperty("ant.file", buildFile.getAbsolutePath()); p.init(); ProjectHelper.configureProject(p, buildFile); try { p.executeTarget("build"); } catch (BuildException ex) { this.failed = true; } |
Leave a Reply