简体   繁体   English

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

[英]Using custom detectors with FindBugs Maven plugin

I have a nice JAR of some custom FindBugs detectors I'd like to use with the FindBugs Maven plugin. 我有一个很好的JAR的一些自定义FindBugs探测器,我想与FindBugs Maven插件一起使用。 There is a way to do this with the plugin via the <pluginList> configuration parameter, but that only accepts local files, URLs, or resources. 有一种方法可以通过<pluginList>配置参数对插件执行此操作,但只接受本地文件,URL或资源。

The only way I found for doing so is to somehow copy my JAR to a local file (maybe via the Dependency plugin) and then configure the FindBugs plugin something like this: 我发现这样做的唯一方法是以某种方式将我的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>

But this is not very flexible. 但这不是很灵活。 Is there a way to use Maven's dependency management features together with FindBugs' plugins? 有没有办法使用Maven的依赖管理功能和FindBugs的插件? I'd like to use something like this: 我想用这样的东西:

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

...but this simply overrides the core FindBugs detectors. ...但这只是覆盖core FindBugs探测器。

I found out that this is possible, although through quite some hacking. 我发现这是可能的,虽然通过一些黑客攻击。 FindBugs is only able to process plugins that are in local JARs, so you have to create one for it, but there is a more flexible way to do this then via the Dependency plugin. FindBugs只能处理本地JAR中的插件,所以你必须为它创建一个插件,但是有一种更灵活的方法可以通过Dependency插件来实现。

The <pluginList> parameter can take either a local file path, a URL or a resource (ie something from the classpath). <pluginList>参数可以采用本地文件路径,URL或资源 (即类路径中的某些内容)。 Whatever you give to it, the addressed file will be copied to target/<filename> , and passed to FindBugs itself. 无论你给它什么,被寻址的文件都将被复制到target/<filename> ,并传递给FindBugs本身。 You can pass FindBugs a JAR file if you create a JAR file that contains your JAR file. 如果创建包含JAR文件的JAR文件,则可以将FindBugs传递给JAR文件。 You can achieve this in the my-detectors project via the Assembly plugin with a descriptor like this: 您可以通过Assembly插件在my-detectors项目中实现这一点,并使用如下描述符:

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

The only other problem to solve is that the FindBugs plugin (at least version 2.3.1) uses an outdated version of the Plexus ResourceManager that extracts the my-detectors.jar incorrectly, so you have to "upgrade" that, too. 要解决的唯一其他问题是FindBugs插件(至少版本2.3.1)使用过时版本的Plexus ResourceManager错误地提取my-detectors.jar ,所以你也必须“升级”它。 Now your custom detectors will work with this: 现在您的自定义探测器将适用于此:

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

Another workaround would be to give the path to the plugin in your local repository. 另一种解决方法是在本地存储库中提供插件的路径。 There is a property for you local repository path so this would still be portable. 您可以使用本地存储库路径的属性,因此这仍然是可移植的。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>findbugs-maven-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
        <pluginList>${settings.localRepository}/path/to/plugin/1.0-SNAPSHOT/artifact-1.0-SNAPSHOT.jar</pluginList>
    </configuration>
</plugin>

Update: Since version 2.4.1 of the findbugs maven plugin there is a configuration option for exactly this usecase. 更新:从findbugs maven插件的2.4.1版开始,就有一个配置选项正好用于此用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM