Java Eclipse插件:如何检测Ant构建的取消和失败



好吧,我有一个非常丑陋的代码,启动ant构建:

private boolean runAntBuild(String antFile, final IProgressMonitor monitor)
            throws CoreException, Exception
    {
        ILaunchConfigurationType configType = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(IAntLaunchConstants.ID_ANT_LAUNCH_CONFIGURATION_TYPE);
        ILaunchConfigurationWorkingCopy wc = configType.newInstance(null, "Ant build");
        ...
        monitor.beginTask("Running ant build", 100);
        ILaunch launch = wc.launch(ILaunchManager.RUN_MODE, monitor);

        final boolean[] buildWasSuccessful = new boolean[] { true };
        // Listen to the text output of the process to see if it ended in
        // success, cancellation or failure:
        for (IProcess proc : launch.getProcesses())
        {
            proc.getStreamsProxy().getErrorStreamMonitor().addListener(new IStreamListener() {
                @Override
                public void streamAppended(String text, IStreamMonitor monitor1)
                {
                    if (text.contains("BUILD FAILED"))
                    {
                        buildWasSuccessful[0] = false;
                    }
                }
            });
            proc.getStreamsProxy().getOutputStreamMonitor().addListener(new IStreamListener() {
                @Override
                public void streamAppended(String text, IStreamMonitor monitor1)
                {
                    if (text.toLowerCase().contains("build cancelled"))
                        monitor.setCanceled(true);
                }
            });
        }
        // Wait for build to finish:
        boolean buildIsStillRunning = true;
        while (buildIsStillRunning)
        {
            buildIsStillRunning = false;
            for (IProcess proc : launch.getProcesses())
            {
                if (!proc.isTerminated())
                {
                    buildIsStillRunning = true;
                    try
                    {
                        Thread.sleep(100);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
        if (monitor.isCanceled())
        {
            throw new Exception("Request was cancelled.");
        }
        if (monitor != null)
            monitor.done();
        return buildWasSuccessful[0];
    }

有没有更好的方法来检测构建的失败或取消?

不能使用"proc"系统返回值吗?约定为0表示成功,任何其他值都是错误。

相关内容

  • 没有找到相关文章

最新更新