Hiện tại không có phương tiện để thực hiện việc này bằng plugin chuẩn. Bạn có thể tạo một plugin để đọc findbugsChecks.xml và xuất thông tin bạn cần.
Mã bên dưới sẽ xuất tổng số lỗi tìm thấy và các lỗi trên mỗi gói cho bất kỳ dự án nào bằng findbugsChecks.xml trong thư mục đầu ra. Bạn có thể cấu hình tên của tập tin nó đọc bằng cách thiết lập thuộc tính findBugsChecks vào cấu hình:
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);
}
}
}
}
Để gói mã này, thêm nó vào src chính thư mục// java của một Mavenproject với POM như thế này:
<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>
Sau đó chạy mvn install để cài đặt plugin.
Để thực sự sử dụng nó, bạn có thể chạy nó như một mục tiêu bổ sung trên dòng lệnh, hoặc liên kết nó với dự án của bạn để chạy như một phần của vòng đời tiêu chuẩn.
Đây là lệnh để chạy từ dòng lệnh (giả sử dự án trước đây đã được biên soạn:
mvn findbugs:check name.seller.rich:maven-findbugs-stats-plugin:0.0.1:stats
Để ràng buộc cấu hình để dự án của bạn vì vậy nó sẽ được chạy trên từng xây dựng, sử dụng cấu hình sau:
<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>
Nguồn
2009-08-12 09:00:59
Tôi thực sự không thể hiểu tại sao tính năng này không được triển khai trước tiên trong plugin ... Lạ. – yegor256
Đã gửi một vé: https://sourceforge.net/tracker/?func=detail&aid=3111339&group_id=61626&atid=497856 – yegor256