How can I fail Maven build if exec:java goal fails?

孤人 提交于 2019-12-06 21:38:00

问题


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.

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.


回答1:


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>

And the following Java class:

package com.example;

public class Main {
    public static void main(String[] args) {
        throw new RuntimeException("A problem occured");
    }
}

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] ------------------------------------------------------------------------

The integration phase is never executed, because of the build error.

So the question is, how are you handling errors in the Java class that loads your db? Is throwing an exception an option?




回答2:


This isn't a feature it has by default, but you might want to request it at http://jira.codehaus.org/browse/MEXEC, as it would be a simple addition.

If you want exec:java to fail the build, the main call will need to throw an exception instead of returning a non-zero exit code.

If that's not an option, you can still use exec:exec - see http://mojo.codehaus.org/exec-maven-plugin/examples/example-exec-for-java-programs.html for a description on how to add the project dependencies to the classpath.

A further option if neither of these suit for some reason is to use the AntRun plugin with the <java .../> task. Project dependencies can also be passed into that.




回答3:


For java goal System.exit(n) where n is non-zero will not working. Need to throw java exception.




回答4:


In addition to throwing an exception, you can also use System.exit(n), where n is non-zero to cause the maven build to fail.



来源:https://stackoverflow.com/questions/2363055/how-can-i-fail-maven-build-if-execjava-goal-fails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!