DevOps / Jenkins

SonarQube集成阿里p3c代码规范

浅时光博客 · 1月16日 · 2023年 · · 本文21231个字,预计阅读54分钟 2755次已读

一、阿里p3c规则描述


sonar 自带检测 java 代码规则插件,也可以开发自定义的插件。《阿里巴巴Java开发手册》在业界影响很大

该指南整合了阿里巴巴集团技术团队多年来的最佳编程实践。 随着我们鼓励重用和更好地理解彼此的程序,大量的Java编程团队对项目之间的代码质量提出了苛刻的要求。 过去我们已经看到许多编程问题。 例如,文章来源(Source):浅时光博客有缺陷的数据库表结构和索引设计可能会导致软件体系结构缺陷和性能风险。 另一个例子是难以维护的混乱代码结构。 此外,未经身份验证的易受攻击的代码容易受到黑客的攻击。 为了解决这类问题,我们在阿里巴巴为Java开发人员开发了本文档。

阿里巴巴P3C规则官方项目:https://github.com/alibaba/p3c

扫描下方二维码,回复关键词【sonar-p3c】获取到所需的资源文件包。

二、Sonar-PMD添加p3c规则


1、克隆Sonar-PMD项目

git clone https://github.com/jborgers/sonar-pmd.git

2、添加p3c-pmd引用

[root@sonar ~]# cd sonar-pmd
[root@sonar sonar-pmd]# cd sonar-pmd-plugin/

#注意版本
      <!-- Alibaba P3C-PMD -->
      <dependency>
        <groupId>com.alibaba.p3c</groupId>
        <artifactId>p3c-pmd</artifactId>
        <version>2.1.1</version>
      </dependency>

3文章来源(Source):https://www.dqzboy.com、添加p3c规则文件

  1. 路径sonar-pmd-plugin/src/main/resources/org/sonar/l10n/pmd/rules/下存放规则的文章来源(Source):https://www.dqzboy.com描述文件html
  2. 在rules目录下新建文件夹pmd-p3c,用来存放阿里p3c规则描述文件
  3. 将阿里的规则文件全部拷贝到pmd-p3c目录下(请在文章顶部下载规则文件)
[root@sonar ~]# cd /root/sonar-pmd/sonar-pmd-plugin/src/main/resources/org/sonar/l10n/pmd/rules/
[root@sonar rules]# mkdir pmd-p3c
  • 然后在路径sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/下新建rules-p3c.xml

4、添加p3c规则配置

pmd.properties

  • 首先向pmd.properties中添加阿里规则,在文件末尾追加如下代码即可
[root@sonar ~]# vim /root/sonar-pmd/sonar-pmd-plugin/src/main/resources/org/sonar/l10n/pmd.properties
##p3c##
#AlibabaJavaComments
rule.pmd.CommentsMustBeJavadocFormatRule.name=[p3c]Javadoc should be used for classes, class variables and methods. The format should be '/** comment **/', rather than '// xxx'.
rule.pmd.AbstractMethodOrInterfaceMethodMustUseJavadocRule.name=[p3c]Abstract methods (including methods in interface) should be commented by Javadoc.
rule.pmd.ClassMustHaveAuthorRule.name=[p3c]Every class should include information of author(s) and date.
rule.pmd.EnumConstantsMustHaveCommentRule.name=[p3c]All enumeration type fields should be commented as Javadoc style.
rule.pmd.AvoidCommentBehindStatementRule.name=[p3c]Single line comments in a method should be put above the code to be commented, by using // and multiple lines by using /* */.
rule.pmd.RemoveCommentedCodeRule.name=[p3c]Codes or configuration that is noticed to be obsoleted should be resolutely removed from projects.

