Maven execute a goal on build fail / FindBugs

2020-07-23 04:33发布

问题:

I have integrated FindBugs plugin to fail the build in case of bugs.
Then using that brilliant answer I configured FindBugs to generate html reports (xml version is barely readable).
The problem is that I have failOnError property set to true, which means that the build would fail in case of bug.

 .....
 <configuration>
        .....
        <failOnError>true</failOnError>
 </configuration>

And then no html report would be generated.

I read about Maven build lifecycle and there is no such thing as "Execute on fail" (like finally block in Java). So, are there any possible workarounds? And shouldn't it be out-of the box Maven feature?

回答1:

Special thanks to @SpaceTrucker for workaround suggestion. Here is the configuration I ended up with:

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>findbugs-maven-plugin</artifactId>
   <version>3.0.4</version>
   <configuration>
       <effort>Max</effort>
       <threshold>Low</threshold>
       <findbugsXmlOutputDirectory>${project.build.directory}/findbugs</findbugsXmlOutputDirectory>
   </configuration>
   <executions>
       <execution>
           <id>noFailOnError</id>
           <phase>verify</phase>
           <goals>
               <goal>check</goal>
           </goals>
           <configuration>
               <failOnError>false</failOnError>
           </configuration>
       </execution>
       <execution>
           <id>failOnError</id>
           <phase>install</phase>
           <goals>
               <goal>check</goal>
           </goals>
           <configuration>
               <failOnError>true</failOnError>
           </configuration>
       </execution>
   </executions>
</plugin> 

The solution is to use different configurations in verify and install phases. Note, that according to that answer transformation (to html) is executed in verifyphase.

Issue was submitted for html report generation.

Results are also can be seen by simply run mvn findbugs:gui