转载:https://www.jianshu.com/p/402987500bfd
一. 简介
Sonar是一个用于代码质量管理的开源平台,用于管理源代码的质量.通过插件形式,可以支持包括java,C#,C/C++,PL/SQL,Cobol,JavaScrip,Groovy等等二十几种编程语言的代码质量管理与检测.
二. 安装
- 环境要求
- jdk 8 [必须要jdk1.8,否则安装完成之后也无法启动]
- mysql 5.6+
- 浏览器 ie 11+ firefox chrome safari
- 在mysql中创建数据库以及用户
/*创建数据库*/
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
/*创建用户*/
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
/*授权*/
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
FLUSH PRIVILEGES;
-
下载,并解压sonarqube 5.6, sonar-scanner-2.6.1
https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.6.zip
https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.6.1.zip -
将解压后的文件夹移入/etc/目录下 (其实随便移到哪个目录都可以)
-
修改sonarqube的配置文件
/etc/sonarqube 5.6/sonar.properties
#数据库配置
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://192.168.1.110:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.driverClassName=com.mysql.jdbc.Driver
#web配置
sonar.web.context=/sonarqube
sonar.web.port=9000
-
安装中文语言包
- 下载中文语言包插件
http://download.csdn.net/download/u010218242/9272667 - 将jar包放入如下目录
/etc/sonarqube-5.6/extensions/plugins
- 下载中文语言包插件
-
启动sonarqube
/etc/sonarqube-5.6/bin/linux-x86-64/sonar.sh start
- sonarqube默认超级帐号
帐号:admin
密码:admin
三. 在maven中使用sonarqube
- 在maven的settings.xml中加入如下配置
<settings>
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>
http://myserver:9000
</sonar.host.url>
</properties>
</profile>
</profiles>
</settings>
- 在项目的pom.xml中加入sonarqube的插件配置
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.0.1</version>
</plugin>
</plugins>
执行 mvn sonar:sonar 命令进行代码分析
详细配置参见:
http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Maven
- 查看分析结果
http://ip:端口/sonarqube/
e.g.
http://192.168.1.110:9000/sonarqube/
pom.xml 例子
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qc</groupId>
<artifactId>sonar_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sonar_test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- sonar项目的语言 -->
<sonar.language>java</sonar.language>
<!-- sonar中的项目名 -->
<sonar.projectName>hello_sonar_test</sonar.projectName>
<!-- sonar中的项目唯一标识(要避免跟别人的项目唯一标识重复) -->
<sonar.projectKey>hello_sonar_test</sonar.projectKey>
<!-- sonar中的项目版本 -->
<sonar.projectVersion>1.0</sonar.projectVersion>
<!-- sonar项目语言 -->
<sonar.language>java</sonar.language>
<!-- sonar项目语言编码 -->
<sonar.sourceEncoding>UTF-8</sonar.sourceEncoding>
<!-- 排除目录,多个排除目录使用逗号分割 -->
<sonar.exclusions>
**/*Bean.java, **/*DTO.java, ank/**/*
</sonar.exclusions>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>