#AlibabaJavaConcurrent
rule.pmd.ThreadPoolCreationRule.name=[p3c]Manually create thread pool is better.
rule.pmd.AvoidUseTimerRule.name=[p3c]Use ScheduledExecutorService instead.
rule.pmd.ThreadShouldSetNameRule.name=[p3c]A meaningful thread name is helpful to trace the error information,so assign a name when creating threads or thread pools.
rule.pmd.AvoidCallStaticSimpleDateFormatRule.name=[p3c]SimpleDataFormat is unsafe, do not define it as a static variable. If have to, lock or DateUtils class must be used.
rule.pmd.ThreadLocalShouldRemoveRule.name=[p3c]type 'ThreadLocal' must call remove() method at least one times.
rule.pmd.AvoidConcurrentCompetitionRandomRule.name=[p3c]Avoid using [Math.random()] by multiple threads.
rule.pmd.CountDownShouldInFinallyRule.name=[p3c]should be called in finally block.
rule.pmd.AvoidManuallyCreateThreadRule.name=[p3c]Explicitly creating threads is not allowed, use thread pool instead.
rule.pmd.LockShouldWithTryFinallyRule.name=[p3c]Lock operation [%s] must immediately follow by try block, and unlock operation must be placed in the first line of finally block.

#AlibabaJavaConstants
rule.pmd.UpperEllRule.name=[p3c]'L' instead of 'l' should be used for long or Long variable.
rule.pmd.UndefineMagicConstantRule.name=[p3c]Magic values, except for predefined, are forbidden in coding.

#AlibabaJavaExceptions
rule.pmd.MethodReturnWrapperTypeRule.name=[p3c]If the return type is primitive, return a value of wrapper class may cause NullPointerException.
rule.pmd.AvoidReturnInFinallyRule.name=[p3c]Never use return within a finally block.
rule.pmd.TransactionMustHaveRollbackRule.name=[p3c]Attribute rollbackFor of annotation Transactional must be set.

#AlibabaJavaFlowControl
rule.pmd.SwitchStatementRule.name=[p3c]In a switch block, each case should be finished by break/return.
rule.pmd.NeedBraceRule.name=[p3c]Braces are used with if, else, for, do and while statements, even if the body contains only a single statement.
rule.pmd.AvoidComplexConditionRule.name=[p3c]Do not use complicated statements in conditional statements.
rule.pmd.AvoidNegationOperatorRule.name=[p3c]The negation operator is not easy to be quickly understood.

#AlibabaJavaNaming
rule.pmd.ClassNamingShouldBeCamelRule.name=[p3c]Class names should be nouns in UpperCamelCase except domain models: DO, BO, DTO, VO, etc.
rule.pmd.AbstractClassShouldStartWithAbstractNamingRule.name=[p3c]Abstract class names must start with Abstract or Base.
rule.pmd.ExceptionClassShouldEndWithExceptionRule.name=[p3c]Must be ended with Exception.
rule.pmd.TestClassShouldEndWithTestNamingRule.name=[p3c]Test cases should be ended with Test.
rule.pmd.LowerCamelCaseVariableNamingRule.name=[p3c]Method names, parameter names, member variable names, and local variable names should be written in lowerCamelCase.
rule.pmd.AvoidStartWithDollarAndUnderLineNamingRule.name=[p3c]All names should not start or end with an underline or a dollar sign.
rule.pmd.ServiceOrDaoClassShouldEndWithImplRule.name=[p3c]Constant variable names should be written in upper characters separated by underscores.
rule.pmd.ConstantFieldShouldBeUpperCaseRule.name=[p3c]Constant variable names should be written in upper characters separated by underscores.
rule.pmd.PackageNamingRule.name=[p3c]All Service and DAO classes must be interface based on SOA principle. Implementation class names.
rule.pmd.BooleanPropertyShouldNotStartWithIsRule.name=[p3c]Do not add 'is' as prefix while defining Boolean variable.
rule.pmd.ArrayNamingShouldHaveBracketRule.name=[p3c]Brackets are a part of an Array type. The definition could be: String[] args

#AlibabaJavaOop
rule.pmd.EqualsAvoidNullRule.name=[p3c]Equals should be invoked by a constant or an object that is definitely not null.
rule.pmd.WrapperTypeEqualityRule.name=[p3c]The wrapper classes should be compared by equals method rather than by symbol of '==' directly.
rule.pmd.PojoMustUsePrimitiveFieldRule.name=[p3c]Rules for using primitive data types and wrapper classes.
rule.pmd.PojoNoDefaultValueRule.name=[p3c]While defining POJO classes like DO, DTO, VO, etc., do not assign any default values to the members.
rule.pmd.PojoMustOverrideToStringRule.name=[p3c]We can call the toString method in a POJO directly to print property values.
rule.pmd.StringConcatRule.name=[p3c]Use the append method in StringBuilder inside a loop body when concatenating multiple strings.
rule.pmd.BigDecimalAvoidDoubleConstructorRule.name=[p3c]Avoid using the constructor BigDecimal(double) to convert double value to a BigDecimal object.

#orm
rule.pmd.IbatisMethodQueryForListRule.name=[p3c]iBatis built in com.ibatis.sqlmap.client.SqlMapClient.queryForList(String statementName,int start,int size) is not recommended

#AlibabaJavaOthers
rule.pmd.AvoidPatternCompileInMethodRule.name=[p3c]When using regex, precompile needs to be done in order to increase the matching performance.
rule.pmd.AvoidApacheBeanUtilsCopyRule.name=[p3c]Avoid using *Apache BeanUtils* to copy attributes.
rule.pmd.AvoidNewDateGetTimeRule.name=[p3c]Use System.currentTimeMillis() to get the current millisecond. Do not use new Date().getTime().
rule.pmd.AvoidMissUseOfMathRandomRule.name=[p3c]The return type of Math.random() is double, value range is 0<=x<1 (0 is possible).
rule.pmd.MethodTooLongRule.name=[p3c]The total number of lines for a method should not be more than 80.
rule.pmd.UseRightCaseForDateFormatRule.name=[p3c]Date format string [%s] is error,When doing date formatting, 'y' should be written in lowercase for 'year'.
rule.pmd.AvoidDoubleOrFloatEqualCompareRule.name=[p3c]To judge the equivalence of floating-point numbers, == cannot be used for primitive types, while equals cannot be used for wrapper classes.

#AlibabaJavaSets
rule.pmd.ClassCastExceptionWithToArrayRule.name=[p3c]Do not use toArray method without arguments.
rule.pmd.UnsupportedExceptionWithModifyAsListRule.name=[p3c]Do not use methods which will modify the list after using Arrays.asList to convert array to list.
rule.pmd.ClassCastExceptionWithSubListToArrayListRule.name=[p3c]Do not cast subList in class ArrayList, otherwise ClassCastException will be thrown.
rule.pmd.ConcurrentExceptionWithModifyOriginSubListRule.name=[p3c]When using subList, be careful to modify the size of original list.
rule.pmd.DontModifyInForeachCircleRule.name=[p3c]Do not remove or add elements to a collection in a foreach loop.
rule.pmd.CollectionInitShouldAssignCapacityRule.name=[p3c]HashMap should set a size when initializing.

pmd-model.xml

  • 然后向pmd-model.xml中添加阿里规则,首先区分定义阿里规则,在文件末尾</sqale>文章来源(Source):https://www.dqzboy.com;标签前添加如下代码:
