1. SonarQube 4.4系统要求
l OpenJDK 7 以上
l MySQL 5.7
l Microsoft Internet Explorer 9、10
2. SonarQube Platform Overview
3. 安装服务端
下载解压后修改配置sonar.properties、wrapper.conf文件
4. 安装分析器
a) SonarQubeRunner: recommended for all non-Maven projects
l 解压后配置SONAR_RUNNER_HOME、PATH环境变量
l 修改<install_directory>/conf/sonar-runner.properties
l 在工程根目录下新建sonar-project.properties
sonar-project.properties
# Required metadata sonar.projectKey=my:project sonar.projectName=My project sonar.projectVersion=1.0 # Path to the parent source code directory. # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. # Since SonarQube 4.2, this property is optional if sonar.modules is set. # If not set, SonarQube starts looking for source code from the directory containing # the sonar-project.properties file. sonar.sources=src # Encoding of the source code sonar.sourceEncoding=UTF-8 # Additional parameters sonar.my.property=value |
l 在工程目录下执行 sonar-runner命令
Maven: recommended for all projects built with Maven
b) SonarQube Ant Task:to integrate with projects built with Ant
· Ant1.7.1 or higher
· Java1.6 or higher
build.xml
<projectname="My Project"default="all"basedir="."xmlns:sonar="antlib:org.sonar.ant"> <!-- Define the SonarQube global properties (the most usual way is to pass these properties via the command line) --> <propertyname="sonar.jdbc.url"value="jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8"/> <propertyname="sonar.jdbc.username"value="sonar"/> <propertyname="sonar.jdbc.password"value="sonar"/> <!-- Define the SonarQube project properties --> <propertyname="sonar.projectKey"value="org.codehaus.sonar:example-java-ant"/> <propertyname="sonar.projectName"value="Simple Java Project analyzed with the SonarQube Ant Task"/> <propertyname="sonar.projectVersion"value="1.0"/> <propertyname="sonar.language"value="java"/> <propertyname="sonar.sources"value="src"/> <propertyname="sonar.binaries"value="build/*.jar"/> <!-- Define the SonarQube target --> <targetname="sonar"> <taskdefuri="antlib:org.sonar.ant"resource="org/sonar/ant/antlib.xml"> <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder --> <classpath path="path/to/sonar/ant/task/lib/sonar-ant-task-*.jar" /> </taskdef> <!-- Execute the SonarQube analysis --> <sonar:sonar/> </target> |
Gradle: to integrate with projects built with Gradle
c) CI Engine:seeJenkins pluginorHudson pluginor Bamboo plugin or AnthillPro plugin. Note that other CI engines can be usedeven if there is no SonarQube plugin available.
http://docs.codehaus.org/display/SONAR/Configuring+SonarQube+Jenkins+Plugin
5. sonarqube eclipse插件
http://dist.sonar-ide.codehaus.org/eclipse/
Sonar支持多种插件,插件的下载地址为:http://docs.codehaus.org/display/SONAR/Plugin+Library
将下载后的插件上传到${SONAR_HOME}extensions\plugins目录下,重新启动sonar。
sonar默认集成了Java Ecosystem插件,该插件是一组插件的合集
1)Java [sonar-java-plugin]:java源代码解析,计算指标等
2)Squid [sonar-squid-java-plugin]:检查违反Sonar定义规则的代码
3)Checkstyle [sonar-checkstyle-plugin]:使用CheckStyle检查违反统一代码编写风格的代码
4)FindBugs [sonar-findbugs-plugin]:使用FindBugs检查违反规则的缺陷代码
5)PMD [sonar-pmd-plugin]:使用pmd检查违反规则的代码
6)Surefire [sonar-surefire-plugin]:使用Surefire执行单元测试
7)Cobertura [sonar-cobertura-plugin]:使用Cobertura获取代码覆盖率
8)JaCoCo [sonar-jacoco-plugin]:使用JaCOCO获取代码覆盖率
下面列出了一些常用的插件:
1)JavaScript代码检查:http://docs.codehaus.org/display/SONAR/JavaScript+Plugin
2)python代码检查:http://docs.codehaus.org/display/SONAR/Python+Plugin
3)Web页面检查(HTML、JSP、JSF、Ruby、PHP等):http://docs.codehaus.org/display/SONAR/Web+Plugin
4)xml文件检查:http://docs.codehaus.org/display/SONAR/XML+Plugin
5)scm源码库统计分析:http://docs.codehaus.org/display/SONAR/SCM+Stats+Plugin
6)文件度量:http://docs.codehaus.org/display/SONAR/Tab+Metrics+Plugin
7)中文语言包:http://docs.codehaus.org/display/SONAR/Chinese+Pack
8)时间表显示度量结果:http://docs.codehaus.org/display/SONAR/Timeline+Plugin
9)度量结果演进图:http://docs.codehaus.org/display/SONAR/Motion+Chart+Plugin
b)插件配置示例(本段内容来自http://www.ibm.com/developerworks/cn/java/j-lo-sonar/)