一、阿里p3c规则描述
sonar 自带检测 java 代码规则插件,也可以开发自定义的插件。《阿里巴巴Java开发手册》在业界影响很大
该指南整合了阿里巴巴集团技术团队多年来的最佳编程实践。 随着我们鼓励重用和更好地理解彼此的程序,大量的Java编程团队对项目之间的代码质量提出了苛刻的要求。 过去我们已经看到许多编程问题。 例如,有缺陷的数据库表结构和索引设计可能会导致软件体系结构缺陷和性能风险。 另一个例子是难以维护的混乱代码结构。 此外,未经身份验证的易受攻击的代码容易受到黑客的攻击。 为了解决这类问题,我们在阿里巴巴为Java开发人员开发了本文档。
阿里巴巴P3C规则官方项目:https://github.com/alibaba/p3c
获取所需的资源文件包
提取码:2264
二、Sonar-PMD添加p3c规则
1、克隆Sonar-原文链接:https://dqzboy.com PMD项目
- Sonarube的版本号与sonar-p3c-pmd是对应的,所以需要选择好版本,否则会还会报es连接不上的错误。
- 项目地址:https://github.com/jborgers/sonar-p
原文链接:https://dqzboy.com md
git clone https://github.com/jborgers/sonar-pmd.git
2、添加p3c-pmd引用
- 查看阿里p3c官方项目引入p3c的说明:https://github.com/alibaba/p3c/tree/master/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、添加p3c规则文件
- 路径
sonar-pmd-plugin/src/main/resources/org/sonar/l10n/pmd/rules/下存放规则的描述文件html - 在rules目录下新建文件夹pmd-p3c,用来存放阿里p3c规则描述文件
- 将阿里的规则文件全部拷贝到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
[root@sonar pmd]# vim rules-p3c.xml
<rules>
<!--p3c-->
<!--AlibabaJavaComments-->
<rule key="CommentsMustBeJavadocFormatRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-comment.xml/CommentsMustBeJavadocFormatRule]]></configKey>
</rule>
<rule key="AbstractMethodOrInterfaceMethodMustUseJavadocRule">
<priority>MAJOR</priority>
<configKey>
<![CDATA[rulesets/java/ali-comment.xml/AbstractMethodOrInterfaceMethodMustUseJavadocRule]]></configKey>
</rule>
<rule key="ClassMustHaveAuthorRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-comment.xml/ClassMustHaveAuthorRule]]></configKey>
</rule>
<rule key="EnumConstantsMustHaveCommentRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-comment.xml/EnumConstantsMustHaveCommentRule]]></configKey>
</rule>
<rule key="AvoidCommentBehindStatementRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-comment.xml/AvoidCommentBehindStatementRule]]></configKey>
</rule>
<rule key="RemoveCommentedCodeRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-comment.xml/RemoveCommentedCodeRule]]></configKey>
</rule>
<!--AlibabaJavaConcurrent-->
<rule key="ThreadPoolCreationRule">
<priority>BLOCKER</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/ThreadPoolCreationRule]]></configKey>
</rule>
<rule key="AvoidUseTimerRule">
<priority>BLOCKER</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/AvoidUseTimerRule]]></configKey>
</rule>
<rule key="AvoidManuallyCreateThreadRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/AvoidManuallyCreateThreadRule]]></configKey>
</rule>
<rule key="ThreadShouldSetNameRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/ThreadShouldSetNameRule]]></configKey>
</rule>
<rule key="AvoidCallStaticSimpleDateFormatRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/AvoidCallStaticSimpleDateFormatRule]]></configKey>
</rule>
<rule key="ThreadLocalShouldRemoveRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/ThreadLocalShouldRemoveRule]]></configKey>
</rule>
<rule key="AvoidConcurrentCompetitionRandomRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/AvoidConcurrentCompetitionRandomRule]]></configKey>
</rule>
<rule key="CountDownShouldInFinallyRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/CountDownShouldInFinallyRule]]></configKey>
</rule>
<rule key="LockShouldWithTryFinallyRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/LockShouldWithTryFinallyRule]]></configKey>
</rule>
<!--AlibabaJavaConstants-->
<rule key="UpperEllRule">
<priority>BLOCKER</priority>
<configKey><![CDATA[rulesets/java/ali-constant.xml/UpperEllRule]]></configKey>
</rule>
<rule key="UndefineMagicConstantRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-constant.xml/UndefineMagicConstantRule]]></configKey>
</rule>
<!--AlibabaJavaException-->
<rule key="MethodReturnWrapperTypeRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-exception.xml/MethodReturnWrapperTypeRule]]></configKey>
</rule>
<rule key="AvoidReturnInFinallyRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-exception.xml/AvoidReturnInFinallyRule]]></configKey>
</rule>
<rule key="TransactionMustHaveRollbackRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-exception.xml/TransactionMustHaveRollbackRule]]></configKey>
</rule>
<!--AlibabaJavaFlowControl-->
<rule key="SwitchStatementRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-flowcontrol.xml/SwitchStatementRule]]></configKey>
</rule>
<rule key="NeedBraceRule">
<priority>BLOCKER</priority>
<configKey><![CDATA[rulesets/java/ali-flowcontrol.xml/NeedBraceRule]]></configKey>
</rule>
<rule key="AvoidComplexConditionRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-flowcontrol.xml/AvoidComplexConditionRule]]></configKey>
</rule>
<rule key="AvoidNegationOperatorRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-flowcontrol.xml/AvoidNegationOperatorRule]]></configKey>
</rule>
<!--AlibabaJavaNaming-->
<rule key="ClassNamingShouldBeCamelRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-naming.xml/ClassNamingShouldBeCamelRule]]></configKey>
</rule>
<rule key="AbstractClassShouldStartWithAbstractNamingRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-naming.xml/AbstractClassShouldStartWithAbstractNamingRule]]></configKey>
</rule>
<rule key="ExceptionClassShouldEndWithExceptionRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-naming.xml/ExceptionClassShouldEndWithExceptionRule]]></configKey>
</rule>
<rule key="TestClassShouldEndWithTestNamingRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-naming.xml/TestClassShouldEndWithTestNamingRule]]></configKey>
</rule>
<rule key="LowerCamelCaseVariableNamingRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-naming.xml/LowerCamelCaseVariableNamingRule]]></configKey>
</rule>
<rule key="AvoidStartWithDollarAndUnderLineNamingRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-naming.xml/AvoidStartWithDollarAndUnderLineNamingRule]]></configKey>
</rule>
<rule key="ConstantFieldShouldBeUpperCaseRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-naming.xml/ConstantFieldShouldBeUpperCaseRule]]></configKey>
</rule>
<rule key="ServiceOrDaoClassShouldEndWithImplRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-naming.xml/ServiceOrDaoClassShouldEndWithImplRule]]></configKey>
</rule>
<rule key="PackageNamingRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-naming.xml/PackageNamingRule]]></configKey>
</rule>
<rule key="BooleanPropertyShouldNotStartWithIsRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-naming.xml/BooleanPropertyShouldNotStartWithIsRule]]></configKey>
</rule>
<rule key="ArrayNamingShouldHaveBracketRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-naming.xml/ArrayNamingShouldHaveBracketRule]]></configKey>
</rule>
<!--AlibabaJavaOOP-->
<rule key="EqualsAvoidNullRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-oop.xml/EqualsAvoidNullRule]]></configKey>
</rule>
<rule key="WrapperTypeEqualityRule">
<priority>BLOCKER</priority>
<configKey><![CDATA[rulesets/java/ali-oop.xml/WrapperTypeEqualityRule]]></configKey>
</rule>
<rule key="PojoMustUsePrimitiveFieldRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-oop.xml/PojoMustUsePrimitiveFieldRule]]></configKey>
</rule>
<rule key="PojoNoDefaultValueRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-oop.xml/PojoNoDefaultValueRule]]></configKey>
</rule>
<rule key="PojoMustOverrideToStringRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-oop.xml/PojoMustOverrideToStringRule]]></configKey>
</rule>
<rule key="StringConcatRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-oop.xml/PojoMustOverrideToStringRule]]></configKey>
</rule>
<rule key="BigDecimalAvoidDoubleConstructorRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-oop.xml/BigDecimalAvoidDoubleConstructorRule]]></configKey>
</rule>
<!--AlibabaJavaORM-->
<rule key="IbatisMethodQueryForListRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-orm.xml/IbatisMethodQueryForListRule]]></configKey>
</rule>
<!--AlibabaJavaOthers-->
<rule key="AvoidPatternCompileInMethodRule">
<priority>BLOCKER</priority>
<configKey><![CDATA[rulesets/java/ali-other.xml/AvoidPatternCompileInMethodRule]]></configKey>
</rule>
<rule key="AvoidApacheBeanUtilsCopyRule">
<priority>BLOCKER</priority>
<configKey><![CDATA[rulesets/java/ali-other.xml/AvoidApacheBeanUtilsCopyRule]]></configKey>
</rule>
<rule key="AvoidNewDateGetTimeRule">
<priority>BLOCKER</priority>
<configKey><![CDATA[rulesets/java/ali-other.xml/AvoidNewDateGetTimeRule]]></configKey>
</rule>
<rule key="AvoidMissUseOfMathRandomRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-other.xml/AvoidMissUseOfMathRandomRule]]></configKey>
</rule>
<rule key="MethodTooLongRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-other.xml/MethodTooLongRule]]></configKey>
</rule>
<rule key="UseRightCaseForDateFormatRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-other.xml/UseRightCaseForDateFormatRule]]></configKey>
</rule>
<rule key="AvoidDoubleOrFloatEqualCompareRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-other.xml/AvoidDoubleOrFloatEqualCompareRule]]></configKey>
</rule>
<!--AlibabaJavaSets-->
<rule key="ClassCastExceptionWithToArrayRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-set.xml/ClassCastExceptionWithToArrayRule]]></configKey>
</rule>
<rule key="UnsupportedExceptionWithModifyAsListRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-set.xml/UnsupportedExceptionWithModifyAsListRule]]></configKey>
</rule>
<rule key="ClassCastExceptionWithSubListToArrayListRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-set.xml/ClassCastExceptionWithSubListToArrayListRule]]></configKey>
</rule>
<rule key="ConcurrentExceptionWithModifyOriginSubListRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-set.xml/ConcurrentExceptionWithModifyOriginSubListRule]]></configKey>
</rule>
<rule key="DontModifyInForeachCircleRule">
<priority>BLOCKER</priority>
<configKey><![CDATA[rulesets/java/ali-set.xml/DontModifyInForeachCircleRule]]></configKey>
</rule>
<rule key="CollectionInitShouldAssignCapacityRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-set.xml/CollectionInitShouldAssignCapacityRule]]></configKey>
</rule>
</rules>
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>标签前添加如下代码:
~]# 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规则

