DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. 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 · Interview
Like (0)
Save
Tweet
Share
6.89K 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

  • What Are the Benefits of Java Module With Example
  • How Chat GPT-3 Changed the Life of Young DevOps Engineers
  • Keep Your Application Secrets Secret
  • Best Navicat Alternative for Windows

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: