如何从Eclipse插件运行ant,将输出发送到Eclipse控制台,以及捕获构建结果(成功/失败)?

Mar*_*ica 4 eclipse ant console

从Eclipse插件中,我想运行一个Ant构建脚本。我还想通过在Eclipse控制台中显示它来向用户显示Ant输出。最后,我还想等待Ant构建完成,并捕获结果:构建成功还是失败?

我发现了三种从Eclipse运行Ant脚本的方法:

  • 实例化一个org.eclipse.ant.core.AntRunner,调用一些设置器,然后调用run()run(IProgressMonitor)。结果要么是正常终止(表示成功),要么是CoreException IStatus包含BuildException(表示失败),否则是其他问题。但是,我在任何地方都看不到Ant输出。
  • 实例化org.eclipse.ant.core.AntRunnerand调用run(Object),并传递一个String[]包含命令行参数的。结果是正常终止(指示成功)或InvocationTargetException(指示失败),否则其他地方出了问题。看来,Ant输出被发送到Eclipse的stdout了。在Eclipse本身中不可见。
  • 调用DebugPlugin.getDefault().getLaunchManager(),然后调用getLaunchConfigurationType(IAntLaunchConfigurationConstants.ID_ANT_BUILDER_LAUNCH_CONFIGURATION_TYPE),然后在set属性上将"org.eclipse.ui.externaltools.ATTR_LOCATION"构建文件名(属性设置DebugPlugin.ATTR_CAPTURE_OUTPUT为true),最后调用launch()。Ant输出显示在Eclipse控制台中,但是我不知道如何在我的代码中捕获构建结果(成功/失败)。或如何等待发射终止。

有什么办法可以同时获得控制台输出捕获结果?

hap*_*rry 5

编辑 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) {
    }
});
Run Code Online (Sandbox Code Playgroud)