Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fail a Maven build from inside Java logic?

I am developing a project in which I would like to use Maven to execute a Java Main class as part of the build process. The Main class's job is to validate that some given (currently hardcoded) files are valid RAML files. In a perfect world, I would like the build to be able to fail the build from within this class's logic, if they are invalid, and have that failure report back to the terminal with the "[BUILD FAILURE]" message just like maven does natively.

I added tho org.codehaus.mojo.exec-maven-plugin plugin to my POM, which is allowing me to execute the Java Main class I mentioned earlier. That section of the POM looks like this:

<plugin>  
   <groupId>org.codehaus.mojo</groupId>  
   <artifactId>exec-maven-plugin</artifactId>  
   <version>1.4.0</version>  
   <executions>  
    <execution>  
     <phase>test</phase>  
     <goals>  
      <goal>java</goal>  
     </goals>  
     <configuration>
      <mainClass>com.example.ubunfu.MainClass</mainClass>  
      <arguments>  
       <argument></argument>
      </arguments>
      <cleanupDaemonThreads>false</cleanupDaemonThreads> <!-- Prevents thread hanging -->
     </configuration>  
    </execution>  
   </executions>  
  </plugin>

However, I'm not sure how to signal from the logic inside of MainClass that the build should fail - if that's necessary. I read about the org.apache.maven.plugins.MojoFailureException in the Maven - Guide to Developing Java Plugins. There it seems to say that I can throw one of these and it will fail the build like I'm hoping for.

I'm not sure what I need to do in order to have the MojoFailureException class available to me inside MainClass. I haven't been able to find a JAR or anything, but if I understand how Maven works, this should be fixed with some kind of addition to the POM, not a JAR.

If you have any insight for me on this, I'd really appreciate it.

-Ryan

like image 784
Ubunfu Avatar asked Sep 13 '26 15:09

Ubunfu


1 Answers

It appears that throwing any exception will cause the build to fail. See this answer.

To get access to MojoFailureException, add the following dependency:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-plugin-api</artifactId>
    <version>3.3.3</version>
</dependency>
like image 124
James Marble Avatar answered Sep 15 '26 04:09

James Marble



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!