How to run ant from the Eclipse plugin, send output to the Eclipse console and capture the build result (success / failure)?

Inside the Eclipse plugin, I want to run Ant build script. I also want to display Ant output for the user by displaying it in the Eclipse console. Finally, I also want to wait for the Ant build to complete and capture the result: has the build completed or failed?

I found three ways to run Ant script from eclipse:

  • Create an instance org.eclipse.ant.core.AntRunner, call some setters and call run()or run(IProgressMonitor). The result is either a normal termination (indicating success) or a CoreException with IStatus, containing BuildException(indicating failure), otherwise something went wrong. However, I do not see Ant output anywhere.
  • Create an instance org.eclipse.ant.core.AntRunnerand call run(Object), passing String[], containing command line arguments. The result is either a normal completion (success of the testimony), or InvocationTargetException(indicating failure), or something else went wrong. It seems that Ant output has been sent to Eclipse stdout; it does not appear in Eclipse itself.
  • Call DebugPlugin.getDefault().getLaunchManager(), then in this call getLaunchConfigurationType(IAntLaunchConfigurationConstants.ID_ANT_BUILDER_LAUNCH_CONFIGURATION_TYPE), then set this attribute "org.eclipse.ui.externaltools.ATTR_LOCATION"to the name of the assembly file (and the attribute DebugPlugin.ATTR_CAPTURE_OUTPUTto true) and finally type launch(). The Ant output is displayed in the Eclipse console, but I don’t know how to write the build result (success / failure) in my code. Or how to wait for the launch to finish, even.

Is there a way to get both console output and get the result?

+3
4

05/16/2016 @Lii , ILaunchConfigurationWorkingCopy#launch IStreamListener . .

, , , . , , - . 3.2, 3.6 API...

// show the console
final IWorkbenchPage activePage = PlatformUI.getWorkbench()
        .getActiveWorkbenchWindow()
        .getActivePage();
activePage.showView(IConsoleConstants.ID_CONSOLE_VIEW);

// let launch manager handle ant script so output is directed to Console view
final ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType type = manager.getLaunchConfigurationType(IAntLaunchConstants.ID_ANT_LAUNCH_CONFIGURATION_TYPE);
final ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(null, [*** GIVE YOUR LAUNCHER A NAME ***]);
workingCopy.setAttribute(ILaunchManager.ATTR_PRIVATE, true);
workingCopy.setAttribute(IExternalToolConstants.ATTR_LOCATION, [*** PATH TO ANT SCRIPT HERE ***]);
final ILaunch launch = workingCopy.launch(ILaunchManager.RUN_MODE, null);
// make sure the build doesnt fail
final boolean[] buildSucceeded = new boolean[] { true };
((AntProcess) launch.getProcesses()[0]).getStreamsProxy()
        .getErrorStreamMonitor()
        .addListener(new IStreamListener() {
            @Override
            public void streamAppended(String text, IStreamMonitor monitor) {
                if (text.indexOf("BUILD FAILED") > -1) {
                    buildSucceeded[0] = false;
                }
            }
        });
// wait for the launch (ant build) to complete
manager.addLaunchListener(new ILaunchesListener2() {
    public void launchesTerminated(ILaunch[] launches) {
        boolean patchSuccess = false;
        try {
            if (!buildSucceeded[0]) {
                throw new Exception("Build FAILED!");
            }
            for (int i = 0; i < launches.length; i++) {
                if (launches[i].equals(launch)
                        && buildSucceeded[0]
                        && !((IProgressMonitor) launches[i].getProcesses()[0]).isCanceled()) {
                    [*** DO YOUR THING... ***]
                    break;
                }
            }
        } catch (Exception e) {
            [*** DO YOUR THING... ***]
        } finally {
            // get rid of this listener
            manager.removeLaunchListener(this);
            [*** DO YOUR THING... ***]
        }
    }

    public void launchesAdded(ILaunch[] launches) {
    }

    public void launchesChanged(ILaunch[] launches) {
    }

    public void launchesRemoved(ILaunch[] launches) {
    }
});
+4

harry.

. streamAppended , .

. , bug. , Happy Harry Happy . ILaunchListener.launchChanged, 4/5 .

, IStreamMonitor.getContents , .

, . ProcessConsole.

/**
 * Adds listener to monitor, and calls listener with any content monitor already has.
 * NOTE: This methods synchronises on monitor while listener is called. Listener may
 * not wait on any thread that waits for monitors monitor, what would result in dead-lock.
 */
public static void addAndNotifyStreamListener(IStreamMonitor monitor, IStreamListener listener) {
    // Synchronise on monitor to prevent writes to stream while we are adding listener.
    // It weird to synchronise on monitor because that a shared object, but that 
    // what ProcessConsole does.  
    synchronized (monitor) {
        String contents = monitor.getContents();
        if (!contents.isEmpty()) {
            // Call to unknown code while synchronising on monitor. This is dead-lock prone!
            // Listener must not wait for other threads that are waiting in line to 
            // synchronise on monitor.
            listener.streamAppended(contents, monitor);
        }
        monitor.addListener(listener);
    }
}

PS: ProcessConsole.java . ProcessConsole.StreamListener? ProcessConsole.StreamListener , , .

+1

Source: https://habr.com/ru/post/1735064/


All Articles