如何在安装时在maven中自动运行findbugs

use*_*928 17 java maven-2 findbugs

findbugs插件添加到maven 很容易,这样就可以运行了

mvn site
Run Code Online (Sandbox Code Playgroud)

但是,我希望它能随时运行

mvn install
Run Code Online (Sandbox Code Playgroud)

就像单元测试一样.也就是说,如果findbugs发现任何错误,我不希望安装成功.我有办法做到这一点吗?

Pas*_*ent 31

关于findbugs:检查目标,文档写道:

如果源代码中存在任何FindBugs违规,则构建失败.默认情况下,XML报告会在错误的目标目录中输出.要查看有关FindBugs选项的更多文档,请参阅FindBugs手册.

所以这正是您正在寻找的目标.您现在只需将检查目标绑定到安装 验证阶段(验证阶段就在安装之前发生,实际上是为了运行任何检查来验证包是否有效并符合质量标准,所以我认为这是一个更好的选择):

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>findbugs-maven-plugin</artifactId>
        <version>2.0.1</version>
        <configuration>
          <effort>Max</effort>
          <threshold>Low</threshold>
          <xmlOutput>true</xmlOutput>
        </configuration>
        <executions>
          <execution>
            <phase>verify</phase> 
            <goals>
              <goal>check</goal> 
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>
Run Code Online (Sandbox Code Playgroud)

当然,调整配置以满足您的需求.