The sonar-pmd plugin adds the rules of Ali p3c-pmd

Reference: http://younian.net.cn/article/143 and https://github.com/mrprince/sonar-p3c-pmd project made a package, throw it into sonar [version 6.5 (build 27846)] and it works fine .

Download address: sonar-pmd-plugin-2.7-SNAPSHOT.jar.zip

Another disadvantage is that the description files provided in the sonar-p3c-pmd project are not detailed enough, and there is no Chinese.

It is a bit tiring to redo according to the p3c-pmd, but it is good to have it, and the basic effect is not bad with the IDE plug-in.

The descriptions are all in: p3c/p3c-pmd/src/main/resources/messages.xml (PS: If you have time, you can write a program to transfer

sonar effect

Bottom) I

dea p3c protocol scan effect

 

Finally, copy the article [http://younian.net.cn/article/143]

Ready to make changes:

  1. Add POM dependencies:
     
    1. <!-- https://mvnrepository.com/artifact/com.alibaba.p3c/p3c-pmd -->
    2. <dependency>
    3. <groupId>com.alibaba.p3c</groupId>
    4. <artifactId>p3c-pmd</artifactId>
    5. <version>1.3.0</version>
    6. </dependency>
    The following takes a Rule as an example to modify and add;
  2. add pmd.properties

    rule.pmd.CommentsMustBeJavadocFormatRule.name=CommentsMustBeJavadocFormatRule

  3. add rules.xml

     
    1. <rule key="CommentsMustBeJavadocFormatRule">
    2. <priority>MAJOR</priority>
    3. <configKey><![CDATA[rulesets/java/ali-comment.xml/CommentsMustBeJavadocFormatRule]]> </configKey>
    4. </rule>
  4. Add the pmd-model.xml file (the specific meaning is unknown)

     
    1. <chc>
    2. <rule-repo>p3c</rule-repo>
    3. <rule-key>CommentsMustBeJavadocFormatRule</rule-key>
    4. <prop>
    5. <key>remediationFunction</key>
    6. <txt>CONSTANT_ISSUE</txt>
    7. </prop>
    8. <prop>
    9. <key>offset</key>
    10. <val>2</val>
    11. <txt>min</txt>
    12. </prop>
    13. </chc>
  5. Add a description file - CommentsMustBeJavadocFormatRule.html. The content comes from the corresponding xml of p3c for the display of the error details page

     
    1. <p>Look for qualified this usages in the same class.</p>
    2. <p>Examples:</p>
    3. <pre>
    4. /**
    5. *
    6. * XXX class function description.
    7. *
    8. */
    9. public class XxClass implements Serializable {
    10. private static final long serialVersionUID = 113323427779853001L;
    11. /**
    12. * id
    13. */
    14. private Long id;
    15. /**
    16. * title
    17. */
    18. private String title;
    19. /**
    20. * find by id
    21. *
    22. * @param ruleId rule id
    23. * @param page start from 1
    24. * @return Result<Xxxx>
    25. */
    26. public Result<Xxxx> funcA(Long ruleId, Integer page) {
    27. return null;
    28. }
    29. }
    30. </pre>

For the time being, it is added on the original basis, and the existing part of the rule is not deleted.

test analysis:

  1. Install sonarqube:
    download sonarqube and decompress it, java8 is required, and a web server is required:
    copy the jar package from the mvn package of the above project to sonarqube-6.5\extensions\plugins and
    run sonarqube-6.5\bin\windows-x86-64\StartSonar. bat

  2. admin/admin login, a string similar to token will be generated

  3. Enter Quality Profiles, create a new Quality Profile and save it. Enter the newly created Quality Profiles,
    click ActiveMore on the left, enter rules, find the rule you just added, there will be an active button on the right side of the list, and you can activate it.
    (The following is the official Usage)

     
    1. Usage
    2. In the quality profile, activate some rules from PMD and run an analysis on your project. Set the sonar.java.source property to tell PMD which version of Java your source code complies to. The default value is 1.5. Possible values: 1.4, 1.5 or 5, 1.6 or 6, 1.7 or 7. Since version 2.2 of the plugin, this property can also be set to 1.8 or 8.
  4. Analysis
    Enter the project folder to start analysis: Dsonar.login is the token above

     
    1. D:\GreenPro\apache-maven-3.3.9\bin\
    2. mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=8f59dbbe29d160a9727038333d33acf31e11caea
  5. After switching Profiles
    for the first time, there will be a new project in the projects list. At this time, you can go to the project details, Administratoration->Quality Profiles, switch Profile

  6. Then run and analyze.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324897078&siteId=291194637