Sonar代码质量检测工具

Sonar(代码质量管理平台)是一个开源平台,用于管理Java源代码的质量。从 Sonar 1.6 版本开始,Sonar从一个质量数据报告工具,转变成为的代码质量管理平台。

主要特点

  • 代码覆盖:通过单元测试,将会显示哪行代码被选中
  • 改善编码规则
  • 搜寻编码规则:按照名字,插件,激活级别和类别进行查询
  • 项目搜寻:按照项目的名字进行查询
  • 对比数据:比较同一张表中的任何测量的趋势
1.在settings.xml文件中加入
<profile>
    <id>sonar</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <sonar.host.url>http://192.168.100.45:9000/sonar</sonar.host.url>
        <sonar.login>admin</sonar.login>
        <sonar.password>admin</sonar.password>
    </properties>
</profile>
2 项目根目录 sonar-project.properties 新建一个(直接从下载的文件里面拷贝)

#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here

#----- Default SonarQube server
sonar.host.url=http://192.168.100.45:9000/sonar

#----- Default source code encoding
#sonar.sourceEncoding=UTF-8

#----- Global database settings (not used for SonarQube 5.2+)
#sonar.jdbc.username=sonar
#sonar.jdbc.password=sonar

#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

#----- MySQL
#sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8

#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE

#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
sonar.host.url=http://192.168.100.45:9000/sonar 这个url一定要改成自己的


4 执行sonar-scanner-2.8/bin/sonar-scanner即可把项目扫描到sonar上

[INFO] User cache: C:\Users\PGmmr\.sonar\cache
[INFO] Load global repositories
[INFO] Load global repositories (done) | time=237ms
[INFO] User cache: C:\Users\PGmmr\.sonar\cache
[INFO] Load plugins index
[INFO] Load plugins index (done) | time=53ms
[INFO] SonarQube version: 6.2
[INFO] Default locale: "zh_CN", source code encoding: "UTF-8"
[INFO] Process project properties
[INFO] Load project repositories
[INFO] Load project repositories (done) | time=20ms
[INFO] Load quality profiles
[INFO] Load quality profiles (done) | time=141ms
[INFO] Load active rules
[INFO] Load active rules (done) | time=673ms
[WARNING] SCM provider autodetection failed. No SCM provider claims to support this project. Please use sonar.scm.provider to define SCM of your project.
[INFO] Publish mode
[INFO] -------------  Scan nexus3
[INFO] Load server rules
[INFO] Load server rules (done) | time=410ms
[INFO] Initializer GenericCoverageSensor
[INFO] Initializer GenericCoverageSensor (done) | time=0ms
[INFO] Base dir: C:\Users\PGmmr\IdeaProjects\nexus3
[INFO] Working dir: C:\Users\PGmmr\IdeaProjects\nexus3\target\sonar
[INFO] Source paths: pom.xml, src/main/java
[INFO] Test paths: src/test/java
[INFO] Source encoding: UTF-8, default locale: zh_CN
[INFO] Index files
[INFO] 2 files indexed
[INFO] Quality profile for java: Sonar way
[INFO] Sensor Lines Sensor
[INFO] Sensor Lines Sensor (done) | time=7ms
[INFO] Sensor JavaSquidSensor
[INFO] Configured Java source version (sonar.java.source): 8
[INFO] JavaClasspath initialization
[INFO] JavaClasspath initialization (done) | time=13ms
[INFO] JavaTestClasspath initialization
[INFO] JavaTestClasspath initialization (done) | time=2ms
[INFO] Java Main Files AST scan
[INFO] 1 source files to be analyzed
[WARNING] Metric 'lines' is an internal metric computed by SonarQube. Provided value is ignored.
[INFO] Java Main Files AST scan (done) | time=349ms
[INFO] 1/1 source files have been analyzed
[INFO] Java Test Files AST scan
[INFO] 1 source files to be analyzed
[INFO] Java Test Files AST scan (done) | time=35ms
[INFO] 1/1 source files have been analyzed
[INFO] Sensor JavaSquidSensor (done) | time=848ms
[INFO] Sensor SCM Sensor
[INFO] No SCM system was detected. You can use the 'sonar.scm.provider' property to explicitly specify it.
[INFO] Sensor SCM Sensor (done) | time=1ms
[INFO] Sensor Coverage Report Import
[INFO] Sensor Coverage Report Import (done) | time=0ms
[INFO] Sensor Coverage Report Import
[INFO] Sensor Coverage Report Import (done) | time=0ms
[INFO] Sensor Unit Test Results Import
[INFO] Sensor Unit Test Results Import (done) | time=0ms
[INFO] Sensor SurefireSensor
[INFO] parsing C:\Users\PGmmr\IdeaProjects\nexus3\target\surefire-reports
[INFO] Sensor SurefireSensor (done) | time=80ms
[INFO] Sensor JaCoCoSensor
[INFO] JaCoCoSensor: JaCoCo report not found : C:\Users\PGmmr\IdeaProjects\nexus3\target\jacoco.exec
[INFO] Sensor JaCoCoSensor (done) | time=0ms
[INFO] Sensor JaCoCoItSensor
[INFO] JaCoCoItSensor: JaCoCo IT report not found: C:\Users\PGmmr\IdeaProjects\nexus3\target\jacoco-it.exec
[INFO] Sensor JaCoCoItSensor (done) | time=1ms
[INFO] Sensor JaCoCoOverallSensor
[INFO] Sensor JaCoCoOverallSensor (done) | time=0ms
[INFO] Sensor XmlFileSensor
[INFO] Sensor XmlFileSensor (done) | time=0ms
[INFO] Sensor Zero Coverage Sensor
[INFO] Sensor Zero Coverage Sensor (done) | time=8ms
[INFO] Sensor Code Colorizer Sensor
[INFO] Sensor Code Colorizer Sensor (done) | time=0ms
[INFO] Sensor CPD Block Indexer
[INFO] JavaCpdBlockIndexer is used for java
[INFO] Sensor CPD Block Indexer (done) | time=17ms
[INFO] Calculating CPD for 0 files
[INFO] CPD calculation finished
[INFO] Analysis report generated in 101ms, dir size=30 KB
[INFO] Analysis reports compressed in 25ms, zip size=9 KB
[INFO] Analysis report uploaded in 843ms
[INFO] ANALYSIS SUCCESSFUL, you can browse http://192.168.100.45:9000/sonar/dashboard/index/org.shoper.nexus3:nexus3
[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[INFO] More about the report processing at http://192.168.100.45:9000/sonar/api/ce/task?id=AVwPOsnxCT6w49OWTi1K
[INFO] Task total time: 4.597 s
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:27 min
[INFO] Finished at: 2017-05-16T11:08:51+08:00
[INFO] Final Memory: 46M/357M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0


看到以上信息。说明扫描成功!搭建sonar很简单,有兴趣的朋友可以搜搜。


下面再附上IDEA插件
Preferences---->Plusgins----->搜索SonarLint     在IDEA里面使用更方便,上面的方式更适合多个项目管理。









猜你喜欢

转载自blog.csdn.net/u010138906/article/details/78788368