Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking.I'm 23, born in Romania, student at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff. I know that I look and act differently from most developers, but this is why you will love to work with me! Constantin has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

How to redirect Ant output to a JTextArea

04.08.2011
| 5221 views |
  • submit to reddit

If you need to capture an Ant script output and resend it to an JTextArea (or similar components), you can try a few solutions. For my purposes I solved this task by writing a new logger that extends the DefaultLogger and overwrite the buildFinished, buildStarted and messageLogged methods:


class MyLogger extends DefaultLogger {

@Override
public void buildFinished(BuildEvent be) {
outputTextArea.append("[BUILD FINISHED]\n");
}

@Override
public void buildStarted(BuildEvent be) {
outputTextArea.append("[BUILD STARTED]\n");
}

@Override
public void messageLogged(BuildEvent be) {
outputTextArea.append(be.getMessage() + "\n");
}
}

Next, MyLogger was set to the current Project:
private boolean runAntScript(String scriptName) {

File buildFile = new File(scriptName);
Project project = new Project();
MyLogger myLogger = new MyLogger();

project.setUserProperty("ant.file", buildFile.getAbsolutePath());
myLogger.setErrorPrintStream(System.err);
myLogger.setOutputPrintStream(System.out);
myLogger.setMessageOutputLevel(Project.MSG_INFO);

project.addBuildListener(myLogger);

try {
project.fireBuildStarted();
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", helper);
helper.parse(project, buildFile);
project.executeTarget(project.getDefaultTarget());
project.fireBuildFinished(null);
} catch (Exception e) {
project.fireBuildFinished(e);
return false;
}

return true;
}


Another solution is to implement the org.apache.tools.ant.BuildListener interface. This interface will force you to overwrite more methods but provide more control, as you can see below:
class myBuildListener implements org.apache.tools.ant.BuildListener  {

public void buildFinished(BuildEvent be) {
outputTextArea.append("[BUILD FINISHED]\n");
}

public void buildStarted(BuildEvent be) {
outputTextArea.append("[BUILD STARTED]\n");
}

public void messageLogged(BuildEvent be) {
outputTextArea.append(be.getMessage() + "\n");
}

public void targetFinished(BuildEvent be) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void targetStarted(BuildEvent be) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void taskFinished(BuildEvent be) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void taskStarted(BuildEvent be) {
throw new UnsupportedOperationException("Not supported yet.");
}

}


Finally, a third solution is to send the Ant script output to a pipe:
private boolean runAntScript(String scriptName) {

PipedOutputStream pipeOut = null;
PipedInputStream pipeIn = null;
BufferedReader pipeReader = null;

try {
pipeOut = new PipedOutputStream();
pipeIn = new PipedInputStream(pipeOut);
pipeReader = new BufferedReader(new InputStreamReader(pipeIn));

System.setOut(new PrintStream(pipeOut));
System.setErr(new PrintStream(pipeOut));
} catch (IOException e) {
return false;
}

File buildFile = new File(scriptName);
Project project = new Project();
DefaultLogger consoleLogger = new DefaultLogger();

project.setUserProperty("ant.file", buildFile.getAbsolutePath());
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_VERBOSE);
project.addBuildListener(new myBuildListener());

try {
project.fireBuildStarted();
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", helper);
helper.parse(project, buildFile);
project.executeTarget(project.getDefaultTarget());
project.fireBuildFinished(null);
} catch (Exception e) {
project.fireBuildFinished(e);
return false;
}

return true;
}


Next, you will need a separate thread to read form pipe and put in the text area. This can be implemented through Java multi-threading capabilities in a few lines of code.


Well, there’s my solutions! Please fill free to bring other new and better ones.

From http://e-blog-java.blogspot.com/2011/04/how-to-redirect-ant-output-to-jtextarea.html

Published at DZone with permission of its author, Constantin Alin.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Gervais Blaise replied on Fri, 2011/04/08 - 3:03am

As I have said to you in a private message I have a project who aim to configure and launch Ant scripts from a Gui. I have choosen the BuildListener solution because I think that she is the less difficult solution to implement and she allow us control what and how to log messages. For those who are interested. The project can be found on My google code page.

Anthony Goubard replied on Fri, 2011/04/08 - 5:05pm

You may want to remove the throw new UnsupportedOperationException("Not supported yet."); and leave the methods empty.

For Ant Commander (my file manager in Java) I have chosen the BuildListener for the Ant console that can be used to monitor the executed Ant tasks and for the command line panel that execute DOS commands via Ant and shows the result in a JTextArea.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.