【问题标题】:cobertura-maven-plugin conflicts with FindBugscobertura-maven-plugin 与 FindBugs 冲突
【发布时间】:2015-06-18 02:54:37
【问题描述】:

在将 cobertura-maven-plugin2.6 更新到 2.7 后,Cobertura 插件与 FindBugs 插件发生冲突。 FindBugs 插件在 cobertura 检测代码中检测到错误:

[INFO] Incorrect lazy initialization of static field pl.chilldev.sites.commons.ErrorCode.__cobertura_counters in pl.chilldev.sites.commons.ErrorCode.__cobertura_init() [pl.chilldev.sites.commons.ErrorCode] In ErrorCode.java

(当 Cobertura 插件版本设置为 2.6 时一切正常)

以防万一,FindBugs 插件版本是 3.0.1。

是否可以通过某种方式将这些插件设置为协同工作?

编辑 1 (pom.xml)

这是主项目目录的pom.xml(子模块只包含依赖列表):

<?xml version="1.0" encoding="utf-8"?>
<!--
# This file is part of the pl.chilldev.sites.
#
# @copyright 2015 © by Rafał Wrzeszcz - Wrzasq.pl.
-->
<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>

    <!-- core project settings -->
    <groupId>pl.chilldev.sites</groupId>
    <artifactId>sites</artifactId>
    <packaging>pom</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <!-- project meta info -->
    <name>ChillDev-Sites</name>
    <url><!-- TODO --></url>
    <description>Content sites storage service.</description>
    <inceptionYear>2015</inceptionYear>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <organization>
        <name>Rafał Wrzeszcz - Wrzasq.pl</name>
        <url>http://wrzasq.pl/</url>
    </organization>

    <!-- plugins configuration -->
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <archive>
                            <index>true</index>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                            <manifestEntries>
                                <url>${project.url}</url>
                                <Specification-Title>${project.name}</Specification-Title>
                                <Specification-Version>${project.version}</Specification-Version>
                                <Specification-Vendor>Rafał Wrzeszcz - Wrzasq.pl; Chillout Development</Specification-Vendor>
                                <Implementation-Title>${project.name}</Implementation-Title>
                                <Implementation-Version>${project.version}</Implementation-Version>
                                <Implementation-Vendor>Rafał Wrzeszcz - Wrzasq.pl; Chillout Development</Implementation-Vendor>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArgs>
                        <arg>-Xlint:all</arg>
                    </compilerArgs>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <escapeString>\</escapeString>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                            <goal>cpd-check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.15</version>
                <dependencies>
                    <!-- Maven Checkstyle plugin has a 6.1.1 version by default which is buggy -->
                    <dependency>
                        <groupId>com.puppycrawl.tools</groupId>
                        <artifactId>checkstyle</artifactId>
                        <version>6.5</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <configLocation>src/main/checkstyle/checkstyle.xml</configLocation>
                    <propertyExpansion>checkstyle.project.basedir=${project.basedir}</propertyExpansion>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.4</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.doxia</groupId>
                        <artifactId>doxia-module-markdown</artifactId>
                        <version>1.6</version>
                    </dependency>

                    <dependency>
                        <groupId>lt.velykis.maven.skins</groupId>
                        <artifactId>reflow-velocity-tools</artifactId>
                        <version>1.1.1</version>
                    </dependency>

                    <dependency>
                        <groupId>org.apache.velocity</groupId>
                        <artifactId>velocity</artifactId>
                        <version>1.7</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pdf-plugin</artifactId>
                <version>1.3</version>
                <!-- TODO:
                    - fix rendering of company and project logos
                    - add UTF-8 font
                -->
                <executions>
                    <execution>
                        <phase>site</phase>
                        <goals>
                            <goal>pdf</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.10.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <effort>Max</effort>
                    <threshold>Low</threshold>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

    <!-- reporting plugins -->
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.10.2</version>
                <configuration>
                    <show>private</show>
                    <excludePackageNames>test.pl.chilldev.sites.*</excludePackageNames>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.4</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.15</version>
                <configuration>
                    <configLocation>src/main/checkstyle/checkstyle.xml</configLocation>
                    <propertyExpansion>checkstyle.project.basedir=${project.basedir}</propertyExpansion>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jxr-plugin</artifactId>
                <version>2.5</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>2.18.1</version>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <!-- TODO: 2.7 doesn't work well with Findbugs -->
                <version>2.6</version>
                <configuration>
                    <formats>
                        <format>html</format>
                    </formats>
                    <instrumentation>
                        <excludes>
                            <exclude>test/**/*.class</exclude>
                        </excludes>
                    </instrumentation>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <effort>Max</effort>
                    <threshold>Low</threshold>
                </configuration>
            </plugin>
        </plugins>
    </reporting>

    <!-- project dependencies -->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- components of the project -->
    <modules>
        <module>sites-backend</module>
        <module>sites-commons</module>
        <module>sites-core</module>
        <module>sites-frontend</module>
        <module>sites-rpc-client-backend</module>
        <module>sites-rpc-client-frontend</module>
        <module>sites-rpc-service</module>
    </modules>
