2009-08-11 26 views
7

有沒有人知道如何配置maven findbugs插件來將錯誤彙總輸出到控制檯(類似於pmd插件)?Maven findbugs:check - 輸出小結的摘要

目前findbugs:檢查只是打印出總共有多少錯誤,我需要檢查單個模塊target/findbugs目錄和每個findbugs.xml文件來解決問題。

<plugin> 
<groupId>org.codehaus.mojo</groupId> 
<artifactId>findbugs-maven-plugin</artifactId> 
<version>2.0.1</version>        
<configuration> 
     <xmlOutput>true</xmlOutput> 
     <xmlOutputDirectory>findbugsreports</xmlOutputDirectory> 
     <findbugsXmlOutput>true</findbugsXmlOutput> 
     <findbugsXmlOutputDirectory>target/site/findbugsreports</findbugsXmlOutputDirectory> 
     <debug>true</debug> 
</configuration> 
</plugin> 

理想情況下,最好在命令行上重新獲得彙總報告。有任何想法嗎?

+0

我實際上不明白爲什麼這個功能沒有在插件中第一次實現......奇怪。 – yegor256 2010-10-29 14:14:43

+0

提交了一張票:https://sourceforge.net/tracker/?func=detail&aid=3111339&group_id=61626&atid=497856 – yegor256 2010-11-18 07:40:51

回答

3

目前還沒有辦法使用標準插件來做到這一點。你可以創建一個插件來閱讀findbugsChecks.xml並輸出你需要的信息。

下面的代碼將輸出在find目錄中找到的findbugsChecks.xml文件中發現的所有bug和每個包的bug。你可以配置它讀取由設置在配置findBugsChecks屬性的文件的名稱:

package name.seller.rich; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

import org.apache.maven.plugin.AbstractMojo; 
import org.apache.maven.plugin.MojoExecutionException; 
import org.apache.maven.plugin.MojoFailureException; 
import org.apache.maven.project.MavenProject; 
import org.codehaus.plexus.util.xml.Xpp3Dom; 
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; 
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 

/** 
* @goal stats 
*/ 
public class FindbugsStatsMojo extends AbstractMojo { 

    /** 
    * Where to read the findbugs stats from 
    * 
    * @parameter expression="${findbugsChecks}" 
    *   default-value="${project.build.directory}/findbugsCheck.xml" 
    */ 
    private File findbugsChecks; 

    /** 
    * Output the Findbus stats for the project to the console. 
    */ 
    public void execute() throws MojoExecutionException, MojoFailureException { 
     if (findbugsChecks != null && findbugsChecks.exists()) { 
      try { 
       Xpp3Dom dom = Xpp3DomBuilder.build(new FileReader(
         findbugsChecks)); 

       // get the summary and output it 
       Xpp3Dom summaryDom = dom.getChild("FindBugsSummary"); 

       // output any information needed 
       getLog().info(
         "Total bug count:" 
           + summaryDom.getAttribute("total_bugs")); 

       Xpp3Dom[] packageDoms = summaryDom.getChildren("PackageStats"); 

       getLog().info(packageDoms.length + " package(s)"); 
       for (int i = 0; i < packageDoms.length; i++) { 
        String info = new StringBuilder().append("package ") 
          .append(packageDoms[i].getAttribute("package")) 
          .append(": types:").append(
            packageDoms[i].getAttribute("total_types")) 
          .append(", bugs:").append(
            packageDoms[i].getAttribute("total_bugs")) 
          .toString(); 
        getLog().info(info); 
       } 
      } catch (FileNotFoundException e) { 
       throw new MojoExecutionException(
         "Findbugs checks file missing", e); 
      } catch (XmlPullParserException e) { 
       throw new MojoExecutionException(
         "Unable to parse Findbugs checks file", e); 
      } catch (IOException e) { 
       throw new MojoExecutionException(
         "Unable to read Findbugs checks file", e); 
      } 
     } 
    } 
} 

要打包此代碼時,它將像這樣的POM添加到src /主/ Java中的Mavenproject的文件夾:

<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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>name.seller.rich</groupId> 
    <artifactId>maven-findbugs-stats-plugin</artifactId> 
    <packaging>maven-plugin</packaging> 
    <version>0.0.1</version> 
    <dependencies> 
    <dependency> 
     <groupId>org.apache.maven</groupId> 
     <artifactId>maven-core</artifactId> 
     <version>2.2.0</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.maven</groupId> 
     <artifactId>maven-plugin-api</artifactId> 
     <version>2.2.0</version> 
    </dependency> 
    </dependencies> 
</project> 

然後運行MVN安裝安裝插件。

要實際使用它,您可以在命令行上將其作爲附加目標運行,或將其綁定到您的項目以作爲標準生命週期的一部分運行。

