使用SonarQube Scanner for Maven 分析

特征

建议使用此分析器启动Java Maven项目的分析。html

兼容性

Maven版本java

2.Xreact

3.Xgit

兼容性 (出错) (滴答)

 

从maven-sonar-plugin 3.4.0.905开始,再也不支持SonarQube <5.6。github

若是使用5.6以前的SonarQube实例,则应该使用maven-sonar-plugin 3.3.0.603。apache

从maven-sonar-plugin 3.1开始,再也不支持Maven <3.0。安全

若是在3.0以前使用Maven,你应该使用maven-sonar-plugin 3.0.2。服务器

先决条件

  • Maven 3.x
  • SonarQube已  安装
  • 至少SonarQube服务器支持的Java最低版本正在使用中(Java 8用于最新的LTS)
  • 您但愿分析的每种语言的语言插件都已安装
  • 您已阅读  分析代码源。 

初始设置

全局设置

编辑位于$ MAVEN_HOME / conf〜/ .m2中的  settings.xml文件,以设置插件前缀和可选的SonarQube服务器URL。maven

例:单元测试

<settings>

    <pluginGroups>

        <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>

    </pluginGroups>

    <profiles>

        <profile>

            <id>sonar</id>

            <activation>

                <activeByDefault>true</activeByDefault>

            </activation>

            <properties>

                <!-- Optional URL to server. Default value is http://localhost:9000 -->

                <sonar.host.url>

                  http://myserver:9000

                </sonar.host.url>

            </properties>

        </profile>

     </profiles>

</settings>

分析一个Maven项目

分析Maven项目包括运行Maven目标:sonar:sonar在pom.xml文件所在的目录中。

mvn clean verify sonar:sonar

# In some situation you may want to run sonar:sonar goal as a dedicated step. Be sure to use install as first step for multi-module projects

mvn clean install

mvn sonar:sonar


# Specify the version of sonar-maven-plugin instead of using the latest. See also 'How to Fix Version of Maven Plugin' below.

mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.4.0.905:sonar

要得到覆盖信息,您须要在分析以前生成覆盖率报告。有关更多信息,请参阅  Java项目的单元测试代码覆盖率

配置SonarQube分析

分析参数列在分析参数页面上。你必须像这样在你的pom.xml的<properties>部分配置它们:

<properties>

  <sonar.exclusions> [...] </sonar.exclusions>

</properties>

安全

任何被授予执行分析  权限的用户都  能够运行分析。

若是“  任何人” 组未得到“  执行分析”  权限,或者SonarQube实例受到保护(该  sonar.forceAuthentication 属性设置为  true),则 必须经过该 属性提供具备“ 执行分析”权限  的用户的分析标记  sonar.login。 sonar-scanner -Dsonar.login=[my analysis token]

 

从SonarQube分析中排除模块

你能够:

  • 在要排除的模块的pom.xml中定义属性<sonar.skip> true </sonar.skip>

  • 使用构建配置文件来排除某些模块(如用于集成测试)
  • 使用高级反应堆选项(如“-pl”)。例如mvn sonar:sonar -pl!module2

示例项目

为了帮助您开始,您能够在这里找到一个简单的项目示例:https :  //github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-maven

如何修复Maven插件的版本

建议  锁定Maven插件的版本

项目使用Maven 3进行分析

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.sonarsource.scanner.maven</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>3.4.0.905</version>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

故障排除

若是你获得一个java.lang.OutOfMemoryError,你能够在* nix环境中设置MAVEN_OPTS环境变量:

export MAVEN_OPTS="-Xmx512m"

在Windows环境中,避免使用双引号,由于它们会被误解。

set MAVEN_OPTS=-Xmx512m

相关文章
相关标签/搜索