</project>

编辑 2 (ErrorCode.java)

package pl.chilldev.sites.commons;

/**
 * JSON-RPC error codes.
 */
public class ErrorCode
{
    /**
     * Dummy code that represents successful operation - should not be used at all.
     */
    public static final int OK = 0;

    /**
     * No entity of specified ID exists.
     */
    public static final int NO_ENTITY = 1;
}

【问题讨论】:

  • 你能告诉我们你的 pom 定义吗?
  • 当然,我编辑了问题帖。帖子中的那个现在可以工作 - 当我将 cobertura-maven-plugin 版本更改为 2.7 时,它会破坏 FindBugs 插件检查。
  • 能否也列出ErrorCode.java?
  • 已添加到帖子中。但我尝试删除这个类 - 它只是 FindBugs 发现的第一个。当我删除这个时,它会发布关于下一个的相同错误。

标签: maven findbugs cobertura


【解决方案1】:

如果您强制 findbugs 在“编译”阶段而不是“测试”阶段运行,它将导致 findbugs 在代码被 cobertuar 检测之前运行,从而解决 Mateusz Balbus 所述的问题。这个 findbugs maven 配置对我有用:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>findbugs-maven-plugin</artifactId>
    <version>3.0.5</version>
    <configuration>
        <executions>
            <execution>
                <id>findbugs-check</id>
                <phase>compile</phase>
                <goals>
                    <goal>check</goal>
                </goals>
            </execution>
        </executions>
    </configuration>
</plugin>

【讨论】:

    【解决方案2】:

    Cobertura 2.7 更改了project.build.outputDirectory 以生成集成测试报告(2.7 中引入的新报告类型)。在下一步中,Findbugs 不仅会检查已编译类的违规行为,还会检查已检测的违规行为。

    查看错误:

    http://jira.codehaus.org/browse/MCOBERTURA-203

    作为一种解决方法,您可以覆盖classFilesDirectory

    ...
    <build>
    ...
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <effort>Max</effort>
                    <threshold>Low</threshold>
                    <classFilesDirectory>${project.build.outputDirectory}</classFilesDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    ...
    </build>
    ...
    

    另一种解决方案是跳过集成测试报告并仅处理单元测试报告:

    http://mojo.codehaus.org/cobertura-maven-plugin/usage.html#Using_different_reports

    【讨论】:

    • 非常感谢!确实是这样。然而,在调查了这个问题之后,我认为这样的变通办法不是一个好的解决方案,因为真正的问题仍然存在于检测类中。所以我决定降级到 2.6 并暂时保留它。以损坏(不是字面意思,而是修改)二进制文件为代价传递 Maven 构建是不值得这样做的。
    • 是的,很好的答案。 (y)
    • 另外,如果你有 findbugs 作为buildreporting 插件,请确保将cobertura 报告插件作为报告插件列表中的第一个条目。否则 findbugs build 配置会在您的 reporting 配置之后运行,从而覆盖并破坏您网站的报告。
    【解决方案3】:

    首先需要了解您的项目结构。

    我认为使用传递依赖可以解决您的问题,但不确定。

    它做了两件事。

    1. 为子模块/子项目中的依赖项设置默认版本
    2. 覆盖传递依赖的版本

    它确实覆盖了传递依赖中的指定值。

    enforcer 插件不会忽略 dependencyManagement。但由于 传递依赖的 版本在它开始工作之前已被更改,因此无法识别差异。

    这是一篇不错的文章:您可以阅读它: http://andydennie.com/2012/08/02/maven-enforcer-plugin-vs-dependencymanagement/

    另一个来源:http://maven.apache.org/enforcer/maven-enforcer-plugin/

    谢谢。

    【讨论】:

    • 要么你没看懂问题,要么我没看懂答案...问题不在于依赖冲突,问题在于两个构建插件不能和平共存相同的构建。我想同时使用。即使在没有模块的扁平结构项目中,它们也会失败。
    • 是的,这是误解。 Mateusz Balbus 的答案是正确的,虽然这个答案没有错但不能解决这个问题。 @RafałWrzeszcz 我应该删除这个答案吗?我认为不是 bcz 也有人可以从中获取信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 1970-01-01
    • 2014-10-26
    • 2013-02-20
    相关资源
    最近更新 更多