sonar如何添加自定義JAVA規則


參考

1、https://segmentfault.com/a/1190000008659108

2、https://docs.sonarqube.org/display/DEV/Adding+Coding+Rules

3、https://docs.sonarqube.org/display/DEV/Adding+Coding+Rules+using+Java

4、https://docs.sonarqube.org/display/PLUG/Writing+Custom+Java+Rules+101

有兩種方式可以擴展代碼規則:但是Java只支持方式1

1、Writing coding rules using Java via a SonarQube plugin

2、Adding XPath rules directly through the SonarQube web interface

 

Writing coding rules in Java is a six-step process:

  • Create a SonarQube plugin.
  • Put a dependency on the API of the language plugin for which you are writing coding rules.
  • Create as many custom rules as required
  • Generate the SonarQube plugin (jar file)
  • Place this jar file in the SONARQUBE_HOME/extensions/plugins directory
  • Restart SonarQube server

下面詳述這個過程:

1、把項目拉到本地

git clone git@github.com:shengulong/sonar-custom-rules-examples.git

2、里面有cobol/java/javascript/php/rpg的自定義規則,我們只關注java的

3、使用intellij idea打開項目java-custom-rules(github上,我已經調試通過)。這是一個maven項目的模板。

4、修改下pom文件:sonar的版本以及對應的java插件版本,這樣sonar6.0以上版本都可以用這個插件

 1 <properties>
 2         <!--<sonar.version>6.3</sonar.version>-->
 3         <sonar.version>6.0</sonar.version>
 4         <!-- this 6.3 is only required to be compliant with SonarLint and it is required
 5             even if you just want to be compliant with SonarQube 5.6 -->
 6         <!--<java.plugin.version>4.7.1.9272</java.plugin.version>-->
 7         <java.plugin.version>4.5.0.8398</java.plugin.version>
 8         <sslr.version>1.21</sslr.version>
 9         <gson.version>2.6.2</gson.version>
10 </properties>

5、修改下文件,因為最初下載的報編譯錯誤


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM