Sonar is an open source platform for code quality management for managing the quality of Java source code. Through the plugin mechanism, Sonar can integrate different testing tools, code analysis tools, and continuous integration tools, such as pmd-cpd, checkstyle, findbugs, and Jenkins. These results are reprocessed through different plug-ins, and the changes in code quality are measured in a quantitative way, so that code quality management can be easily carried out for projects of different scales and types.
1. Configure
the database
Create sonar database
create database sonar default charset=utf8;
Configure sonar user
create user 'sonar' identified by 'sonar123'; grant all on sonar.* to 'sonar'@'%' identified by 'sonar123'; grant all on sonar.* to 'sonar'@'localhost' identified by 'sonar123'; flush privileges;
2. Download
sonar and sonar-runner, sonar Chinese package (sonar-l10n-zh-plugin)
Environment requirements for Sonarqube installation: java and mysql.
Software download address:
http://www.sonarqube.org/downloads/
3. Modify the configuration of sonarque and sonar-runner
Edit the ${SONAR_HOME}/conf/sonar.properties configuration database
sonar.jdbc.username=sonar sonar.jdbc.password=sonar123 sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
Edit the ${sonar-runner_home}/conf/sonar-runner.properties configuration database
sonar.host.url=http://localhost:9000 sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8 sonar.jdbc.username=sonar sonar.jdbc.password=sonar123 sonar.sourceEncoding=UTF-8 sonar.login=admin sonar.password=admin
4. Setting environment variables (Mac OSX)
sudo vi ~/.bash_profile # sonar config export SONAR_HOME=/Users/jiangzhiqiang/dev/sonarqube-5.1.1 export SONAR_RUNNER_HOME=/Users/jiangzhiqiang/dev/sonar-runner-2.4 export PATH=${PATH}:${SONAR_RUNNER_HOME}
make the configuration take effect
source ~/.bash_profile
Check whether the configuration takes effect
echo $SONAR_HOME
Execute sonar-runner -h on the command line If the content is displayed, the sonar-runner configuration is successful.
5. Install the plugin
1. 使用Update Center, 用管理员账号登录 “Administer System” 后, Settings —> System —>
Update Center —> Available Plugins , 搜索合适的插件安装。
2. 下载插件,放在 $SONARQUBE_HOME/extensions/plugins
3. 重启 sonar
./sonar.sh restart
重启 sonar 后安装的插件才会生效。
6. 启动 sonar
目录切换至sonar的<install_directory>/bin/linux-x86-64/目录,启动服务
./sonar.sh start 启动服务
./sonar.sh stop 停止服务
./sonar.sh restart 重启服务
启动 sonar 服务后,访问 http://localhost:9000
或 ps aux|grep sonar 显示 —>
jiangzhiqiang 705 0.1 2.9 3817144 240372 ?? S 3:15下午 0:27.72 /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/bin/java -Djava.awt.headless=true -Xmx1G -Xms256m -Xss256k -Djava.net.preferIPv4Stack=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -Djava.io.tmpdir=/Users/jiangzhiqiang/dev/sonarqube-5.1.1/temp -cp ./lib/common/*:./lib/search/* org.sonar.search.SearchServer /var/folders/72/0c884kmd7m1d4qsg1jcfm5540000gn/T/sq-process2282470883822622271properties
为启动成功。
7.
为项目配置 sonar runner 运行需要的配置
在项目的根目录创建 sonar-project.properties,内容:
# required metadata # projectKey 是项目的唯一标识,不能重复 sonar.projectKey=pattern sonar.projectName=pattern sonar.projectVersion=1.0 sonar.sourceEncoding=UTF-8 sonar.modules=java-module #sonar.modules=java-module,javascript-module,html-module # Java module java-module.sonar.projectName=Java Module java-module.sonar.language=java # sources为源码目录 java-module.sonar.sources=src # .表示projectBaseDir指定的目录 java-module.sonar.projectBaseDir=. # binaries为编译后的classes目录,这个目录要注意一下, # 如果都在bin下面或者目录还有很多层级才到classe目录,可以直接为sonar.binaries=bin, # sonar会自动遍历所有目录 sonar.binaries=target/classes # JavaScript module #javascript-module.sonar.projectName=JavaScript Module #javascript-module.sonar.language=js #javascript-module.sonar.sources=js #javascript-module.sonar.projectBaseDir=webapp # webroot \src\main\webapp # Html module #html-module.sonar.projectName=Html Module #html-module.sonar.language=web #html-module.sonar.sources=pages #html-module.sonar.projectBaseDir=webapp
8. 项目分析
在项目目录路径运行
sonar-runner -X, 即对项目进行分析,成功后访问http:\\localhost:9000 查看分析的结果。
注:加 -X 是debug模式。