sonarQube

sonarQube


Sonarqube搭建

1、安裝

這裏準備的是***sonarqube7.7.zip***,我的安裝路徑是/u02/ycc

使用unzip解壓壓縮包;

預置條件

1).已安裝JAVA環境

2).已安裝有MySQL數據庫

3).sonarQube壓縮包

2、數據庫配置:

# 創建數據庫sonar
create database sonar character set utf8 collate utf8_general_ci;

# 創建數據庫用戶sonar可用地址爲192.168.6.226密碼sonar
CREATE USER sonar@'192.168.6.226' identified by 'sonar';

# 賦權給用戶sonar對數據庫sonar有所有權限
grant all on sonar.* to 'sonar'@'%' identified by 'sonar';

# 授權sonar用戶可以在本地連接數據庫
grant all on sonar.* to 'sonar'@'localhost' identified by 'sonar';

# 授權sonar用戶可以在226連接數據庫
grant all on sonar.* to 'sonar'@'192.168.6.226' identified by 'sonar';

# 刷新權限
flush privileges;

在這裏插入圖片描述

對權限的所有操作最後需要刷新下權限,即flush privileges;使之更改立馬生效。

3、修改sonar配置文件:sonar.properties

我的數據庫在17,使用時更改這個地址到自己的數據庫地址即可。

sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://192.168.6.17:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL

修改wrapper配置文件的java路徑

wrapper配置文件額sonar.properties在同一個目錄裏,這裏需要注意一點,路徑後面需要額外加上/java。不加會報

Unable to start JVM: Permission denied (13)的錯誤。

wrapper.java.command=/u02/ycc/jdk1.8.0_161/bin/java

啓動

sh /u02/ycc/sonar.../bin/linux.../sonar.sh start

如果使用root的話會出現如下錯誤:

在這裏插入圖片描述

在這裏插入圖片描述

換個用戶,並賦予這個用戶sonar目錄的權限即可。

4、啓動sonarqube

將中文插件sonar-l10n-zh-plugin-1.28.jar複製到extensions/plugins

在這裏插入圖片描述

5、安裝SonarQube Scanner和配置

//解壓文件
//進入文件
//編輯文件
[root@localhost local]#unzip  sonar-scanner-cli-3.0.3.778-linux.zip
[root@localhost local]#mv sonar-scanner-cli-3.0.3.778-linux  sonar-scanner
[root@localhost local]# cd sonar-scanner
[root@localhost sonar-scanner]# vim conf/sonar-scanner.properties 
#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
 
sonar.host.url=http://192.168.6.226:9000
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://192.168.6.17:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.login=sonar
sonar.password=sonar

在項目的根目錄創建sonar-project.properties

#sonar登陸用戶
sonar.login=admin
#sonar登陸密碼
sonar.password=admin
#需要掃描的項目對應的key自定義即可
sonar.projectKey=content-receive
#需要掃描的項目對應的顯示項目名自定義即可
sonar.projectName=content-receive
sonar.projectVersion=1.0-SNAPSHOT
sonar.sourceEncoding=UTF-8
sonar.language=java
#掃描的源碼位置
sonar.sources=src/main/java/com/jsc/content
#掃描的test位置
sonar.tests=src/test/java/com/jsc/content
#掃描java的源碼位置
sonar.java.binaries=target/classes/com/jsc/content

在項目當前目錄執行scanner :

sh /sonarscannerdir/bin/sonar-scanner -X

運行結束在sonarQube頁面即可看到剛纔掃描的項目。

在這裏插入圖片描述

6、maven-sonar-plugin

對於Maven項目,除了使用SonarQube Scanner進行分析之外,還可以使用maven-sonar-plugin插件進行分析。使用maven-sonar-plugin插件的步驟如下:(setting.xml)

在這裏插入圖片描述

在這裏插入圖片描述

	<profile>
		<id>sonar</id>
		<activation>
		<activeByDefault>true</activeByDefault>
		</activation>
		<properties>
		<sonar.jdbc.url>jdbc:mysql://192.168.6.226:3306/sonar</sonar.jdbc.url>
		<sonar.jdbc.username>sonar</sonar.jdbc.username>
		<sonar.jdbc.password>sonar</sonar.jdbc.password>
			<sonar.host.url>http://192.168.6.226:9000</sonar.host.url>
		</properties>
		
		</profile> 
  </profiles>
   <activeProfiles>
        <activeProfile>UFindNexus</activeProfile>
		<activeProfile>sonar</activeProfile>
    </activeProfiles>

