代码质量管理 SonarQube-01-入门介绍

打印 上一主题 下一主题

主题 884|帖子 884|积分 2652

拓展阅读

Devops-01-devops 是什么?
Devops-02-Jpom 简而轻的低侵入式在线构建、主动摆设、日常运维、项目监控软件
代码质量管理 SonarQube-01-入门介绍
项目管理平台-01-jira 入门介绍 缺陷跟踪管理系统,为针对缺陷管理、任务追踪和项目管理的商业性应用软件
项目管理平台-01-Phabricator 入门介绍 一套集成的强盛工具,帮助公司构建更高质量的软件
持续集成平台 01 jenkins 入门介绍
持续集成平台 02 jenkins plugin 插件
test coverate-05-测试覆盖率 SonarQube 是一个综合性的代码质量管理平台,其中包罗了对测试覆盖率的支持
Docker learn-29-docker 安装 sonarQube with mysql
是什么

SonarQube 是一个开放平台,用于管理代码质量。
Sonar 中文资料
在 Windows 安装 SonarQube

要求

确保已经安装了 JDK 和 MySQL。
下载

当前版本为 6.7.1
运行

假定 ${BASE_DIR} 为 D:\Learn\sonar\sonarqube-6.7.1(本地解压路径)

  • 运行 ${BASE_DIR}\bin\windows-x86-64 目录下的 StartSonar.bat
  1. wrapper  | --> Wrapper Started as Console
  2. wrapper  | Launching a JVM...
  3. jvm 1    | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
  4. jvm 1    |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
  5. jvm 1    |
  6. jvm 1    | WrapperSimpleApp: Unable to locate the class org.sonar.application.App: java.lang.UnsupportedClassVersionError: org/sonar/application/App : Unsupported major.minor version 52.0
  7. jvm 1    |
  8. jvm 1    | WrapperSimpleApp Usage:
  9. jvm 1    |   java org.tanukisoftware.wrapper.WrapperSimpleApp {app_class} [app_arguments]
  10. jvm 1    |
  11. jvm 1    | Where:
  12. jvm 1    |   app_class:      The fully qualified class name of the application to run.
  13. jvm 1    |   app_arguments:  The arguments that would normally be passed to the
  14. jvm 1    |                   application.
  15. wrapper  | <-- Wrapper Stopped
复制代码
访问

浏览器直接访问 localhost:9000
默认的账户暗码为 admin/admin
关闭

在命令行中使用 Ctrl+c。
测试


  • 生成体验命令
根据提示,生成 admin 对应的 token。
勾选 java=》maven 项目
生成体验命令如下:
  1. # Path to JVM executable. By default it must be available in PATH.
  2. # Can be an absolute path, for example:
  3. wrapper.java.command=D:\Program Files\Java\jdk1.8.0_102\bin\java.exe
  4. #wrapper.java.command=java
复制代码

  • 项目测试
任意找个 java maven 项目,测试体验:
  1. ${BASE_DIR}/extensions/jdbc-driver/mysql
复制代码
根据提示,访问http://localhost:9000/dashboard/index/com.ryo:netty 即可看到对应的 QA 结果。
在Mac上安装


  • JDK版本
  1. # Comment the following lines to deactivate the default embedded database.
  2. #sonar.jdbc.url: jdbc:derby://localhost:1527/sonar;create=true
  3. #sonar.jdbc.driverClassName: org.apache.derby.jdbc.ClientDriver
  4. #sonar.jdbc.validationQuery: values(1)
  5. ~~~~~~~~~~~~~~~...~~~~~~~~~~~~~~~~~~
  6. #----- MySQL 5.x/6.x
  7. # Comment the embedded database and uncomment the following
  8. #properties to use MySQL. The validation query is optional.
  9. #sonar.jdbc.validationQuery: select 1
  10. sonar.jdbc.driverClassName: com.mysql.jdbc.Driver
  11. sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
  12. sonar.jdbc.username=root
  13. sonar.jdbc.password=123456
