---
title: "Sonar代码分析实战（7）——1.2.3 使用Maven插件"
author: "headsen"
date: "2013-02-04T20:32:00Z"
tags: []
categories: ["开源软件"]
slug: "sonar-7-1-2-3-maven"
draft: false
lastmod: "2013-02-04T20:32:00Z"
id: 1788815
url: "https://headsen.iteye.com/blog/1788815"
---

如果您的项目是基于Maven构建，那么推荐使用此方式。

 

#### 前提准备

必须先对Maven的本地仓库进行一些配置，以便Maven Sonar插件正常运行。

相关的配置请参考本手册前面章节。

 

#### 分析Maven工程

分析Maven工程只需要在pom.xml存在的目录下运行MVN的Sonar插件指令。Sonar指令运行前最好运行install指令，保证代码最新。

 

推荐的方法

**mvn clean install -DskipTests=true**

**mvn sonar:sonar**

 

设置skipTests=true是为了避免单元测试执行两次。install和sonar指令默认都会执行单元测试。你可以禁用单元测试，相关的配置参考Maven的相关文档。

 

注意：

使用Eclipse

确保没有使用maven embedder插件。定义Maven执行环境为本地安装的Maven，使用最新的Macen插件，不要勾选"resolve workspace artifacts"。

详细说明：[http://jira.codehaus.org/browse/SONAR-929](http://jira.codehaus.org/browse/SONAR-929)

 

高级选项

一些Sonar不支持的高级选项请不要使用，如"--projects" 和 "--resume-from"。

 

替代方法

如果你不想使用上面的两行指令，你可以直接使用下面的一行指令。

**mvn clean install sonar:sonar -Dmaven.test.failure.ignore=true**

这个指令运行时，单元测试会执行两次，如果加上参数DskipTests=true。Sonar就会执行单元测试，代码分析报告中也不会有单元测试的结果。

参数-Dmaven.test.failure.ignore=true确保及时单元测试执行失败，Sonar的分析结果依然会收录。

 

#### []()配置Sonar分析

和Sonar Runner一样，可以对Sonar分析进行一些参数配置。

一个简单的pom.xml配置示意[here](https://github.com/SonarSource/sonar-examples/blob/master/projects/languages/java/maven/java-maven-simple/pom.xml)。

额外的分析参数，请下面章节中。

 

安全：

从Sonar3.4开始，如果一个工程被匿名访问，执行代码分析时，'sonar.login' and 'sonar.password'属性是必须的。这些属性必须属于该项目‘User’角色下的一个用户。你可以通过其他方式，提供这些属性：

直接通过命令行：

-Dsonar.login=myUser -Dsonar.password=myPassword

在pom.Xml文件

在Maven全局配置文件settings.xml

 

特殊的一个项目允许匿名分析：

1、'[sonar.forceAuthentication](http://docs.codehaus.org/display/SONAR/Security)'设置为true;

2、'[sonar.forceAuthentication](http://docs.codehaus.org/display/SONAR/Security)'设置为false，且'Anyone'用户组没有被授予该项目'User'角色

 

备注：

 

工程实例：

同样的为了帮助入门学习，gitthub提供了实例项目，可以供浏览和下载。

[browsed](https://github.com/SonarSource/sonar-examples) or [downloaded](https://github.com/SonarSource/sonar-examples/zipball/master): projects/languages/java/maven/java-maven-simple

 

Maven插件版本

两个版本的Sonar maven插件存在，支持Maven2和Maven3。使用哪种取决于项目的Maven 版本。

**Maven2 Only：**

将以下配置加载pom.xml文件中

```
<build>
```

```
  <pluginManagement>
```

```
    <plugins>
```

```
      <plugin>
```

```
        <groupId>org.codehaus.mojo</groupId>
```

```
        <artifactId>sonar-maven-plugin</artifactId>
```

```
        <version>1.0</version>
```

```
      </plugin>
```

```
    <plugins>
```

```
  </pluginManagement>
```

```
</build>
```

**Maven 3 only：**

```
<build>
```

```
  <pluginManagement>
```

```
    <plugins>
```

```
      <plugin>
```

```
        <groupId>org.codehaus.mojo</groupId>
```

```
        <artifactId>sonar-maven-plugin</artifactId>
```

```
        <version>2.0</version>
```

```
      </plugin>
```

```
    <plugins>
```

```
  </pluginManagement>
```

```
</build>
```

**Both Maven 2 and Maven 3**

```
<build>
```

```
  <pluginManagement>
```

```
    <plugins>
```

```
      <plugin>
```

```
        <groupId>org.codehaus.mojo</groupId>
```

```
        <artifactId>sonar-maven-plugin</artifactId>
```

```
        <version>${sonarVersion}</version>
```

```
      </plugin>
```

```
    <plugins>
```

```
  </pluginManagement>
```

```
</build>
```

```
<profile>
```

```
  <id>maven-2</id>
```

```
  <activation>
```

```
    <file>
```

```
      <!-- basedir expression is only recognized by Maven 3.x (see MNG-2363) -->
```

```
      <missing>${basedir}</missing>
```

```
    </file>
```

```
  </activation>
```

```
  <properties>
```

```
    <sonarVersion>1.0</sonarVersion>
```

```
  </properties>
```

```
</profile>
```

```
<profile>
```

```
  <id>maven-3</id>
```

```
  <activation>
```

```
    <file>
```

```
      <!-- basedir expression is only recognized by Maven 3.x (see MNG-2363) -->
```

```
      <exists>${basedir}</exists>
```

```
    </file>
```

```
  </activation>
```

```
  <properties>
```

```
    <sonarVersion>2.0</sonarVersion>
```

```
  </properties>
```

```
</profile>
```

 

 

#### 分析多模块多语言项目

从Sonar3.3开始，支持分析多模块多语言项目。只需为每个模块pom.xml配置对应的'sonar.language'属性即可。

同样，为了帮助读者尽快上手，github上有实例可供浏览和下载。

 [browsed](https://github.com/SonarSource/sonar-examples) or [downloaded](https://github.com/SonarSource/sonar-examples/zipball/master): projects/languages/multi-language/multi-language-java-javascript-maven

 

 

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