英文:
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论