使用FindBugs Maven插件的自定义检测器

Lór*_*tér 5 java maven-2 findbugs

我有一个很好的JAR的一些自定义FindBugs探测器,我想与FindBugs Maven插件一起使用.有一种方法可以通过<pluginList>配置参数对插件执行此操作,但只接受本地文件,URL或资源.

我发现这样做的唯一方法是以某种方式将我的JAR复制到本地文件(可能通过Dependency插件),然后配置FindBugs插件,如下所示:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>findbugs-maven-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
        <pluginList>${project.build.directory}/my-detectors.jar</pluginList>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

但这不是很灵活.有没有办法使用Maven的依赖管理功能和FindBugs的插件?我想用这样的东西:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>findbugs-maven-plugin</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.lptr.findbugs</groupId>
            <artifactId>my-detectors</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)

...但这只是覆盖了coreFindBugs探测器.

Lór*_*tér 2

我发现这是可能的,尽管经过了相当多的黑客攻击。FindBugs 只能处理本地 JAR 中的插件,因此您必须为其创建一个插件,但有一种更灵活的方法可以通过 Dependency 插件来执行此操作。

<pluginList>参数可以采用本地文件路径、URL 或资源(即来自类路径的内容)。无论你给它什么,寻址的文件都会被复制到target/<filename>,并传递给 FindBugs 本身。如果您创建包含您的 JAR 文件的 JAR 文件,则可以向 FindBugs 传递 JAR 文件。您可以my-detectors通过带有如下描述符的 Assembly 插件在项目中实现此目的:

<assembly>
    <id>doublepack</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <files>
        <file>
            <source>${project.build.directory}/${project.build.finalName}.jar</source>
            <destName>my-detectors.jar</destName>
        </file>
    </files>
</assembly>
Run Code Online (Sandbox Code Playgroud)

唯一需要解决的其他问题是 FindBugs 插件(至少版本 2.3.1)使用过时版本的 Plexus ResourceManager,该版本my-detectors.jar无法正确提取,因此您也必须“升级”它。现在您的自定义检测器将使用以下内容:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>findbugs-maven-plugin</artifactId>
    <configuration>
        <pluginList>my-detectors.jar</pluginList>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-resources</artifactId>
            <version>1.0-alpha-7</version>
        </dependency>
        <dependency>
            <groupId>com.lptr.findbugs</groupId>
            <artifactId>my-detectors</artifactId>
            <version>1.0</version>
            <classifier>doublepack</classifier>
        </dependency>
    </dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)