用Sonarqube檢查和度量代碼質(zhì)量――安裝sonarqube
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-09-02 14:10:54 閱讀次數(shù):4989次
以前關(guān)注點(diǎn)一直在怎么提高應(yīng)用程序的質(zhì)量,沒太在意代碼級(jí)別的質(zhì)量。最近因?yàn)槟承┮蛩氐耐苿?dòng),需要關(guān)注到代碼級(jí)別的質(zhì)量去,把質(zhì)量工作盡量往前推,也符合質(zhì)量控制的原則。 試用了一下sonarqube(老版本的叫sonar,ww.sonarqube.org),對(duì)代碼的提升的確有很多的作用,sonarqube能從7個(gè)維度來(lái)對(duì)代碼質(zhì)量進(jìn)行度量。多大的作用,大家實(shí)踐下就很容易看出來(lái)。尤其是建議大家把rules里面的說(shuō)明和例子都好好看看,對(duì)以后自己寫代碼的時(shí)候,質(zhì)量提高有很大好處。
Sonarqube安裝:
Sonarqube一共分3 部分:
以下安裝步驟是Linux下的安裝示例
數(shù)據(jù)庫(kù):
這里我用的是mysql數(shù)據(jù)庫(kù),直接執(zhí)行SQL:
CREATEDATABASEsonarCHARACTERSETutf8COLLATEutf8_general_ci;
賦予后面連接sonarqube的數(shù)據(jù)庫(kù)用戶讀寫權(quán)限即可
web服務(wù): 修改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服務(wù)器,性能也足夠好,不需要配置tomcat什么的,到這里整個(gè)sonar web服務(wù)配置完成了,到sonarqube/bin/linux-x86-64目錄下,啟動(dòng)./sonar.sh start即可,啟動(dòng)后有任何問題可以查看log: sonarqube/logs/sonar.log, 通過瀏覽器訪問http://192.168.22.99:9001/sonarqube, 打開登陸頁(yè)面,默認(rèn)管理員賬戶是admin/admin
分析器:
Sonarqube通過插件 支持20+種語(yǔ)言, Java, python, C#, C/C++, PL/SQL, Cobol等, 但C語(yǔ)言的插件是收費(fèi)的。到這里http://docs.codehaus.org/display/SONAR/Plugin+Library 下載對(duì)應(yīng)語(yǔ)言的插件,放置到sonarqube/extensions/plugins目錄下,重啟web服務(wù)即可。
分析器主要5種:
SonarQube Runner(萬(wàn)能,支持后面幾種方式的工程),
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目錄下,在環(huán)境變量里面加上SONAR_RUNNER_HOME="/home//sonarruner"。
到這里整個(gè)Sonarqube的運(yùn)行環(huán)境就全部配置完成了,下一篇講解怎么運(yùn)行分析器。
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)