以上就是本次文章所有知识点,如果文章对你有帮助就动动你那发财的小手,点个赞留个言,给本站加点热度。谢谢支持!

博主,你好!第3步中添加p3c规则文件 在文章顶部下载的文件中只有sonar-pmd-p3c_v3.4.tar.gz,而里面是sonar-pmd源码,未能找到规则文件呢。还请博主明示。谢谢!
那是我已经打包好的,规则文件已经放进去了
你好,请问自定义的p3c规则有没有办法加到sonorQube中?
文章你都没细看
我的意思是:规则是用java写的规则文件,这个插件工程中看到的都是配置文件,写规则的java文件是如何集成到这个插件工程的,这儿没看懂
你还是没看文章,最后需要打包呀
<!-- Alibaba P3C-PMD -->
com.alibaba.p3c
p3c-pmd
2.1.1
我的理解是这个插件中装了p3c-pmd,插件中进行了一系列配置,将p3cjava规则文件通过配置文件让sonorQube能识别到了,所以自己在p3c-pmd规则工程中自定义的规则是不是就没办法集成到sonorQube了?
你好,步骤一公众号下载的压缩文件解压密码没有,请说一下密码,谢谢!
本站点域名
你好,重启sonorQube时报错,能帮忙看下不?
ERROR web[][o.s.s.p.Platform] Background initialization failed. Stopping SonarQube
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘jdk.internal.loader.ClassLoaders$AppClassLoader@4e25154f-org.sonar.server.rule.RegisterRules’: Initialization
初始化有问题,检查下插件版本,依赖这些,具体查看sonarqube的日志
已解决,请忽略!
你好,有个问题
在 章节 二 .3 添加p3c规则文件 中提到了创建文件 rules-p3c.xml ,请问该文件中需要添加规则吗
需要添加,文章里面好像没写是吧?
是的,文章中没写。我在按这个过程编译打包后,放到sonarqube上重启失败了。
文章更新了,你刷新看下。我看官网GitHub更新版本了 不知道之前的版本是否还能正常集成 你测试下吧
谢谢,这个可以的。已经装好了。
文章中 pmd-model.xml 的代码第二行,是否多了 标签”“