2013-08-22 38 views
0

好失败,我的代码,这实在是太丑和平是推出Ant构建:Java的Eclipse插件:如何检测取消和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]; 
    } 

是否有更好的方法来检测的失败或取消建立?

回答

0

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

+0

不,值即使失败也是0。 – sara

相关问题