[英]Java Eclipse plugin: How to detect cancellation and failure of Ant build
Well, I have this really ugly peace of code that launch ant build: 好吧,我拥有启动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];
}
Is there a better way to detect failure or cancellation of the build? 有没有更好的方法来检测构建失败或取消?
Can't you use "proc" system return value ? 您不能使用“ proc”系统返回值吗? The convention is 0 is success and any other value is an error.
约定为0表示成功,其他任何值均为错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.