pom.xmlbuild中增加如下配置:

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

在對應項目的控制檯輸入mvn clean verify sonar:sonar
或mvn clean install org.sonarsource.scanner.maven:sonar-maven-plugin:3.1.1:sonar執行掃描;這裏注意,如果有多個maven的setting.xml會使用環境變量配置的setting.xml。執行完即可在sonarqube頁面查看。

如果這裏執行報錯的話可以使用IDEA的run maven運行:

在這裏插入圖片描述

也可以在pom.xml中增加profile,此時選中sonar-project,執行 clean install sonar:sonar即可。

    <profiles>
        <profile>
            <id>sonar-project</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <sonar.jdbc.url>jdbc:mysql://192.168.6.213:3306/sonar</sonar.jdbc.url>
                <sonar.jdbc.username>root</sonar.jdbc.username>
                <sonar.jdbc.password>passok</sonar.jdbc.password>
                <sonar.host.url>http://192.168.6.213:9000</sonar.host.url>
                <!-- 需要忽略的-->
                <sonar.exclusions>src/main/java/com/jsc/codec/**</sonar.exclusions>
            </properties>
        </profile>
    </profiles>

[外鏈圖片轉存失敗(img-z2wEKtpp-1564126546810)(C:\Users\Administrator.PC-201901241237\AppData\Roaming\Typora\typora-user-images\1564121973140.png)]


Sonarqube使用

SonarQube 是一個開源的代碼分析平臺, 用來持續分析和評測項目源代碼的質量。 通過SonarQube我們可以檢測出項目中重複代碼, 潛在bug, 代碼規範,安全性漏洞等問題, 並通過SonarQube web UI展示出來。

在這裏插入圖片描述

1.SonarQube掃描方法

Jenkins中調用
通過jenkins插件調用sonarScanner或使用Maven、Gradle等內置掃描器
依據項目需要,對代碼持續掃描,並將結果推送到sonarqube 進行頁面展示

SonarQube Scanner
使用scanner,通過配置文件,修改項目信息,在命令行中調用scanner工具,進行掃描,並推送給sonarqube

Maven、Gradle等內置掃描器

以maven爲例,需要修改maven和sonarqube配置文件,在mvn編譯後,使用mvn命令,進行代碼掃描,並推送給sonarqube(需要編譯源代碼) ,參見上文。

2.SonarQube web UI

顯示用戶所有的項目概況,各項目質量評級,並提供條件篩選

在這裏插入圖片描述

3.SonarQube web UI –項目頁面

通過在主頁面選擇單個項目,進入項目詳情,該頁面提供了當前項目最近一次掃描的結果評級,歷史累計和新增問題數量,代碼行數等信息 。

在這裏插入圖片描述

4.SonarQube web UI –問題頁面

提供當前用戶名下所有問題的列表,並提供條件篩選,包括問題類型,嚴重程度等
在當個項目中,問題頁面顯示單項目信息 。

在這裏插入圖片描述

選中單個問題,查看問題代碼詳情,sonarqube給出問題描述和修改意見 。

在這裏插入圖片描述

5.SonarQube web UI –評估頁面

給出當前項目的評估概況信息,大小,可靠性,重複率,覆蓋率等 。

在這裏插入圖片描述

6.SonarQube web UI –代碼頁面

以.java文件爲依據,給出各個.java文件統計信息 。

在這裏插入圖片描述

7.SonarQube web UI –活動頁面

頁面展示了每次代碼掃描的基本信息和代碼情況的折線圖,折線圖可以根據需要調整顯示bugs數量,代碼行數,覆蓋率等信息 。
在這裏插入圖片描述


SonarQube Jekins集成

1、安裝jenkins sonar插件。

2、配置sonarServer

進入系統管理–>系統配置界面。(這裏選擇測試環境的sonarQube地址)

在這裏插入圖片描述

進入系統管理–>全局工具配置

在這裏插入圖片描述

回到主頁找到需要配置的項目,如果沒有則需要新建項目,這裏不贅述如何創建。選中項目配置sonar(這裏使用

sonar-scanner)。

在這裏插入圖片描述

在這裏插入圖片描述

在構建歷史中可以看到運行中的構建,點進去查看信息:

在這裏插入圖片描述

另外一種方式是直接使用maven命令打包,此時需要配置setting.xml,配置見前文。

在這裏插入圖片描述

3、查看結果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章