【问题标题】:SonarQube + Lombok configurationSonarQube + Lombok 配置
【发布时间】:2014-09-23 10:36:27
【问题描述】:

我正在开始一个新项目,我需要使用SonarQube,我想使用Lombok,我已经在 Eclipse 中配置了它,除了静态分析之外一切正常。

  • 未使用的私有字段:当我有一个@Data 类时,所有字段都报告为Unused private field
  • @Getter(lazy=true):当我使用这个注解时,我得到了Redundant nullcheck of value known to be non-null,见@Getter(lazy=true)(这与编译代码有关)。

我认为一个可能的解决方案是delombok 项目,编译并运行Sonar。

SonarQube Jira中的类似问题:

@SuppressWarnings("PMD.UnusedPrivateField") 解决方案不适用于最新的SonarQube 4.2

我该如何解决这个问题?

【问题讨论】:

    标签: sonarqube lombok


    【解决方案1】:

    前段时间我问了一个类似的问题: sonarqube 4.2 and lombok

    基本上,您不能再在代码中使用注释(如@SuppressWarnings)了。相反,您需要在 SonarQube 中设置(全局)规则排除:

    点击设置->排除->问题 并在“忽略多个标准的问题”部分添加条目,然后输入如下内容:

    Rule Key Pattern  File Path Pattern
    squid:S1068       **/models/**/*.java
    

    它使你的源代码更简洁一些(因为你不再需要@SuppressWarnings),但我不喜欢设置全局规则的想法,因为它可能会导致其他问题。


    更新:

    对于“已知为非空值的冗余空值检查”,您可以添加如下内容:

    Rule Key Pattern                                   File Path Pattern
    findbugs:RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE  **/xxxxx.java
    

    还有一个可能(或可能不会)对您有用的:

    Rule Key Pattern                        File Path Pattern
    common-java:InsufficientBranchCoverage  **/models/**/*.java 
    

    【讨论】:

    • 是的,我赞成您的问题,但您的解决方案仅适用于 @Data 注释,并且我有一些带有 @Getter @Setter@ManagedBeans (JSF) 类,并非所有项目都使用 lombock,并且并非所有字段都使用注释,所以,我认为,就我而言,这不是一个解决方案。对于@Getter(lazy=true),此解决方案不适用。
    • 在我的项目中,我也有设置来涵盖“冗余 nullcheck”警告。您只需要找到这些规则键并向它们添加例外即可。 (另见我上面的更新)
    • 我认为lombok 应该提高可读性并减少代码大小,而不是强制禁用对某些情况可能有用的声纳规则。
    • @AVolpe:我敢打赌,Lombok 做得恰到好处,而 Sonar 是错误的。 1. 使用@Data,在 Lombok 生成 getter 之前,这些字段确实未被使用。 如何 这样做并不重要,问题在于检查工具没有考虑到生成。 2. 关于“已知非空值的冗余空值检查”,问题是该值不能保证为非空值,details
    • @maaartinus Redundant nullcheck@Data 结合使用时不会出现,它出现在没有@Data 的类中。而且你说得对,这不是 lombok 问题,而是 SonarQube 问题,我不怪 lombok 这个问题,我只是说这是必要的另一个不强制减少规则集的解决方案,例如,当你用@PostConstruct 标记一个方法并将其设为私有,Sonar 会抛出未使用的规则,我认为我们需要定义一种机制来使用特定注释来处理一些方法/字段。
    【解决方案2】:

    作为一种解决方法,我现在让声纳对 delombok 生成的代码进行分析。

    我想这也不是一个完美的解决方案,因为我正在分析生成的代码,而不是实际由开发人员编写的代码。我发现它比使用 @SuppressWarnings, //NOSONAR 或关闭 Sonar 本身的规则更好。

    请参阅下面的示例以在 Maven 中实现此目的。将此添加到您的 pom.xml:

    <properties>
        ...
        <!-- This is exposed as a workaround to do the sonar analysis in combination with delombok -->
        <src.dir>src/main/java</src.dir>
        ...
    </properties>
    ...
    <plugins>
        ...
        <plugin>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-maven-plugin</artifactId>
            <version>${lombok-plugin.version}</version>
            <executions>
                <execution>
                    <phase>verify</phase>
                    <goals>
                        <goal>delombok</goal>
                    </goals>
                    <configuration>
                        <addOutputDirectory>false</addOutputDirectory>
                        <sourceDirectory>src/main/java</sourceDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        ...
    </plugins>
    ...
    <profiles>
    ...
    <profile>
            <!-- we have to use this profile to analyse code with sonar until https://jira.codehaus.org/browse/MSONAR-70 is fixed ! -->
            <id>sonar</id>
            <properties>
                <src.dir>target/generated-sources/delombok</src.dir>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-maven-plugin</artifactId>
                        <version>${lombok-plugin.version}</version>
                        <executions>
                            <execution>
                                <phase>verify</phase>
                                <goals>
                                    <goal>delombok</goal>
                                </goals>
                                <configuration>
                                    <addOutputDirectory>true</addOutputDirectory>
                                    <sourceDirectory>src/main/java</sourceDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>sonar-maven-plugin</artifactId>
                        <version>2.2</version>
                    </plugin>
                </plugins>
            </build>
        </profile>
        ...
    </profiles>
    

    【讨论】:

    • 我在我的回答中添加了一个 Maven 示例。我试图只放置 pom 的相关部分,所以如果您还有其他问题,请随时提问。同时我注意到jira.codehaus.org/browse/MSONAR-70 是固定的,所以可能上面的代码可以以某种方式简化。
    • 这不会导致 SCM 插件/责备出现问题吗?
    【解决方案3】:
    【解决方案4】:

    对于多模块项目,基于 finrod 的回答中提到的内容,我必须在我的声纳配置文件中添加以下属性以避免重复违规(声纳正在分析 src/main/java 和 target/generated-sources/德隆波克)

    <properties>
    
        <!-- Sonar will analyze the delombokized version of the code -->
        <sonar.exclusions>src/main/java/**/*</sonar.exclusions>
    
    </properties>
    

    【讨论】:

      猜你喜欢
      • 2018-03-03
      • 1970-01-01
      • 2018-08-14
      • 2014-11-26
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 2016-11-23
      相关资源
      最近更新 更多