复制代码
SonarQube

  1. wrapper  | --> Wrapper Started as Console
  2. wrapper  | Launching a JVM...
  3. jvm 1    | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
  4. jvm 1    |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
  5. jvm 1    |
  6. jvm 1    | 2016.10.14 11:50:58 INFO  app[][o.s.a.AppFileSystem] Cleaning or creating temp directory D:\Tools\sonar\sonarqube-6.1\temp
  7. jvm 1    | 2016.10.14 11:50:58 INFO  app[][o.s.p.m.JavaProcessLauncher] Launch process[es]: D:\Program Files\Java\jdk1.8.0_102\jre\bin\java -Djava.awt.headless=true -Xmx1G -Xms256m -Xss256k -Djna.nosys=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -Djava.io.tmpdir=D:\Tools\sonar\sonarqube-6.1\temp -javaagent:D:\Program Files\Java\jdk1.8.0_102\jre\lib\management-agent.jar -cp ./lib/common/*;./lib/search/* org.sonar.search.SearchServer D:\Tools\sonar\sonarqube-6.1\temp\sq-process635522788444000175properties
  8. jvm 1    | 2016.10.14 11:51:05 INFO  app[][o.s.p.m.Monitor] Process[es] is up
  9. jvm 1    | 2016.10.14 11:51:05 INFO  app[][o.s.p.m.JavaProcessLauncher] Launch process[web]: D:\Program Files\Java\jdk1.8.0_102\jre\bin\java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djruby.management.enabled=false -Djruby.compile.invokedynamic=false -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -Djava.io.tmpdir=D:\Tools\sonar\sonarqube-6.1\temp -javaagent:D:\Program Files\Java\jdk1.8.0_102\jre\lib\management-agent.jar -cp ./lib/common/*;./lib/server/*;D:\Tools\sonar\sonarqube-6.1\lib\jdbc\h2\h2-1.3.176.jar org.sonar.server.app.WebServer D:\Tools\sonar\sonarqube-6.1\temp\sq-process6946582725671729886properties
  10. jvm 1    | 2016.10.14 11:51:32 INFO  app[][o.s.p.m.Monitor] Process[web] is up
  11. jvm 1    | 2016.10.14 11:51:32 INFO  app[][o.s.p.m.JavaProcessLauncher] Launch process[ce]: D:\Program Files\Java\jdk1.8.0_102\jre\bin\java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -Djava.io.tmpdir=D:\Tools\sonar\sonarqube-6.1\temp -javaagent:D:\Program Files\Java\jdk1.8.0_102\jre\lib\management-agent.jar -cp ./lib/common/*;./lib/server/*;./lib/ce/*;D:\Tools\sonar\sonarqube-6.1\lib\jdbc\h2\h2-1.3.176.jar org.sonar.ce.app.CeServer D:\Tools\sonar\sonarqube-6.1\temp\sq-process7599430162435853222properties
  12. jvm 1    | 2016.10.14 11:51:38 INFO  app[][o.s.p.m.Monitor] Process[ce] is up
复制代码
配置MySQL


  • 在MySQL中创建用户sonar
  1. mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=d1e98b04bdd3efa9fcd139f24fc7162aba80983a
复制代码

  • 创建数据库sonar
  1. D:\CODE\_OTHER\netty>mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=1c6e94d365e83d003ccc519e1d341beb922e2d9f
  2. [INFO] Scanning for projects...
  3. [INFO] ------------------------------------------------------------------------
  4. ......
  5. [INFO] ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/com.ryo:netty
  6. [INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
  7. [INFO] More about the report processing at http://localhost:9000/api/ce/task?id=AWEs50bzKz5jZpakiUaE
  8. [INFO] Task total time: 12.419 s
  9. [INFO] ------------------------------------------------------------------------
  10. [INFO] Reactor Summary:
  11. [INFO]
  12. [INFO] netty .............................................. SUCCESS [ 17.062 s]
  13. [INFO] netty-guide ........................................ SKIPPED
  14. [INFO] ------------------------------------------------------------------------
  15. [INFO] BUILD SUCCESS
  16. [INFO] ------------------------------------------------------------------------
  17. [INFO] Total time: 17.419 s
  18. [INFO] Finished at: 2018-01-25T18:40:11+08:00
  19. [INFO] Final Memory: 30M/509M
  20. [INFO] ------------------------------------------------------------------------
复制代码

  • 将mysql-connector-java-5.1.38.jar复制到:
  1. houbinbindeMacBook-Pro:shell houbinbin$ java -version
  2. java version "1.8.0_91"
  3. Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
  4. Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
复制代码

  • 编辑~/conf/sonar.properties
  1. houbinbindeMacBook-Pro:macosx-universal-64 houbinbin$ pwd
  2. /Users/houbinbin/it/tools/sonar/sonarqube-6.1/bin/macosx-universal-64
  3. houbinbindeMacBook-Pro:macosx-universal-64 houbinbin$ ls
  4. SonarQube.pid        lib                sonar.sh        wrapper
  5. houbinbindeMacBook-Pro:macosx-universal-64 houbinbin$ ./sonar.sh
  6. Usage: ./sonar.sh { console | start | stop | restart | status | dump }
  7. houbinbindeMacBook-Pro:macosx-universal-64 houbinbin$ ./sonar.sh start
  8. Starting SonarQube...
  9. Started SonarQube.
复制代码

  • 重新启动
  1. CREATE USER sonar IDENTIFIED BY 'sonar';
  2. GRANT ALL PRIVILEGES ON *.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar' WITH GRANT OPTION;
复制代码
将Sonar设置为中文

  • 管理员登录,搜刮Chinese Pack,并在Update Center中安装并重启。
  • 下载http://repository.codehaus.org/org/codehaus/sonar-plugins/l10n/sonar-l10n-zh-plugin/1.6/sonar-l10n-zh-plugin-1.6.jar,放入$SONAR_HOME/extensions/plugins目录,并重新启动。
使用 Maven 配合 Sonar

使用 Maven 举行 Sonar 分析


  • 添加到 Maven 的 settings.xml 中
  1. CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
复制代码

  • 添加到项目的 pom.xml 中
  1. /Users/houbinbin/it/tools/sonar/sonarqube-6.1/extensions/jdbc-driver/mysql
复制代码
备注:清除对 JavaScript 的校验,指定属性值。
  1. # Comment the following lines to deactivate the default embedded database.
  2. #sonar.jdbc.url: jdbc:derby://localhost:1527/sonar;create=true
  3. #sonar.jdbc.driverClassName: org.apache.derby.jdbc.ClientDriver
  4. #sonar.jdbc.validationQuery: values(1)
  5. ~~~~~~~~~~~~~~~...~~~~~~~~~~~~~~~~~~
  6. #----- MySQL 5.x/6.x
  7. # Comment the embedded database and uncomment the following
  8. #properties to use MySQL. The validation query is optional.
  9. #sonar.jdbc.validationQuery: select 1
  10. sonar.jdbc.driverClassName: com.mysql.jdbc.Driver
  11. sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
  12. sonar.jdbc.username=sonar
  13. sonar.jdbc.password=sonar
复制代码

  • 运行
  1. houbinbindeMacBook-Pro:macosx-universal-64 houbinbin$ ./sonar.sh restart
  2. Stopping SonarQube...
  3. Waiting for SonarQube to exit...
  4. Stopped SonarQube.
  5. Starting SonarQube...
  6. Started SonarQube.
复制代码
结果:
  1. <settings>
  2.     <pluginGroups>
  3.         <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
  4.     </pluginGroups>
  5.     <profiles>
  6.         <profile>
  7.             <id>sonar</id>
  8.             <activation>
  9.                 <activeByDefault>true</activeByDefault>
  10.             </activation>
  11.             <properties>
  12.                
  13.                 <sonar.host.url>
  14.                   http://localhost:9000
  15.                 </sonar.host.url>
  16.             </properties>
  17.         </profile>
  18.      </profiles>
  19. </settings>
复制代码
可以看到警告日记
  1. <build>
  2.     <pluginManagement>
  3.         <plugins>
  4.             <plugin>
  5.                 <groupId>org.sonarsource.scanner.maven</groupId>
  6.                 <artifactId>sonar-maven-plugin</artifactId>
  7.                 <version>3.1.1</version>
  8.             </plugin>
  9.         </plugins>
  10.     </pluginManagement>
  11.     <plugins>
  12.         <plugin>
  13.             <groupId>org.sonarsource.scanner.maven</groupId>
  14.             <artifactId>sonar-maven-plugin</artifactId>
  15.         </plugin>
  16.     </plugins>
  17. </build>
复制代码
我们可以知道在这些 sonar.properties 中不须要设置。
访问 http://localhost:9000/dashboard/index/com.ryo:git-demo, 您可以看到

缩小核心


  • 使用 //NOSAONAR 来忽略一行
  • 使用这个来忽略所有 js 文件。
  1.         # Comment the following lines to deactivate the default embedded database.
  2. #sonar.jdbc.url: jdbc:derby://localhost:1527/sonar;create=true
  3. #sonar.jdbc.driverClassName: org.apache.derby.jdbc.ClientDriver
  4. #sonar.jdbc.validationQuery: values(1)
  5. ~~~~~~~~~~~~~~~...~~~~~~~~~~~~~~~~~~
  6. #----- MySQL 5.x/6.x
  7. # Comment the embedded database and uncomment the following
  8. #properties to use MySQL. The validation query is optional.
  9. #sonar.jdbc.validationQuery: select 1
  10. sonar.jdbc.driverClassName: com.mysql.jdbc.Driver
  11. sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
  12. sonar.jdbc.username=sonar
  13. sonar.jdbc.password=sonar
复制代码
常见异常


  • 没有找到编译后的信息,报错如下:
  1. mvn clean sonar:sonar
复制代码
配置修改如下:
  1. houbinbindeMacBook-Pro:git-demo houbinbin$ mvn sonar:sonar
  2. [INFO] Scanning for projects...
  3. [INFO]
  4. [INFO] ------------------------------------------------------------------------
  5. [INFO] Building git-demo 1.0-SNAPSHOT
  6. [INFO] ------------------------------------------------------------------------
  7. [INFO]
  8. [INFO] --- sonar-maven-plugin:3.1.1:sonar (default-cli) @ git-demo ---
  9. [INFO] User cache: /Users/houbinbin/.sonar/cache
  10. [INFO] Load global repositories
  11. [INFO] Load global repositories (done) | time=165ms
  12. [WARNING] Property 'sonar.jdbc.url' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
  13. [WARNING] Property 'sonar.jdbc.username' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
  14. [WARNING] Property 'sonar.jdbc.password' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
  15. [INFO] User cache: /Users/houbinbin/.sonar/cache
  16. [INFO] Load plugins index
  17. [INFO] Load plugins index (done) | time=5ms
  18. [INFO] SonarQube version: 6.1
  19. [INFO] Default locale: "zh_CN", source code encoding: "UTF-8" (analysis is platform dependent)
  20. [INFO] Process project properties
  21. [INFO] Load project repositories
  22. [INFO] Load project repositories (done) | time=135ms
  23. [INFO] Load quality profiles
  24. [INFO] Load quality profiles (done) | time=92ms
  25. [INFO] Load active rules
  26. [INFO] Load active rules (done) | time=345ms
  27. [INFO] Publish mode
  28. [INFO] -------------  Scan git-demo
  29. [INFO] Load server rules
  30. [INFO] Load server rules (done) | time=59ms
  31. [INFO] Base dir: /Users/houbinbin/IT/code/git-demo
  32. [INFO] Working dir: /Users/houbinbin/IT/code/git-demo/target/sonar
  33. [INFO] Source paths: pom.xml
  34. [INFO] Source encoding: UTF-8, default locale: zh_CN
  35. [INFO] Index files
  36. [INFO] 0 files indexed
  37. [INFO] Sensor Lines Sensor
  38. [INFO] Sensor Lines Sensor (done) | time=0ms
  39. [INFO] Sensor SCM Sensor
  40. [INFO] Sensor SCM Sensor (done) | time=1ms
  41. [INFO] Sensor XmlFileSensor
  42. [INFO] Sensor XmlFileSensor (done) | time=0ms
  43. [INFO] Sensor Zero Coverage Sensor
  44. [INFO] Sensor Zero Coverage Sensor (done) | time=0ms
  45. [INFO] Sensor Code Colorizer Sensor
  46. [INFO] Sensor Code Colorizer Sensor (done) | time=0ms
  47. [INFO] Sensor CPD Block Indexer
  48. [INFO] Sensor CPD Block Indexer (done) | time=0ms
  49. [INFO] Calculating CPD for 0 files
  50. [INFO] CPD calculation finished
  51. [INFO] Analysis report generated in 47ms, dir size=12 KB
  52. [INFO] Analysis reports compressed in 6ms, zip size=4 KB
  53. [INFO] Analysis report uploaded in 42ms
  54. [INFO] ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/com.ryo:git-demo
  55. [INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
  56. [INFO] More about the report processing at http://localhost:9000/api/ce/task?id=AVfDe6043OTpXts6eJaH
  57. [INFO] Task total time: 1.687 s
  58. [INFO] ------------------------------------------------------------------------
  59. [INFO] BUILD SUCCESS
  60. [INFO] ------------------------------------------------------------------------
  61. [INFO] Total time: 3.059 s
  62. [INFO] Finished at: 2016-10-14T21:57:25+08:00
  63. [INFO] Final Memory: 18M/398M
  64. [INFO] ------------------------------------------------------------------------
复制代码
运行:
  1. [WARNING] Property 'sonar.jdbc.url' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
  2. [WARNING] Property 'sonar.jdbc.username' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
  3. [WARNING] Property 'sonar.jdbc.password' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
复制代码
QA 工具

idea 中可以安裝 SonarLintAlibaba 來提拔代碼質量。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

水军大提督

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表