0

I have a problem with logs from ant command. When I run command in CMD i see step by step progress of deployment or validation but when I execute the same target in my java app I do not see mentioned logs - I see final logs when deployment is finished.

Code in button:

  if (orgSelectName.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(null, "Choose org !");
            buttonGroup2.clearSelection();
        } else {
            String selectedOrg = orgSelectName.getSelectedItem().toString();
            String pathDesktopBuildOrg = System.getProperty("user.home") + "/Desktop/Deployment/" + selectedOrg + "/build.xml";
            MyLogger myLogger = new MyLogger();

            File buildFile = new File(pathDesktopBuildOrg);
            Project antProject = new Project();
            myLogger.setErrorPrintStream(System.err);
            myLogger.setOutputPrintStream(System.out);
            myLogger.setMessageOutputLevel(Project.MSG_INFO);
            antProject.addBuildListener(myLogger);

                antProject.setUserProperty("ant.file", buildFile.getAbsolutePath());
                antProject.fireBuildStarted();
                antProject.init();
                ProjectHelper helper = ProjectHelper.getProjectHelper();
                antProject.addReference("ant.projectHelper", helper);
                helper.parse(antProject, buildFile);
                String target = "deployCodeCheckOnly";
                buttonGroup2.clearSelection();
                antProject.executeTarget(target);
                antProject.fireBuildFinished(null);


        }

What should I do to get these logs and see step by step progress in my java app?

When I run in CMD I see this and I would like to have the same result in java app.

DeployLogs

I've added additional class class MyLogger extends DefaultLogger {

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

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

    @Override
    public void messageLogged(BuildEvent be) {
        actionResult.append(be.getMessage() + "\n");
    }
2
  • Could you share the relevant Java code? That would help us help you better. – sfdcfox Sep 12 '19 at 19:16
  • @sfdcfox Code from button has been added to description above. – Freeezer789 Sep 12 '19 at 20:14
0

To monitor the status of the build, use a BuildListener. The class would look like:

import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.BuildEvent;

public class MyBuildListener implements BuildListener {
  Object controller; // Replace Object with the correct type.
  public MyBuildListener(Object controller) {
    this.controller = controller;
  }
  public void buildFinished(BuildEvent event) {}
  public void buildStarted(BuildEvent event) {}
  public void messageLogged(BuildEvent event) {
    // handle this event //
  }
  public void targetFinished(BuildEvent event) {}
  public void targetStarted(BuildEvent event) {}
  public void targetFinished(BuildEvent event) {}
  public void taskStarted(BuildEvent event) {}
}

Then, in your main logic:

this.buildListener = new MyBuildListener(this);
antProject.addBuildListener(this.buildListener);

You could also use an anonymous inner class, etc, depending on your Java version.

3
  • Thank You for response but how I should implement this ? I've created this class and had to change it to abstract class, then when I would like to add the "main logic to my ActionListener because I use it for validate button I have errors like: cannot find symbol (variable buildListener). And I should add something into these methods buildFinished, Started etc ? – Freeezer789 Sep 14 '19 at 10:48
  • @Tomasz The BuildListener should be its own class, and shouldn't be abstract, as far as I can tell (because then you can't instantiate it). The buildListener variable should be in the class that calls the addBuildListener method. You can choose to handle the additional methods (buildFinished, buildStarted, etc) if you need to handle them; this answer only addresses monitoring the message/progress of the build. – sfdcfox Sep 14 '19 at 12:37
  • I've added new piece of code. I created a new class with methods buildStarted etc but when I click "validation" button I still do not see the logs in real time - I see only logs after finish. – Freeezer789 Sep 14 '19 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.