如果exec:java目标失败,如何使Maven构建失败? [英] How can I fail Maven build if exec:java goal fails?
问题描述
我们正在使用Maven exec:java
目标来运行自定义Java应用程序,该应用程序配置数据库以供我们的集成测试使用.我们希望在exec:exec
上使用exec:java
,以便能够在要使用的主类的类路径中使用项目依赖项.
几次由于有效原因应用程序失败,但是Maven构建继续进行,好像没有发生任何错误.
We're using the Maven exec:java
goal to run a custom java application that configures a database for use with our integration tests. We want to use exec:java
over exec:exec
to be able to use the project dependencies in the classpath of the main class to be used.
A few times the application has failed for valid reasons, but the Maven build continued as if nothing had gone wrong.
是否可以与exec:java
一起使用的任何"failonerror"类型参数?恐怕要将system.exit()
代码添加到正在运行的类中,因为我怀疑它会在Maven VM中运行,因此不仅会杀死自身,还会杀死Maven本身.
Is there any "failonerror" type argument that can be used with exec:java
? I'm afraid to add system.exit()
codes to the class being run, as I suspect it will kill not only itself, but also Maven itself, due to the fact it's running in the Maven VM.
推荐答案
我只是对POM中声明的以下插件配置进行了简单测试:
I just did a simple test with the following plugin configuration declared in a POM:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>my-exec-java</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
...
</plugins>
...
</build>
</project>
以及以下Java类:
package com.example;
public class Main {
public static void main(String[] args) {
throw new RuntimeException("A problem occured");
}
}
这是我在调用integration-test
阶段时得到的:
And this is what I get when invoking the integration-test
phase:
$ mvn clean integration-test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building q2363055
[INFO] task-segment: [clean, integration-test]
[INFO] ------------------------------------------------------------------------
...
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: /home/pascal/Projects/stackoverflow/q2363055/target/q2363055-1.0-SNAPSHOT.jar
[INFO] Preparing exec:java
[WARNING] Removing: java from forked lifecycle, to prevent recursive invocation.
[INFO] No goals needed for project - skipping
[INFO] [exec:java {execution: my-exec-java}]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. null
A problem occured
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19 seconds
[INFO] Finished at: Tue Mar 02 23:40:32 CET 2010
[INFO] Final Memory: 16M/79M
[INFO] ------------------------------------------------------------------------
由于构建错误,永远不会执行integration
阶段.
The integration
phase is never executed, because of the build error.
问题是,如何处理加载数据库的Java类中的错误?抛出异常是一种选择吗?
So the question is, how are you handling errors in the Java class that loads your db? Is throwing an exception an option?
这篇关于如果exec:java目标失败,如何使Maven构建失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!