使用Maven调用程序时如何获取构建状态?

问题描述

我正在开发一个可验证maven项目是否编译的插件,我正在使用maven调用程序在每个项目上运行let x = toscalar(Events | take 1 | project pack("ts",start_timestmp,"sof",someotherfield)); Sensor_Data | where timestmp == todatetime(x["ts"]) | where someotherfield == tostring(x["sof"]) 目标,但是我没有找到如何从中获取构建结果,这是我要使用的代码示例:

install

解决方法

Usage page中,您只需要检查execute语句的结果:

InvocationResult result = invoker.execute( request );
 
if ( result.getExitCode() != 0 )
{
    throw new IllegalStateException( "Build failed." );
}

这将从调用结果中检索退出代码,如果不为0,则抛出异常(传统的全透明代码)。请注意,我们可以通过将InvocationOutputHandler实例添加到调用者或请求中来捕获构建输出。

将其添加到您的示例中将是:

private void verify(File file) {
    Invoker invoker = new DefaultInvoker();
    InvocationRequest request = new DefaultInvocationRequest();
    request.setGoals(Collections.singletonList("install"))
    .setMavenOpts("-Dmaven.test.skip=true")
    .setBaseDirectory(file).
    setBatchMode(true);
    try {
        InvocationResult result = invoker.execute(request);
        if ( result.getExitCode() != 0 )
        {
            throw new IllegalStateException( "Build failed." );
        }
    } catch (Exception e) {
        failedToCompileList.add(file.getAbsolutePath());
        getLog().error(e);
    }
}