拓展阅读
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
- wrapper | --> Wrapper Started as Console
- wrapper | Launching a JVM...
- jvm 1 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
- jvm 1 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
- jvm 1 |
- 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
- jvm 1 |
- jvm 1 | WrapperSimpleApp Usage:
- jvm 1 | java org.tanukisoftware.wrapper.WrapperSimpleApp {app_class} [app_arguments]
- jvm 1 |
- jvm 1 | Where:
- jvm 1 | app_class: The fully qualified class name of the application to run.
- jvm 1 | app_arguments: The arguments that would normally be passed to the
- jvm 1 | application.
- wrapper | <-- Wrapper Stopped
复制代码 访问
浏览器直接访问 localhost:9000
默认的账户暗码为 admin/admin
关闭
在命令行中使用 Ctrl+c。
测试
根据提示,生成 admin 对应的 token。
勾选 java=》maven 项目
生成体验命令如下:- # Path to JVM executable. By default it must be available in PATH.
- # Can be an absolute path, for example:
- wrapper.java.command=D:\Program Files\Java\jdk1.8.0_102\bin\java.exe
- #wrapper.java.command=java
复制代码 任意找个 java maven 项目,测试体验:- ${BASE_DIR}/extensions/jdbc-driver/mysql
复制代码 根据提示,访问http://localhost:9000/dashboard/index/com.ryo:netty 即可看到对应的 QA 结果。
在Mac上安装
- # Comment the following lines to deactivate the default embedded database.
- #sonar.jdbc.url: jdbc:derby://localhost:1527/sonar;create=true
- #sonar.jdbc.driverClassName: org.apache.derby.jdbc.ClientDriver
- #sonar.jdbc.validationQuery: values(1)
- ~~~~~~~~~~~~~~~...~~~~~~~~~~~~~~~~~~
- #----- MySQL 5.x/6.x
- # Comment the embedded database and uncomment the following
- #properties to use MySQL. The validation query is optional.
- #sonar.jdbc.validationQuery: select 1
- sonar.jdbc.driverClassName: com.mysql.jdbc.Driver
- sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
- sonar.jdbc.username=root
- sonar.jdbc.password=123456
复制代码 SonarQube
- wrapper | --> Wrapper Started as Console
- wrapper | Launching a JVM...
- jvm 1 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
- jvm 1 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
- jvm 1 |
- 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
- 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
- jvm 1 | 2016.10.14 11:51:05 INFO app[][o.s.p.m.Monitor] Process[es] is up
- 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
- jvm 1 | 2016.10.14 11:51:32 INFO app[][o.s.p.m.Monitor] Process[web] is up
- 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
- jvm 1 | 2016.10.14 11:51:38 INFO app[][o.s.p.m.Monitor] Process[ce] is up
复制代码 配置MySQL
- mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=d1e98b04bdd3efa9fcd139f24fc7162aba80983a
复制代码- D:\CODE\_OTHER\netty>mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=1c6e94d365e83d003ccc519e1d341beb922e2d9f
- [INFO] Scanning for projects...
- [INFO] ------------------------------------------------------------------------
- ......
- [INFO] ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/com.ryo:netty
- [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://localhost:9000/api/ce/task?id=AWEs50bzKz5jZpakiUaE
- [INFO] Task total time: 12.419 s
- [INFO] ------------------------------------------------------------------------
- [INFO] Reactor Summary:
- [INFO]
- [INFO] netty .............................................. SUCCESS [ 17.062 s]
- [INFO] netty-guide ........................................ SKIPPED
- [INFO] ------------------------------------------------------------------------
- [INFO] BUILD SUCCESS
- [INFO] ------------------------------------------------------------------------
- [INFO] Total time: 17.419 s
- [INFO] Finished at: 2018-01-25T18:40:11+08:00
- [INFO] Final Memory: 30M/509M
- [INFO] ------------------------------------------------------------------------
复制代码
- 将mysql-connector-java-5.1.38.jar复制到:
- houbinbindeMacBook-Pro:shell houbinbin$ java -version
- java version "1.8.0_91"
- Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
- Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
复制代码
- 编辑~/conf/sonar.properties
- houbinbindeMacBook-Pro:macosx-universal-64 houbinbin$ pwd
- /Users/houbinbin/it/tools/sonar/sonarqube-6.1/bin/macosx-universal-64
- houbinbindeMacBook-Pro:macosx-universal-64 houbinbin$ ls
- SonarQube.pid lib sonar.sh wrapper
- houbinbindeMacBook-Pro:macosx-universal-64 houbinbin$ ./sonar.sh
- Usage: ./sonar.sh { console | start | stop | restart | status | dump }
- houbinbindeMacBook-Pro:macosx-universal-64 houbinbin$ ./sonar.sh start
- Starting SonarQube...
- Started SonarQube.
复制代码- CREATE USER sonar IDENTIFIED BY 'sonar';
- 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 中
- CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
复制代码- /Users/houbinbin/it/tools/sonar/sonarqube-6.1/extensions/jdbc-driver/mysql
复制代码 备注:清除对 JavaScript 的校验,指定属性值。- # Comment the following lines to deactivate the default embedded database.
- #sonar.jdbc.url: jdbc:derby://localhost:1527/sonar;create=true
- #sonar.jdbc.driverClassName: org.apache.derby.jdbc.ClientDriver
- #sonar.jdbc.validationQuery: values(1)
- ~~~~~~~~~~~~~~~...~~~~~~~~~~~~~~~~~~
- #----- MySQL 5.x/6.x
- # Comment the embedded database and uncomment the following
- #properties to use MySQL. The validation query is optional.
- #sonar.jdbc.validationQuery: select 1
- sonar.jdbc.driverClassName: com.mysql.jdbc.Driver
- sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
- sonar.jdbc.username=sonar
- sonar.jdbc.password=sonar
复制代码- houbinbindeMacBook-Pro:macosx-universal-64 houbinbin$ ./sonar.sh restart
- Stopping SonarQube...
- Waiting for SonarQube to exit...
- Stopped SonarQube.
- Starting SonarQube...
- Started SonarQube.
复制代码 结果:- <settings>
- <pluginGroups>
- <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
- </pluginGroups>
- <profiles>
- <profile>
- <id>sonar</id>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- <properties>
-
- <sonar.host.url>
- http://localhost:9000
- </sonar.host.url>
- </properties>
- </profile>
- </profiles>
- </settings>
复制代码 可以看到警告日记- <build>
- <pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.sonarsource.scanner.maven</groupId>
- <artifactId>sonar-maven-plugin</artifactId>
- <version>3.1.1</version>
- </plugin>
- </plugins>
- </pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.sonarsource.scanner.maven</groupId>
- <artifactId>sonar-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
复制代码 我们可以知道在这些 sonar.properties 中不须要设置。
访问 http://localhost:9000/dashboard/index/com.ryo:git-demo, 您可以看到
缩小核心
- 使用 //NOSAONAR 来忽略一行
- 使用这个来忽略所有 js 文件。
- # Comment the following lines to deactivate the default embedded database.
- #sonar.jdbc.url: jdbc:derby://localhost:1527/sonar;create=true
- #sonar.jdbc.driverClassName: org.apache.derby.jdbc.ClientDriver
- #sonar.jdbc.validationQuery: values(1)
- ~~~~~~~~~~~~~~~...~~~~~~~~~~~~~~~~~~
- #----- MySQL 5.x/6.x
- # Comment the embedded database and uncomment the following
- #properties to use MySQL. The validation query is optional.
- #sonar.jdbc.validationQuery: select 1
- sonar.jdbc.driverClassName: com.mysql.jdbc.Driver
- sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
- sonar.jdbc.username=sonar
- sonar.jdbc.password=sonar
复制代码 常见异常
配置修改如下:- houbinbindeMacBook-Pro:git-demo houbinbin$ mvn sonar:sonar
- [INFO] Scanning for projects...
- [INFO]
- [INFO] ------------------------------------------------------------------------
- [INFO] Building git-demo 1.0-SNAPSHOT
- [INFO] ------------------------------------------------------------------------
- [INFO]
- [INFO] --- sonar-maven-plugin:3.1.1:sonar (default-cli) @ git-demo ---
- [INFO] User cache: /Users/houbinbin/.sonar/cache
- [INFO] Load global repositories
- [INFO] Load global repositories (done) | time=165ms
- [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.
- [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.
- [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.
- [INFO] User cache: /Users/houbinbin/.sonar/cache
- [INFO] Load plugins index
- [INFO] Load plugins index (done) | time=5ms
- [INFO] SonarQube version: 6.1
- [INFO] Default locale: "zh_CN", source code encoding: "UTF-8" (analysis is platform dependent)
- [INFO] Process project properties
- [INFO] Load project repositories
- [INFO] Load project repositories (done) | time=135ms
- [INFO] Load quality profiles
- [INFO] Load quality profiles (done) | time=92ms
- [INFO] Load active rules
- [INFO] Load active rules (done) | time=345ms
- [INFO] Publish mode
- [INFO] ------------- Scan git-demo
- [INFO] Load server rules
- [INFO] Load server rules (done) | time=59ms
- [INFO] Base dir: /Users/houbinbin/IT/code/git-demo
- [INFO] Working dir: /Users/houbinbin/IT/code/git-demo/target/sonar
- [INFO] Source paths: pom.xml
- [INFO] Source encoding: UTF-8, default locale: zh_CN
- [INFO] Index files
- [INFO] 0 files indexed
- [INFO] Sensor Lines Sensor
- [INFO] Sensor Lines Sensor (done) | time=0ms
- [INFO] Sensor SCM Sensor
- [INFO] Sensor SCM Sensor (done) | time=1ms
- [INFO] Sensor XmlFileSensor
- [INFO] Sensor XmlFileSensor (done) | time=0ms
- [INFO] Sensor Zero Coverage Sensor
- [INFO] Sensor Zero Coverage Sensor (done) | time=0ms
- [INFO] Sensor Code Colorizer Sensor
- [INFO] Sensor Code Colorizer Sensor (done) | time=0ms
- [INFO] Sensor CPD Block Indexer
- [INFO] Sensor CPD Block Indexer (done) | time=0ms
- [INFO] Calculating CPD for 0 files
- [INFO] CPD calculation finished
- [INFO] Analysis report generated in 47ms, dir size=12 KB
- [INFO] Analysis reports compressed in 6ms, zip size=4 KB
- [INFO] Analysis report uploaded in 42ms
- [INFO] ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/com.ryo:git-demo
- [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://localhost:9000/api/ce/task?id=AVfDe6043OTpXts6eJaH
- [INFO] Task total time: 1.687 s
- [INFO] ------------------------------------------------------------------------
- [INFO] BUILD SUCCESS
- [INFO] ------------------------------------------------------------------------
- [INFO] Total time: 3.059 s
- [INFO] Finished at: 2016-10-14T21:57:25+08:00
- [INFO] Final Memory: 18M/398M
- [INFO] ------------------------------------------------------------------------
复制代码 运行:- [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.
- [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.
- [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 中可以安裝 SonarLint 和 Alibaba 來提拔代碼質量。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |