2009-08-11 4 views
7

Est-ce que quelqu'un sait comment configurer le plugin maven findbugs pour afficher un résumé des bogues sur la console (similaire au plugin pmd)? Actuellement, findbugs: check ne fait qu'imprimer le nombre total de bogues et j'ai besoin de vérifier le répertoire target/findbugs des modules individuels et chaque fichier findbugs.xml pour corriger les problèmes.Maven findbugs: check - Résumé des bogues de sortie

<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> 

Idéalement, il serait bon d'obtenir un rapport de synthèse sur la ligne de commande. Des idées?

+0

Je n'arrive pas à comprendre pourquoi cette fonctionnalité n'a pas été implémentée en premier dans le plugin ... Bizarre. – yegor256

+0

Soumis un ticket: https://sourceforge.net/tracker/?func=detail&aid=3111339&group_id=61626&atid=497856 – yegor256

Répondre

3

Il n'y a actuellement aucun moyen de le faire en utilisant le plugin standard. Vous pouvez créer un plugin pour lire le findbugsChecks.xml et afficher les informations dont vous avez besoin.

Le code ci-dessous affichera le nombre total de bogues trouvés et les bogues par paquet pour tout projet avec un findbugsChecks.xml dans le répertoire de sortie. Vous pouvez configurer le nom du fichier qu'il lit en définissant la propriété findBugsChecks sur la configuration:

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); 
      } 
     } 
    } 
} 

Pour le paquet de ce code, l'ajouter à la src/main/dossier java d'un MavenProject avec un POM comme ceci:

<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> 

Lancez ensuite mvn install pour installer le plug-in.

Pour l'utiliser réellement, vous pouvez l'exécuter en tant qu'objectif supplémentaire sur la ligne de commande ou le lier à votre projet pour l'exécuter dans le cadre du cycle de vie standard.

est ici la commande à exécuter à partir de la ligne de commande (en supposant que le projet a déjà été compilé:

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

Pour lier les configurations à votre projet il sera exécuté sur chaque version, utilisez la configuration suivante:

<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

Question muette: Pourquoi définissez-vous les exécutions dans votre déclaration 'findbugs-maven-plugin'? – Snekse

3

Après le long des concepts ci-dessus, je l'ai soulevé cette question sur le Maven findbugs question suivi. http://jira.codehaus.org/browse/MFINDBUGS-118. J'ai aussi codé et soumis un patch qui montre bugs au total pour chaque projet. Il pourrait facilement être modifié t o obtenir d'autres détails.

Le code ignore les projets spécifiés comme produisant des sorties POM et ignore également les projets dont les POM spécifient true dans leur configuration findbugs. Nous exécutons un grand build maven multi-module avec le patch appliqué.

Avec le patch appliqué vous exécutez mvn findbugs: vérifier et vous obtenez quelque chose comme la sortie suivante (sortie obscurcie pour protéger les coupables :):

[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

Le lien vers le bug JIRA est cassé – yegor256

+0

Utilisez simplement la version 2.5 ou supérieure du plugin. –

4

J'utilise ce hack, basé sur maven-groovy- plugin:

<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

Vous pouvez le faire avec Violations Maven Plugin. Il est configuré avec des modèles pour identifier les fichiers de rapport sur le système de fichiers. Il doit fonctionner après findbugs, ou tout autre outil d'analyse de code statique.

Il sera

  • Imprimer les violations dans le journal de construction.
  • Echec possible de la génération si le nombre de violations détectées est supérieur à un nombre configuré.