强制maven无法修复非空的违规行为 [英] force maven to fail the build on nonnull violations
问题描述
下面有一个简单的代码用于测试带有maven的findbugs NonNull注释。
I have the simple code below for testing the findbugs NonNull annotation with maven.
我执行mvn clean install site,
我得到一个目录target / site / css和target / site / images,
但仅此而已。
我原本希望得到一份报告,说println(null)违反了NonNull条件。
I execute "mvn clean install site", and I get a directory target/site/css and target/site/images, but nothing more. I was expecting to get a report, saying that println(null) violates the NonNull condition.
我需要做什么才能获得该报告?
What do I need to do to get that report?
另外,如果存在NonNull违规,
是否有办法防止mvn clean install成功?
Also, is there a way to prevent "mvn clean install" to succeed if there are NonNull violations?
注意:我知道我可以通过Sonar获得此类报告;
但是,如果出现此类错误,我希望mvn clean install失败,
之后无需使用可选的声纳工具。
Note: I am aware that I can get such report with Sonar; However, I would like "mvn clean install" to fail if there are such errors, without the need to use an optional Sonar tool afterwards.
src / main / java / test / Hello.java
src/main/java/test/Hello.java
package test;
import edu.umd.cs.findbugs.annotations.NonNull;
public class Hello {
static public void print(@NonNull Object value) {
System.out.println("value: " + value.toString());
}
static public void main(String[] args) {
if (args.length > 0) {
print(args[0]);
} else {
print(null);
}
}
}
和pom.xml文件:
and the pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hello</groupId>
<artifactId>hello</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>net.sourceforge.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>net.sourceforge.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.5.2</version>
</plugin>
</plugins>
</reporting>
</project>
---
更新,解决方案
解决方案,基于Augusto的答案:
将此项添加到项目下的pom.xml文件中:
solution, based on the answer from Augusto: Add this to the pom.xml file, under project:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<includeTests>true</includeTests>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
<execution>
<id>findbugs-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
如果存在NonNull违规,mvn clean install将失败。
With this, "mvn clean install" will fail if there is a NonNull violation.
报告对我不起作用,因为我使用maven 3,
并且报告功能在maven 3中已经改变(现在它使用普通的maven插件)
The reporting was not working for me, because I am using maven 3, and the reporting feature has changed in maven 3 (now it uses a normal maven plugin)
推荐答案
大卫,
你的问题的答案在 findbugs maven插件文档(参见findbugs:check)
The answer to your question is in the findbugs maven plugin documentation (see findbugs:check)
这篇关于强制maven无法修复非空的违规行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!