How to redirect Ant output to a JTextArea
Join the DZone community and get the full member experience.
Join For FreeIf 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 {Next, MyLogger was set to the current Project:
@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");
}
}
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
Interface (computing)
Java (programming language)
Task (computing)
Form (document)
Opinions expressed by DZone contributors are their own.
Comments