說明:sonar依賴數據庫.
MySQL優化
1、筆者使用的是mysql數據庫.首先對mysql做簡單的優化配置.
[root@localhost bin]# cat /etc/my.cnf[mysqld]max_allowed_packet=10Mdatadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql# Disabling symbolic-links is recommended to PRevent assorted security riskssymbolic-links=0# 修改默認的編碼為utf8default-character-set=utf8# 修改默認的存儲引擎為InnoDBdefault-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.logpid-file=/var/run/mysqld/mysqld.pid
2、重啟mysql服務
[root@localhost bin]# service mysqld restartStopping 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.propertiessonar.jdbc.username=rootsonar.jdbc.passWord=123456sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformancesonar.web.javaOpts=-Xmx768m -XX:MaxPermSize=160m -XX:+HeapDumpOnOutOfMemoryErrorsonar.web.host=0.0.0.0sonar.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]# lslib sonar.sh wrapper[root@localhost linux-x86-64]# ./sonar.sh startStarting 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://m.survivalescaperooms.com/dennisit/p/4546245.html]
新聞熱點
疑難解答