首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SonarQube + Lombok配置

SonarQube + Lombok配置
EN

Stack Overflow用户
提问于 2014-07-31 13:06:27
回答 4查看 23.7K关注 0票数 13

我正在启动一个新项目,我需要使用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 )

我该如何解决这个问题?

EN

回答 4

Stack Overflow用户

发布于 2014-08-18 10:51:19

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

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

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

代码语言:javascript
复制
<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>
票数 7
EN

Stack Overflow用户

发布于 2015-03-18 03:04:52

Stack Overflow用户

发布于 2014-07-31 15:14:07

不久前,我问了一个类似的问题:声呐4.2和lombok

基本上,您不能再用代码中的注释(比如@SuppressWarnings)来完成这个任务了。相反,您需要在SonarQube中设置(全局)规则排除:

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

代码语言:javascript
复制
Rule Key Pattern  File Path Pattern
squid:S1068       **/models/**/*.java

它使您的源代码稍微干净一些(因为您不再需要@SuppressWarnings了),但是我不喜欢设置全局规则的想法,因为它可能会导致其他问题。

更新:

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

代码语言:javascript
复制
Rule Key Pattern                                   File Path Pattern
findbugs:RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE  **/xxxxx.java

另一种可能(或不可能)对你有用:

代码语言:javascript
复制
Rule Key Pattern                        File Path Pattern
common-java:InsufficientBranchCoverage  **/models/**/*.java 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25059643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档