参考:
一 Sonar 概述
Sonar 是一个用于代码质量管理的开放平台。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具。
与持续集成工具(例如 Hudson/Jenkins 等)不同,Sonar 并不是简单地把不同的代码检查工具结果(例如 FindBugs,PMD 等)直接显示在 Web 页面上,而是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。
在对其他工具的支持方面,Sonar 不仅提供了对 IDE 的支持,可以在 Eclipse 和 IntelliJ IDEA 这些工具里联机查看结果;同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar。
此外,Sonar 的插件还可以对 Java 以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持。
二 Sonar 的安装
Sonar的相关下载和文档可以在下面的链接中找到:。需要注意最新版的Sonar需要至少JDK 1.8
及以上版本。
上篇文章我们已经可以成功的使用进行拉去,Sonar的功能就是来检查代码是否有BUG。除了检查代码是否有bug还有其他的功能,比如说:你的代码注释率是多少,代码有一些建议,编写语法的建议。所以我们叫质量管理
Sonar还可以给代码打分,并且引用了技术宅的功能(告诉你有很多地方没改)
Sonar 是 Codehaus 上面的一个开源项目,使用的是 LGPL V3 软件许可。我们可以在其官方网站上下载其源代码及安装包。
其源代码需要使用分布式版本控制软件 Git 进行检出(Check Out),命令行方式如下:
git clone git://github.com/SonarSource/sonar.git
基础环境安装
[root@linux-node1 ~]# yum install -y java-1.8.0[root@linux-node1 ~]# cd /usr/local/src软件包我们通过wget或者下载,rz上传到服务器#软件包下载:https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.6.zip[root@linux-node1 src]# unzip sonarqube-5.6.zip[root@linux-node1 src]# mv sonarqube-5.6 /usr/local/[root@linux-node1 src]# ln -s /usr/local/sonarqube-5.6/ /usr/local/sonarqube
数据库安装配置
Sonar 默认使用的是 Derby 数据库,但这个数据库一般用于评估版本或者测试用途。商用及对数据库要求较高时,建议使用其他数据库。Sonar 可以支持大多数主流关系型数据库(例如 Microsoft SQL Server, MySQL, Oracle, PostgreSQL 等)
本文以 mariadb 为例说明如何更改 Sonar 的数据库设置:
yum install -y mariadb mariadb-server [root@salt-node2 ~]# systemctl enable mariadb [root@salt-node2 ~]# systemctl start mariadb [root@salt-node2 ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the currentpassword for the root user. If you've just installed MariaDB, andyou haven't set the root password yet, the password will be blank,so you should just press enter here.Enter current password for root (enter for none): ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)Enter current password for root (enter for none): OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation.Set root password? [Y/n] yNew password: Re-enter new password: Password updated successfully!Reloading privilege tables.. ... Success!By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] y ... Success!Normally, root should only be allowed to connect from 'localhost'. Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] y ... Success!By default, MariaDB comes with a database named 'test' that anyone canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [Y/n] y - Dropping test database... ... Success! - Removing privileges on test database... ... Success!Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.Reload privilege tables now? [Y/n] y ... Success!Cleaning up...All done! If you've completed all of the above steps, your MariaDBinstallation should now be secure.Thanks for using MariaDB! [root@salt-node2 ~]# mysql -uroot -pEnter password: Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 18Server version: 5.5.52-MariaDB MariaDB ServerCopyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]>
配置数据库
MariaDB [(none)]> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;Query OK, 1 row affected (0.02 sec)MariaDB [(none)]> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar@pw';Query OK, 0 rows affected (0.01 sec)MariaDB [(none)]> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar@pw';Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)
配置Sonar配置文件链接数据库
[root@salt-node2 ~]# cd /usr/local/sonarqube/conf/ [root@salt-node2 /usr/local/sonarqube/conf]# lssonar.properties wrapper.conf [root@salt-node2 /usr/local/sonarqube/conf]# vim sonar.properties # Property values can:# - reference an environment variable, for example sonar.jdbc.url= ${env:SONAR_JDBC_URL}# - be encrypted. See http://redirect.sonarsource.com/doc/settings-encryption.html#--------------------------------------------------------------------------------------------------# DATABASE## IMPORTANT: the embedded H2 database is used by default. It is recommended for tests but not for# production use. Supported databases are MySQL, Oracle, PostgreSQL and Microsoft SQLServer.# User credentials.# Permissions to create tables, indices and triggers must be granted to JDBC user.# The schema must be created first.sonar.jdbc.username=sonarsonar.jdbc.password=sonar@pw#----- Embedded Database (default)# H2 embedded database server listening port, defaults to 9092#sonar.embeddedDatabase.port=9092#----- MySQL 5.6 or greater# Only InnoDB storage engine is supported (not myISAM).# Only the bundled driver is supported. It can not be changed.#sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformancesonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance 99 #sonar.web.host=0.0.0.0 配置监听地址106 #sonar.web.port=9000 配置监听端口
配置Java访问数据库驱动(可选)
默认情况Sonar有自带的嵌入的数据库,那么你如果使用类是Oracle数据库,必须手动复制驱动类到${SONAR_HOME}/extensions/jdbc-driver/oracle/
目录下,其它支持的数据库默认提供了驱动。其它数据库的配置可以参考官方文档: 启动sonar