如何在使用Maven Invoker时获取构建状态?

huangapple go评论98阅读模式
英文:

How to get build status when using the maven invoker?

问题

我正在开发一个插件,用于验证Maven项目是否编译通过。我正在使用Maven Invoker来在每个项目上运行“install”目标,但我没有找到如何获取构建结果的方法。以下是我尝试使用的代码示例:

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 {
        invoker.execute(request);
    } catch (Exception e) {
        failedToCompileList.add(file.getAbsolutePath());
        getLog().error(e);
    }
}
英文:

I'm developing a plugin that verifies if a maven project compiles or not, I'm using the maven invoker to run install goal on each project but I didn't find how to get the build result from, here's an example of the code I'm trying to use:

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 {
		invoker.execute(request);
	} catch (Exception e) {
		failedToCompileList.add(file.getAbsolutePath());
		getLog().error(e);
	}
}

答案1

得分: 0

使用页面中,您只需检查execute语句的结果:

InvocationResult result = invoker.execute(request);

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

这将从调用结果中获取退出代码,并在其不为0(传统的全清代码)时抛出异常。请注意,我们可以通过将InvocationOutputHandler实例添加到invoker或request中来捕获构建输出。

将这部分添加到您的示例中:

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);
    }
}
英文:

From the Usage page, you just need to check the results of the execute statement:

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

> This will retrieve the exit code from the invocation result, and throw an exception if it's not 0 (the traditional all-clear code). Note that we could capture the build output by adding an InvocationOutputHandler instance to either the invoker or the request.

Adding this to your example would be:

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);
    }
}

huangapple
  • 本文由 发表于 2020年10月21日 23:06:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64466505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定