Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add Ali java Development Specification to sonarqube

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to add Ali java development specification to sonarqube, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Pull the sonar-pmd source code and switch to 3.2.1. Modify the pom file to add p3c-pmd dependencies:

Com.alibaba.p3c p3c-pmd 2.0.0

To start configuring the Ali Java specification, the configuration rule is demonstrated here: the total number of lines of a single method does not exceed 80 lines, and the name of the rule is MethodTooLongRule, which comes from the rule name configured in the P3C project P3C\ p3c-pmd\ src\ main\ resources\ rulesets\ java\ ali-other.xml file.

First, create a new pmd-p3c directory to store the rule description html under the sonar-pmd project sonar-pmd-plugin\ src\ main\ resources\ org\ sonar\ l10n\ pmd\ rules directory, then create a new MethodTooLongRule.html, and add the following:

The total number of rows of a single method does not exceed 80 rows.

Description: the total number of lines including method signature, closing curly braces, code within the method, comments, blank lines, carriage returns and any invisible characters does not exceed 80 lines.

Modify the sonar-pmd project sonar-pmd-plugin\ src\ main\ resources\ org\ sonar\ l10n\ pmd.properties configuration file, and add the MethodTooLongRule rule name as follows:

Rule.pmd.MethodTooLongRule.name= [p3c] The total number of lines for a method should not be more than 80.

In the sonar-pmd project sonar-pmd-plugin\ src\ main\ resources\ org\ sonar\ plugins\ pmd directory, create a new rules-p3c.xml rule file and add the MethodTooLongRule rule configuration as follows:

MAJOR CODE_SMELL other

Modify the sonar-pmd project sonar-pmd-plugin\ src\ main\ resources\ com\ sonar\ sqale\ pmd-model.xml configuration file and add the following code before the tag at the end of the file:

P3C-PMD p3c-pmd P3C_PMD p3c_pmd pmd MethodTooLongRule remediationFunction CONSTANT_ISSUE offset 2 min

Add the introduction of P3C rules, modify the PmdRulesDefinition.java file, and add the P3C rule base to the define method

@ Override public void define (Context context) {NewRepository repository = context .createRepository (PmdConstants.REPOSITORY_KEY, PmdConstants.LANGUAGE_KEY) .setName (PmdConstants.REPOSITORY_NAME); extractRulesData (repository, "/ org/sonar/plugins/pmd/rules.xml", "/ org/sonar/l10n/pmd/rules/pmd") / / add extractRulesData (repository, "/ org/sonar/plugins/pmd/rules-p3c.xml", "/ org/sonar/l10n/pmd/rules/pmd-p3c"); repository.done ();}

Package it with mvn package-Dmaven.test.skip=true. If the description file html of the rule contains Chinese content, then note that the file encoding needs to be utf-8.

Restart the service by placing the sonar-pmd-plugin-3.2.1.jar in the sonar-pmd-plugin\ target directory under the sonarqube service / extensions/plugins directory.

Log in to Sonarqube on the quality configuration page, click the "create" button to create a new P3C quality, select Java, click "create" and jump to the details interface of the quality configuration, then click "more Activation rules", search for P3C in the search rules, click "batch modify", click "apply" to the newly created quality configuration, and then go back to the "quality configuration" page and set the quality configuration Set as Default [as default]. Then you can analyze the quality of the project.

Question:

1. If you fail to analyze the missing class com/google/gson/Gson, add the following dependencies in the sonar-pmd project pom file:

Com.google.code.gson gson 2.8.5

Other:

1. The rule states that the html content comes from the msg and desc content configured in the message.xml file under P3C\ p3c-pmd\ src\ main\ resources. Add the sample code using tags. If the code is configured with generics or the code is configured for xml, use tags. Examples are as follows:

Negative example: List list = new ArrayList (); list.add ("22"); / / warn List test = (ArrayList) list.subList (0,1)

two。 The name of the rule comes from the msg content of the rule configured by message_en.xml under P3C\ p3c-pmd\ src\ main\ resources. However, some msg is too long and sonar restart fails. Please delete it as appropriate.

3. Rules-p3c.xml add rule type type temporarily uses CODE_SMELL and BUG

4. The configKey configuration in the rules-p3c.xml add rule should correspond to the file name of the rule configuration file and the name of the rule in the directory P3C\ p3c-pmd\ src\ main\ resources\ rulesets\ java.

On how to add Ali java development specifications in sonarqube to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report