用Sonarqube檢查和度量代碼質量――安裝sonarqube
來源:程序員人生 發布時間:2014-09-02 14:10:54 閱讀次數:4983次
以前關注點一直在怎么提高應用程序的質量,沒太在意代碼級別的質量。最近因為某些因素的推動,需要關注到代碼級別的質量去,把質量工作盡量往前推,也符合質量控制的原則。 試用了一下sonarqube(老版本的叫sonar,ww.sonarqube.org),對代碼的提升的確有很多的作用,sonarqube能從7個維度來對代碼質量進行度量。多大的作用,大家實踐下就很容易看出來。尤其是建議大家把rules里面的說明和例子都好好看看,對以后自己寫代碼的時候,質量提高有很大好處。
Sonarqube安裝:
Sonarqube一共分3 部分:
以下安裝步驟是Linux下的安裝示例
數據庫:
這里我用的是mysql數據庫,直接執行SQL:
CREATEDATABASEsonarCHARACTERSETutf8COLLATEutf8_general_ci;
賦予后面連接sonarqube的數據庫用戶讀寫權限即可
web服務: 修改sonarqube/conf/sonar.properties
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=mysql_username
sonar.jdbc.password=mysql_password
# Comment the following line to deactivate the default embedded database.
#sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar
#----- MySQL 5.x
# Comment the embedded database and uncomment the following line to use MySQL
sonar.jdbc.url=jdbc:mysql://192.168.22.99:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=t
sonar.web.host=0.0.0.0
sonar.web.context=/sonarqube
sonar.web.port=9001
sonarqube自帶web服務器,性能也足夠好,不需要配置tomcat什么的,到這里整個sonar web服務配置完成了,到sonarqube/bin/linux-x86-64目錄下,啟動./sonar.sh start即可,啟動后有任何問題可以查看log: sonarqube/logs/sonar.log, 通過瀏覽器訪問http://192.168.22.99:9001/sonarqube, 打開登陸頁面,默認管理員賬戶是admin/admin
分析器:
Sonarqube通過插件 支持20+種語言, Java, python, C#, C/C++, PL/SQL, Cobol等, 但C語言的插件是收費的。到這里http://docs.codehaus.org/display/SONAR/Plugin+Library 下載對應語言的插件,放置到sonarqube/extensions/plugins目錄下,重啟web服務即可。
分析器主要5種:
SonarQube Runner(萬能,支持后面幾種方式的工程),
Maven(和maven編譯工程集成),
SonarQube Ant Task(和ant編譯工程集成),
Gradle(和Gradle編譯工具集成,很少聽過),
CI Engine(主要和Jenkins , Hudson等CI工具集成)。
以下主要講Sonarqube runner分析器的使用:
下載Sonarqube 分析器:http://docs.codehaus.org/display/SONAR/Installing+and+Configuring+SonarQube+Runner, 解壓后修改conf目錄下的sonar-runner.properties,
如下例子。
#----- Default SonarQube server
sonar.host.url=http://192.168.23.94:9001/sonarqube
#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar
#----- MySQL
sonar.jdbc.url=jdbc:mysql://192.168.23.99:3306/sonarqube_qa?useUnicode=true&characterEncoding=utf8
#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE
#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
#----- Global database settings
sonar.jdbc.username=mysql_username
sonar.jdbc.password=mysql_password
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
#----- Security (when 'sonar.forceAuthentication' is set to 'true')
sonar.login=admin
sonar.password=admin
把sonarruner/bin加入到path目錄下,在環境變量里面加上SONAR_RUNNER_HOME="/home//sonarruner"。
到這里整個Sonarqube的運行環境就全部配置完成了,下一篇講解怎么運行分析器。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