---
title: "Sonar代码分析实战（4）——1.1.3\tSonar分析客户端"
author: "headsen"
date: "2013-01-28T19:08:19Z"
tags: []
categories: ["开源软件"]
slug: "sonar-4-1-1-3-sonar"
draft: false
lastmod: "2013-01-28T19:10:49Z"
id: 1777016
url: "https://headsen.iteye.com/blog/1777016"
---

# Sonar客户端

Sonar的安装难点之一就在于各种客户端的选择、配置和集成等。本文档将次章节作为“安装Sonar”的并行章节阐述。

## 安装配置Sonnar Runner

Sonar Runner是最常用的分析客户端之一，可以对绝大多数类型的项目进行分析。

下载地址：[下载目录](http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-runner/)。现在的版本是sonar-runner-2.0.zip

安装要求：JDK1.5 或更高。

##### 安装过程

1、将下载包解压到一个本地路径下。

2、将解压路径设置为环境变量：SONAR\_RUNNER\_HOME

3、更改配置文件：$SONAR\_RUNNER\_HOME/conf/sonar-runner.properties。

主要是数据源的配置，sonar-runner会将分析的结果存放在数据库中。如下面所示，只需将注释的模板适当位置去掉注释并更改为自己对应的Sonar配置即可。

`#----- Default Sonar server`

`#sonar.host.url=`[http://localhost:9000](http://localhost:9000/)

 

`#----- PostgreSQL`

`#sonar.jdbc.url=jdbc:postgresql://localhost/sonar`

`#sonar.jdbc.driver=org.postgresql.Driver`

 

`#----- MySQL`

`#sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8`

`#sonar.jdbc.driver=com.mysql.jdbc.Driver`

 

`#----- Oracle`

`#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE`

`#sonar.jdbc.driver=oracle.jdbc.driver.OracleDriver`

 

`#----- Global database settings`

`#sonar.jdbc.username=sonar`

`#sonar.jdbc.password=sonar`

 

`#----- Default directory layout`

`#sonar.sources=src/main/java`

`#sonar.tests=src/test/java`

`#sonar.binaries=target/classes`

` `

4、将$SONAR\_RUNNER\_HOME/bin添加到Path环境变量中。

5、可以通过Shell或命令行检查安装情况，输入'sonar-runner.bat -h'（Linux输入'sonar-runner -h'），将会得到以下信息：

`usage: sonar-runner [options]`

` `

`Options:`

`-h,--help             Display help information`

`-X,--debug            Produce execution debug output`

`-D,--define <arg>     Define property`

要获得更多的信息，命令增加参数-Dsonar.verbose=true即可。  

使用SonarRunner执行Sonar具体分析请参考相关章节。

## 安装配置Maven分析客户端

支持Maven 2.0.10, 2.1.x, 2.2.x or 3.x。若没有安装Maven，请下载 [Maven](http://maven.apache.org/)。

##### Maven相关配置

由于基于Maven进行分析时，需要使用Maven的插件指令处罚Sonar分析，所以需要对Maven的全局配置进行修改。

编辑setting.xml: 将以下配置放在对于的XML节点，同时将相关配置更改为自己配置。

`<settings>`

`<profiles>`

`<profile>`

`<id>sonar</id>`

`<activation>`

`<activeByDefault>true</activeByDefault>`

`</activation>`

`<properties>`

`<!-- EXAMPLE FOR MYSQL -->`

`<sonar.jdbc.url>`

`jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8`

`</sonar.jdbc.url>`

`<sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</sonar.jdbc.driverClassName>`

`<sonar.jdbc.username>sonar</sonar.jdbc.username>`

`<sonar.jdbc.password>sonar</sonar.jdbc.password>`

` `

`<!-- optional URL to server. Default value is`[`http://localhost:9000`](http://localhost:9000/)`-->`

`<sonar.host.url>`

`                  `[`http://myserver:9000`](http://myserver:9000/)

`</sonar.host.url>`

`</properties>`

`</profile>`

`</profiles>`

`</settings>`

使用Maven插件分析大型项目时，常常会出现内存不足OutOfMemoryError。这时需要更改Maven的默认内存：

`export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=128m"`

Window下，export改为set即可。

使用Maven插件客户端执行Sonar具体分析请参考相关章节。

 

## 安装配置Ant客户端

首先下载Sonar Ant Task包：

[http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-ant-task/2.0/sonar-ant-task-2.0.jar](http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-ant-task/2.0/sonar-ant-task-2.0.jar)

**特点：**

Sonar Ant Task允许将Sonar分析放在Ant的build脚本中。

**要求：**

Ant 1.7.1+、Java 1.5+

**安装：**

下载Sonar Ant Task即可。

使用Ant Task执行Sonar具体分析请参考相关章节。

## 持续集成CI

暂时不做翻译。请参考英文文档。

Page: [Installing on AIX, HPUX or Solaris](http://docs.codehaus.org/display/SONAR/Installing+on+AIX%2C+HPUX+or+Solaris)

Page: [Deploying Sonar on JEE Server](http://docs.codehaus.org/display/SONAR/Deploying+Sonar+on+JEE+Server)

Page: [Running Sonar as a Service on Windows](http://docs.codehaus.org/display/SONAR/Running+Sonar+as+a+Service+on+Windows)

Page: [Running Sonar as a Service on Linux](http://docs.codehaus.org/display/SONAR/Running+Sonar+as+a+Service+on+Linux)

Page: [Running Sonar behind a Proxy](http://docs.codehaus.org/display/SONAR/Running+Sonar+behind+a+Proxy)

 

文档下载：[http://wenku.baidu.com/view/088e5b1b6edb6f1aff001fc0.html](http://wenku.baidu.com/view/088e5b1b6edb6f1aff001fc0.html)