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/)