DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > How to redirect Ant output to a JTextArea

How to redirect Ant output to a JTextArea

A. Programmer user avatar by
A. Programmer
·
Apr. 08, 11 · Java Zone · Interview
Like (0)
Save
Tweet
6.64K Views

Join the DZone community and get the full member experience.

Join For Free

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

Interface (computing) Java (programming language) Task (computing) Form (document)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Which JVM Version Is the Fastest?
  • 10 Books Every Senior Engineer Should Read
  • Waterfall Vs. Agile Methodologies: Which Is Best For Project Management?
  • How To Integrate Third-Party Login Systems in Your Web App Using OAuth 2.0

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo