持续代码质量平台Sonar-3使用
Analyzing Source Code
参考链接:官方文档
本文只涉及maven和sonar-scanner方式,其它请参考上面链接。
Maven
适合Maven项目
- 在sonarqube平台上面个人账户生成token。
- 在本机的mvn配置settings.xml 增加下面配置
1
2
3
4
5
6
7
8
9
10<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>http://ip:9000</sonar.host.url>
<sonar.login>第一步生成的token值</sonar.login>
</properties>
</profile> - 代码库执行打包:mvn clean install
- 代码库进行分析并上传sonarqube平台:mvn sonar:sonar
PS:
1 | 如果代码库pom文件配置如下 |
Sonar-scanner
适合所有项目
- 下载Sonar-scanner,设置环境变量,配置
/conf/sonar-scanner.properties 1
2
3
4
5
6
7#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here
#----- Default SonarQube server
sonar.host.url=http://ip:9000
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
sonar.login=个人账户生成的token - 在项目根目录增加sonar-project.properties,内容如下:
1
2
3
4
5
6
7
8
9
10
11# must be unique in a given SonarQube instance
sonar.projectKey=ggg:aaa #对应的项目key为ggg:aaa
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=nnn #对应的项目名称为nnn
sonar.projectVersion=1.0.0 #对应的版本为:1.0.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
sonar.java.binaries=target/classes #配置为项目具体的class目录,如果不配置的话只进行源代码分析
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8 - 执行扫描分析:
Build(可以不执行构建)
sonar-scanner
API
点击:http://ip:9000/web_api/ ,查看Sonarqube支持的web_api。
以Python2.7 为例简单介绍api使用:
1 | #! /usr/bin/env python |