---
title: "Sonar代码分析实战（6）——1.2.2 Sonar Runner执行代码分析"
author: "headsen"
date: "2013-02-04T20:27:26Z"
tags: []
categories: ["开源软件"]
slug: "sonar-6-1-2-2-sonar-runner"
draft: false
lastmod: "2013-02-04T20:27:26Z"
id: 1788812
url: "https://headsen.iteye.com/blog/1788812"
---

使用Sonar做代码分析时，有很多种途径，但是推荐使用Sonar Runner。

本节描述基于Sonar Runner 2.0+

 

#### 前提准备

你必须提前下载，并安装Sonar Runner。下载：

[http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-runner/2.0/sonar-runner-2.0.zip](http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-runner/2.0/sonar-runner-2.0.zip)&lt;!--\[if !supportNestedAnchors]--&gt;[]()[]()&lt;!--\[endif]--&gt;

安装配置请参考前面章节中

 

#### 简单工程

在项目根路径下，创建配置文件，文件名为sonar-project.properties。sonar-runner执行分析时，会读取该文件。

 

文件内容示意：[]()[]() **sonar-project.properties**

[]()[]()**# required metadata**

**sonar.projectKey=my:project**

**sonar.projectName=My project**

**sonar.projectVersion=1.0**

 

**# path to source directories (required)**

**sonar.sources=srcDir1,srcDir2**

 

**# path to test source directories (optional)**

**sonar.tests=testDir1,testDir2**

 

**# path to project binaries (optional), for example directory of Java bytecode**

**sonar.binaries=binDir**

 

**# optional comma-separated list of paths to libraries. Only path to JAR file and path to directory of classes are supported.**

**sonar.libraries=path/to/library.jar,path/to/classes/dir**

 

**# The value of the property must be the key of the language.**

**sonar.language=cobol**

 

**# Additional parameters**

**sonar.my.property=value**

配置好上述文件后，从命令行在根路径下执行下面命令启动项目的代码分析。

**sonar-runner**

为了帮助新手尽快入门，github存取服务上存放简单的示例代码，可供[浏览](https://github.com/SonarSource/sonar-examples)和[下载](https://github.com/SonarSource/sonar-examples/zipball/master)。

#### 多模块工程

在Sonar分析时可以使用两种方式配置项目的结构。一种需要在项目下配置一个总文件，一种可以在每个模块下各自配置一个文件。

**方式一：**

将所有的配置放在一个**sonar-project.properties**文件，并放在项目的根路径下。

 

 

**文件内容示意：**

**# Root project information**

**sonar.projectKey=org.mycompany.myproject**

**sonar.projectName=My Project**

**sonar.projectVersion=1.0-SNAPSHOT**

 

**# Some properties that will be inherited by the modules**

**sonar.sources=src**

 

**# List of the module identifiers**

**sonar.modules=module1,module2**

 

**# Properties can obviously be overriden for**

**# each module - just prefix them with the module ID**

**module1.sonar.projectName=Module 1**

**module2.sonar.projectName=Module 2**

 

**方式二：**

每个模块下的配置放在各自的独立文件中。

 

**配置示意：**

总配置的内容 "MyProject/sonar-project.properties"  

**# Root project information**

**sonar.projectKey=org.mycompany.myproject**

**sonar.projectName=My Project**

**sonar.projectVersion=1.0-SNAPSHOT**

 

**# Some properties that will be inherited by the modules**

**sonar.sources=src**

 

**# List of the module identifiers**

**sonar.modules=module1,module2**

 

 

**子配置一 "MyProject/module1/sonar-project.properties"**

**# Redefine properties**

**# Note that you do not need to prefix the property here**

**sonar.projectName=Module 1**

 

 

**子配置二 "MyProject/module2/sonar-project.properties"**

**# Redefine properties**

**# Note that you do not need to prefix the property here**

**sonar.projectName=Module 2**

 

**值得注意:**

**1、子配置继承于父配置，子配置将可以覆盖父配置，通过两种方法：**

在父配置中配置属性前增加模块标识前缀。

在子配置中直接定义配置。

**2、特殊情况可以指定根目录**

默认情况下，模块的根目录默认为模块的标识符（如上面的示例）。特殊情况下，可以在配置文件中使用“sonar.projectBaseDir”属性来指定根目录。如：

**module1.sonar.projectBaseDir=My Module One  #含空格**

 

**module1.sonar.projectBaseDir=modules/mod1  #多层级**

**module2.sonar.projectBaseDir=modules/mod2**

 

**3、多模块项目使用Sonar做分析时不能只指定一个源代码目录。** 

为了帮助尽快入门，这里依然有部分示意代码。你可以[浏览](https://github.com/SonarSource/sonar-examples)和[下载](https://github.com/SonarSource/sonar-examples/zipball/master)。

**4、相同结构的多模块**projects/languages/java/sonar-runner/java-sonar-runner-modules-same-structure

**5、不同结构的多模块**projects/languages/java/sonar-runner/java-sonar-runner-modules-different-structures

**6、模块独自配置**projects/languages/java/sonar-runner/java-sonar-runner-modules-own-configuration-file

 

#### 多模块多语言功能

从Sonar3.3开始，Sonar支持多语言多模块工程的代码分析。

而配置也极其简单，只需在多模块工程的基础上增加一个语言属性的配置，如：

**module.sonar.language**

**module1.sonar.language=java**

**module2.sonar.language=javascript**

 

为了帮助尽快入门，这里依然有部分示意代码。你可以[浏览](https://github.com/SonarSource/sonar-examples)和[下载](https://github.com/SonarSource/sonar-examples/zipball/master)。项目路径： 

projects/languages/multi-language/multi-language-java-javascript-sonar-runner

高级用法

如果不在项目的工程的根路径创建sonar-project.properties文件，还可以进行其他选择。

 

1、通过command命令行直接指定：

**sonar-runner -Dsonar.projectKey=myproject -Dsonar.sources=src1 ..**

2、通过command命令行指定配置文件路径： 'project.settings'

**sonar-runner -Dproject.settings=../myproject.properties**

 

通过设置'sonar.working.directory'属性可以设置Sonar的工作目录，默认为'.sonar'

通过设置'project.home'属性可以指定项目的根路径。根路径下必须包含sonar-project.properties配置文件（除非执行指令时显示给定路径。）

命令行执行分析时，可以追加参数。[参数。](http://docs.codehaus.org/display/SONAR/Analysis+Parameters)

 

#### 疑难解答

对于较大的工程项目，经常出现内存不够的问题，需要设置虚拟机的内存。

**export SONAR\_RUNNER\_OPTS="-Xmx512m -XX:MaxPermSize=128m"**

在Window环境下：

**set SONAR\_RUNNER\_OPTS=-Xmx512m -XX:MaxPermSize=128m**

#### 从Sonar Runner 1.X向2.0迁移

只需将以下属性名替换为新属性名即可：

**sources =&gt; sonar.sources**

**tests =&gt; sonar.tests**

**binaries =&gt; sonar.binaries**

**libraries =&gt; sonar.libraries**

在sonar-project.properties文件明确设置sonar.sourceEncoding属性。

 

 

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