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

标签 eclipse ant console

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

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

  • 实例化一个 org.eclipse.ant.core.AntRunner,调用一些 setter 并调用 run()run(IProgressMonitor)。结果要么是正常终止(表示成功),要么是带有包含 BuildExceptionIStatus 的 CoreException(表示失败),或者是其他错误。但是,我在任何地方都看不到 Ant 输出。
  • 实例化 org.eclipse.ant.core.AntRunner 并调用 run(Object),传递包含命令的 String[]行参数。结果要么是正常终止(指示成功),要么是 InitationTargetException(指示失败),或者是其他错误。 Ant 输出似乎被发送到 Eclipse 的 stdout;它在 Eclipse 本身中不可见。
  • 调用 DebugPlugin.getDefault().getLaunchManager(),然后调用 getLaunchConfigurationType(IAntLaunchConfigurationConstants.ID_ANT_BUILDER_LAUNCH_CONFIGURATION_TYPE),然后调用该设置属性 "org. eclipse.ui.externaltools.ATTR_LOCATION" 添加到构建文件名(并将属性 DebugPlugin.ATTR_CAPTURE_OUTPUT 设置为 true),最后调用 launch()。 Ant 输出显示在 Eclipse 控制台中,但我不知道如何在代码中捕获构建结果(成功/失败)。或者甚至如何等待启动终止。

有没有办法让控制台输出都捕获结果?

最佳答案

编辑 05/16/2016 @Lii 提醒我,ILaunchConfigurationWorkingCopy#launch 调用与 IStreamListener 之间的任何输出附加的将会丢失。他对这个答案做出了贡献here .

原始答案 我意识到这是一篇旧文章,但我能够在我的一个插件中完全按照您的要求进行操作。如果此时它对您没有帮助,也许对其他人会有帮助。我最初是在 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) {
    }
});

关于eclipse - 如何从 Eclipse 插件运行 ant,将输出发送到 Eclipse 控制台,并捕获构建结果(成功/失败)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2364247/

相关文章:

c++ - ANT 不会终止 openmp 可执行文件 (C++)

java - 分发可执行 Jar 控制台应用程序

eclipse - Azure SF 教程项目使用 gradle 5.4 构建,但不使用 Eclipse 2019-06

eclipse - persistence.xml在hibernate项目中的优先级

java - 在 Mac 中通过 Eclipse 在 Google 应用程序引擎中创建新的 Web 应用程序项目

git 忽略目录 "build"及其所有内容,但不包括 "build.xml"

android - 在 Eclipse 中使用 gson 与 Android 的问题

java - 在centos for java上安装opencv

c++ - 如何使用 gotoxy 函数代替 clrscr

c# - 读取键盘输入而不总是让我的控制台应用程序聚焦?