2013-08-22 39 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

相關問題