说明:sonar依赖数据库.
mysql优化
1、笔者使用的是mysql数据库.首先对mysql做简单的优化配置.
[root@localhost bin]# cat /etc/my.cnf [mysqld] max_allowed_packet=10M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # 修改默认的编码为utf8 default-character-set=utf8 # 修改默认的存储引擎为InnoDB default-storage-engine=InnoDB # 这个参数主要作用是缓存innodb表的索引,数据,插入数据时的缓冲 innodb_buffer_pool_size = 256M # 配置查询缓存的大小 query_cache_size=128M # 启动mysql高速缓存 query_cache_type=1 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
2、重启mysql服务
[root@localhost bin]# service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
sonar安装和部署
1、sonar部署
[root@localhost local]# pwd
/usr/local [root@localhost local]# unzip sonarqube-4.5.4.zip
修改sonar配置文件
[root@localhost conf]# pwd
/usr/local/sonarqube-4.5.4/conf [root@localhost conf]# vim sonar.properties sonar.jdbc.username=root sonar.jdbc.password=123456 sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.web.javaOpts=-Xmx768m -XX:MaxPermSize=160m -XX:+HeapDumpOnOutOfMemoryError sonar.web.host=0.0.0.0 sonar.web.port=9000
创建sonar数据库依赖
create database sonar default character set utf8;
2、利用nginx反响代理
upstream配置
upstream tomcat_tools.sonar.local { server 127.0.0.1:9000 weight=10 max_fails=2 fail_timeout=300s; } server { listen 80; server_name tools.sonar.local.com; root /usr/local/sonarqube-4.5.4/web/; access_log /usr/local/sonarqube-4.5.4/logs/tools.sonar.local.com_access.log main; error_log /usr/local/sonarqube-4.5.4/logs/tools.sonar.local.com_error.log warn; error_page 403 404 /40x.html; location / { index index.html index.htm; proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://tomcat_tools.sonar.local;
expires 0d; } }
增减配置完成后,重启nginx
[root@localhost domains]# service nginx restart
3、修改防火墙开放9000端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9000 -j ACCEPT
4、启动sonar
[root@localhost linux-x86-64]# pwd
/usr/local/sonarqube-4.5.4/bin/linux-x86-64 [root@localhost linux-x86-64]# ls lib sonar.sh wrapper [root@localhost linux-x86-64]# ./sonar.sh start Starting SonarQube... Started SonarQube.
5、访问tools.sonar.local.com
6、登录sonar[默认账号admin/admin]安装汉化包
Settings/SYSTEM/Update Center/Available Plugins
选择汉化包,汉化完成之后需要重新启动.
项目代码提交sonar检测代码质量
1、在maven本地仓库的settings.xml中 <profiles>节点之间增加如下内容
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.jdbc.url> jdbc:mysql://192.168.147.129:3306/sonar?useUnicode=true&characterEncoding=utf8
</sonar.jdbc.url>
<sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>root</sonar.jdbc.username>
<sonar.jdbc.password>123456</sonar.jdbc.password>
<sonar.host.url>http://tools.sonar.local.com</sonar.host.url>
</properties>
</profile>
</profiles>
或者直接在应用项目的总pom中增加如上内容.区别是前者为所有项目增加,后者只是针对单个项目配置.
2、创建mvn命令

3、执行命令,查看sonar控制面板的项目

如图,为刚才的项目接入到sonar检测上的情况.点击查看该代码的质量情况.
转载请注明出处:[http://www.cnblogs.com/dennisit/p/4546245.html]