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

99封情书 提交于 2019-12-05 02:27:09

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?

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.

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

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.

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