下面是從命令行運行的命令(假設該項目已預先編譯:

mvn findbugs:check name.seller.rich:maven-findbugs-stats-plugin:0.0.1:stats 

要綁定配置到您的項目,所以它會在每個版本中運行,使用以下配置:

<build> 
    <plugins> 
    <plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>findbugs-maven-plugin</artifactId> 
    <version>2.1</version> 
    <executions> 
    <execution> 
     <id>check</id> 
     <phase>package</phase> 
     <goals> 
     <goal>check</goal> 
     </goals> 
    </execution> 
    </executions>        
    <configuration> 
    <xmlOutput>true</xmlOutput> 
    <xmlOutputDirectory>findbugsreports</xmlOutputDirectory> 
    <findbugsXmlOutput>true</findbugsXmlOutput> 
    <findbugsXmlOutputDirectory>${findbugsOutputDirectory}</findbugsXmlOutputDirectory> 
    <debug>true</debug> 
    <failOnError>false</failOnError> 
    </configuration> 
    </plugin> 
    <plugin> 
    <groupId>name.seller.rich</groupId> 
    <artifactId>maven-findbugs-stats-plugin</artifactId> 
    <executions> 
     <execution> 
     <id>stats</id> 
     <phase>package</phase> 
     <goals> 
      <goal>stats</goal> 
     </goals> 
     </execution> 
    </executions> 
    </plugin> 
    </plugins> 
</build> 
+0

愚蠢的問題:爲什麼在你的'findbugs-maven-plugin'聲明中定義執行? – Snekse 2013-05-21 23:41:59

3

以上我所提出的行家FindBugs的問題跟蹤這個問題沿着從概念之後,http://jira.codehaus.org/browse/MFINDBUGS-118。我也編碼並提交了一個補丁,顯示總錯誤對每一個項目,它可以很容易地修飾的T o獲得其他細節。

該代碼忽略指定爲生成POM輸出的項目,並忽略其POM在findbugs配置中指定true的項目。我們正在運行一個應用了補丁的大型多模塊maven版本。

的補丁應用於你運行mvn FindBugs的:檢查和你喜歡的東西下面的輸出(輸出混淆,以保護有罪:):

[INFO] Summary 
[INFO] ------- 
[INFO] C:\PATH\Abstraction\PalDefinitions\target/findbugsXml.xml 4 
[INFO] C:\PATH\System\target/findbugsXml.xml 19 
[INFO] C:\PATH\ApplicationLayer\target/findbugsXml.xml 13 
[INFO] C:\PATH\Support\ServiceStub\target/findbugsXml.xml 11 
[INFO] C:\PATH\Support\MultiPlatform\target/findbugsXml.xml 10 
[INFO] C:\PATH\Support\Serializer\target/findbugsXml.xml 19 
[INFO] C:\PATH\Support\Brander\target/findbugsXml.xml 19 
[INFO] C:\PATH\PlatformAbstraction\Pal1\target/findbugsXml.xml 8 
[INFO] C:\PATH\PlatformAbstraction\Pal2\target/findbugsXml.xml 0 
[INFO] C:\PATH\PlatformAbstraction\Pal3\target/findbugsXml.xml 0 
[INFO] C:\PATH\PlatformAbstraction\Pal4\target/findbugsXml.xml 0 
[INFO] C:\PATH\Framework\Common\target/findbugsXml.xml 12 
[INFO] C:\PATH\Framework\legacyFramework\target/findbugsXml.xml 7 
[INFO] C:\PATH\Framework\UIFramework\target/findbugsXml.xml 7 
[INFO] C:\PATH\ExecutionLayer\Stub\target/findbugsXml.xml 0 
[INFO] C:\PATH\ExecutionLayer\BB\BB\target/findbugsXml.xml 1 
[INFO] TOTAL = 130 
[INFO] ------- 
[INFO] Number of bugs 130 falls BELOW summaryThreshold 260. Check OK 
+0

JIRA錯誤鏈接中斷 – yegor256 2010-11-16 07:12:45

+0

只需使用版本2.5或更高版本的插件即可。 – 2014-11-07 15:08:21

4

我使用這個技巧,基於Maven的groovy-插件:

<plugin> 
    <groupId>org.codehaus.groovy.maven</groupId> 
    <artifactId>gmaven-plugin</artifactId> 
    <version>1.0-rc-5-SNAPSHOT</version> 
    <executions> 
    <execution> 
     <phase>prepare-package</phase> 
     <goals> 
     <goal>execute</goal> 
     </goals> 
     <configuration> 
     <source> 
      def file = new File("${project.build.directory}/findbugsXml.xml") 
      if (!file.exists()) { 
      fail("Findbugs XML report is absent: " + file.getPath()) 
      } 
      def xml = new XmlParser().parse(file) 
      def bugs = xml.BugInstance 
      def total = bugs.size() 
      if (total &gt; 0) { 
      log.info("Total bugs: " + total) 
      for (i in 0..total-1) { 
       def bug = bugs[i] 
       log.info(
       bug.LongMessage.text() 
       + " " + bug.Class.'@classname' 
       + " " + bug.Class.SourceLine.Message.text() 
      ) 
      } 
      } 
     </source> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 
0

您可以使用Violations Maven Plugin來做到這一點。它配置有模式以識別文件系統上的報告文件。它需要在findbugs或任何其他靜態代碼分析工具之後運行。

它將

  • 打印侵犯生成日誌。
  • 如果發現的違例數量高於配置的數字,則可以選擇性地使構建失敗。