SonarQube 配置

1. 安装 SonarQube 和数据库

下载最新的 sonarqube-6.1、MySQL 5.7.X。

创建数据库:

CREATE DATABASE `SonarQube` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE USER SonarQube IDENTIFIED BY 'SonarQube#123';

GRANT ALL PRIVILEGES ON SonarQube.* TO SonarQube;
flush privileges;

配置 sonarqube 服务器, $sonarqube_home/conf/sonar.properties 文件:

sonar.jdbc.username=SonarQube
sonar.jdbc.password=SonarQube#123

sonar.jdbc.url=jdbc:mysql://localhost:3306/SonarQube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

sonar.web.host=0.0.0.0
sonar.web.port=9000

2. 分析 Maven 工程

$M2_HOME/conf/settings.xml 配置如下:

<profile>
    <id>sonar</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <sonar.jdbc.url>jdbc:mysql://localhost:3306/SonarQube</sonar.jdbc.url>
        <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
        <sonar.jdbc.username>SonarQube</sonar.jdbc.username>
        <sonar.jdbc.password>SonarQube#123</sonar.jdbc.password>
        <sonar.host.url>http://localhost:9000</sonar.host.url>
    </properties>
</profile>

注意这里的数据库配置和 sonar.host 配置必须与前面的一致。

然后就可以通过 mvn sonar:sonar 来执行代码分析,并将结果保存在 Sonar 数据库中。

也可以参考: http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Maven

分析 Ant 项目

参考: http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Ant

首先下载 SonarQube Scanner for Ant 2.5

<project name="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) -->
<property name="sonar.host.url" value="http://localhost:9000" />

...

<!-- Define the SonarQube project properties -->
<property name="sonar.projectKey" value="org.codehaus.sonar:example-java-ant" />
<property name="sonar.projectName" value="Simple Java Project analyzed with the SonarQube Ant Task" />
<property name="sonar.projectVersion" value="1.0" />
<property name="sonar.sources" value="src" />
<property name="sonar.java.binaries" value="build" />
<property name="sonar.java.libraries" value="lib/*.jar" />
...

<!-- Define the SonarQube target -->
<target name="sonar">
    <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
        <!-- Update the following line, or put the "sonarqube-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
        <classpath path="path/to/sonar/ant/task/lib/sonarqube-ant-task-*.jar" />
    </taskdef>

    <!-- Execute the SonarQube analysis -->
    <sonar:sonar />
</target>

然后执行 ant sonar 即可。

多模块项目配置:

<!-- Set modules IDs -->
<property name="sonar.modules" value="module-one,module-two"/>

<!-- For modules, properties are inherited from the parent. They can be overridden as shown below: -->
<property name="module-one.sonar.projectName" value="Module One" />
<property name="module-one.sonar.sources" value="sources/java" />
<property name="module-one.sonar.binaries" value="target" />
<!-- Default module base directory is <curent_directory>/<module_ID>. It can be overridden if necessary -->
<property name="module-one.sonar.projectBaseDir" value="Module 1" />  

与 Jekins 集成

对于 Ant 项目,在 Jekins 下可能会碰到 SVNAuthenticationException: svn: E170001 错误,在 SonarQube 服务器上设置 配置 > 通用配置 > SCM, 把 Disable the SCM Sensor 打开。


欢迎关注我的微信公众号: coderbee笔记,可以更及时回复你的讨论。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据