~]# vim /root/sonar-pmd/sonar-pmd-plugin/src/main/resources/com/sonar/sqale/pmd-model.xml
#在文件末尾</chc></sqale>标签前添加如下代码
    <chc>
        <key>P3C-PMD</key>
        <name>p3c-pmd</name>
        <chc>
            <key>P3C_PMD</key>
            <name>p3c pmd</name>
            <!--AlibabaJavaComments-->
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>CommentsMustBeJavadocFormatRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AbstractMethodOrInterfaceMethodMustUseJavadocRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>ClassMustHaveAuthorRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>EnumConstantsMustHaveCommentRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AvoidCommentBehindStatementRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>RemoveCommentedCodeRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>

            <!--AlibabaJavaConcurrent-->
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>ThreadPoolCreationRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AvoidUseTimerRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>ThreadShouldSetNameRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AvoidCallStaticSimpleDateFormatRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>ThreadLocalShouldRemoveRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AvoidConcurrentCompetitionRandomRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>CountDownShouldInFinallyRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>

            <!--AlibabaJavaConcurrent-->
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>UpperEllRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>UndefineMagicConstantRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>UndefineMagicConstantRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AvoidManuallyCreateThreadRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>

            <!--AlibabaJavaExceptions-->
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>MethodReturnWrapperTypeRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AvoidReturnInFinallyRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>TransactionMustHaveRollbackRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>

            <!--AlibabaJavaFlowControl-->
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>SwitchStatementRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>NeedBraceRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AvoidComplexConditionRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AvoidNegationOperatorRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>

            <!--AlibabaJavaNaming-->
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>ClassNamingShouldBeCamelRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AbstractClassShouldStartWithAbstractNamingRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>

            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>ExceptionClassShouldEndWithExceptionRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>TestClassShouldEndWithTestNamingRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>LowerCamelCaseVariableNamingRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AvoidStartWithDollarAndUnderLineNamingRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>ConstantFieldShouldBeUpperCaseRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>ServiceOrDaoClassShouldEndWithImplRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>PackageNamingRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>BooleanPropertyShouldNotStartWithIsRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>ArrayNamingShouldHaveBracketRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>

            <!--AlibabaJavaOop-->
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>EqualsAvoidNullRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>WrapperTypeEqualityRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>WrapperTypeEqualityRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>PojoMustUsePrimitiveFieldRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>PojoNoDefaultValueRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>PojoMustOverrideToStringRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>StringConcatRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>

            <!-- orm -->
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>IbatisMethodQueryForListRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <!--AlibabaJavaOthers-->
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AvoidPatternCompileInMethodRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AvoidApacheBeanUtilsCopyRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AvoidNewDateGetTimeRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>AvoidMissUseOfMathRandomRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>MethodTooLongRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>UseRightCaseForDateFormatRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>

            <!--AlibabaJavaSets-->
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>ClassCastExceptionWithToArrayRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>UnsupportedExceptionWithModifyAsListRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>ClassCastExceptionWithSubListToArrayListRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>ConcurrentExceptionWithModifyOriginSubListRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>DontModifyInForeachCircleRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
            <chc>
                <rule-repo>pmd</rule-repo>
                <rule-key>CollectionInitShouldAssignCapacityRule</rule-key>
                <prop>
                    <key>remediationFunction</key>
                    <txt>CONSTANT_ISSUE</txt>
                </prop>
                <prop>
                    <key>offset</key>
                    <val>2</val>
                    <txt>min</txt>
                </prop>
            </chc>
        </chc>
    </chc>

5、添加p3c规则的引入

~]# vim /root/sonar-pmd/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/rule/PmdRulesDefinition.java
RulesDefinitionXmlLoader ruleLoader = new RulesDefinitionXmlLoader();
ruleLoader.load(repository, PmdRulesDefinition.class.getResourceAsStream("/com/sonar/sqale/c"), "UTF-8");

//alibaba-pmd-p3c
extractRulesData(repository, "/org/sonar/plugins/pmd/rules-p3c.xml", "/org/sonar/l10n/pmd/rules/pmd-p3c");

6、构建pmd-p3c插件包

  • 注意:JAVA版本要在11以上
[root@sonar ~]# cd sonar-pmd
[root@sonar sonar-pmd]# mvn package -Dmaven.test.skip=true

三、SonarQube导入插件


  • sonar-pmd-plugin/target/下的sonar-pmd-plugin.jar包放于sonarqube/extensions/plugins/ 中,重启服务
  • 注意:如果之前有旧版本的pmd插件,需要删除
#将构建的插件包拷贝到该目录下
[root@sonar plugins]# cp /root/sonar-pmd/sonar-pmd-plugin/target/sonar-pmd-plugin-3.4.0-SNAPSHOT.jar ./

#更改插件属组跟属主
[root@sonar ~]# chown -R sonar. /usr/local/sonarqube/extensions/plugins/

#导入之后,重启sonarqube服务
[root@sonar ~]# systemctl restart sonar.service

#检查启动日志
[root@sonarqube ~]# tailf /usr/local/sonarqube/logs/sonar.20220509.log

四、SonarQube中启用p3c规则


1、激活pmd-p3c规则

2、检查pmd-p3c规则

以上就是本次文章所有知识点,如果文章对你有帮助就动动你那发财的小手,点个赞留个言,给文章来源(Source):https://www.dqzboy.com本站加点热度。谢谢支持!


本文作者:浅时光博客
原文链接:https://www.dqzboy.com/10462.html
版权声明:知识共享署名-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)协议进行许可,转载时请以超链接形式标明文章原始出处和作者信息
免责声明:本站发布的内容(图片、视频和文字)以及一切破解补丁、注册激活和软件解密分析文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。访问和下载本站内容,说明您已同意上述条款!


0 条回应

必须 注册 为本站用户, 登录 后才可以发表评论!