内容简介:SonarQube 已經內建 SonarC#,可以直接對 C# 進行檢查,本文將以 .NET Core 為例,並搭配 Jenkins 自動執行 SonarQube。macOS High Sierra 10.13.4SonarQube 7.1
SonarQube 已經內建 SonarC#,可以直接對 C# 進行檢查,本文將以 .NET Core 為例,並搭配 Jenkins 自動執行 SonarQube。
Version
macOS High Sierra 10.13.4
SonarQube 7.1
Jenkins 2.107.1
.NET Core 2.0.7
GitHub
將 .NET Core 專案放到 GitHub。
- 本文將 .NET Core 專案放在
https://github.com/oomusou/Core2SonarQubeJenkins
當然也可以將 git repository 放在不同的 git server,如 Bitbucket
SonarQube
Installation
$ brew install sonarqube
使用 Homebrew 安裝 SonarQube。
brew services start sonarqube sonar console
Start SonarQube
$ sonar console
使用 sonar console 自行啟動 SonarQube。
Test SonarQube
輸入 localhost:9000 ,若看到 SonarQube 首頁,則表示安裝成功
右上角 Log in 可登入管理設定 SonarQube,預設為 admin/admin
SonarQube Scanner for MSBuild
SonarQube 雖然已經包含 SonarC#,但必須靠 SonarQube Scanner for MSBuild 才能執行,預設 SonarQube 並沒有包含 Scanner,必須自行安裝。
其實 SonarQube Scanner for MSBuild 包含 SonarQube Scanner,只是為了 C# 要編譯的特性再包了一層
Download Scanner
- 到 Analyzing with SonarQube Scanner for MSBuild 下載 Scanner,選擇
Download for .NET core 2.0下載。
下載後為一 zip 壓縮檔,解壓縮後可安裝在任何目錄。
將 zip 解開後,放到 home directory 下,並重新命名為 SonarScannerMsbuild 。
重新命名只為了縮短目錄名稱而已
Configuration
SonarQube.Analysis.xml
<Property Name="sonar.host.url">http://localhost:9000</Property> <Property Name="sonar.login">admin</Property> <Property Name="sonar.password">admin</Property>
編輯 ~/SonarScannerMsbuild/SonarQube.Analysis.xml ,修改 sonar.host.url 、 sonar.login 與 sonar.password 三個 property。
SonarQube.Analysis.xml
Test Scanner
$ dotnet ~/SonarScannerMsbuild/SonarScanner.MSBuild.dll begin /k:core2 /n:Core2 /v:1.0
使用 SonarQube Scanner for MSBuild 對 .NET Core 專案進行檢查。
由於 .NET Core 是跨平台, SonarScanner.MSBuild.dll 必須由 dotnet 執行。
-
SonarScanner.MSBuild.dll需指定完整路徑,無法透過$PATH設定 - 需加上
begin,dotnet build必須包在 scanner 內 - /k :SonarQube 對專案的 key,內部將以此 key 作為辨別,必須唯一
- /n :在 SonarQube 網頁上顯示的專案名稱
- /v :在 SonarQube 網頁上顯示的版本編號
- 在專案目錄下使用
SonarScanner.MSBuild.dll檢查 C#
$ dotnet build
使用 dotnet build 編譯專案。
Script 類不用編譯,可以直接使用 SonarQube Scanner 就可以檢查,但 C# 需要編譯,因此必須 dotnet build
dotnet build
$ dotnet ~/SonarScannerMsbuild/SonarScanner.MSBuild.dll end
- 需加上
end,scanner 正式將dotnet build檢查出的結果寫入 SonarQube project
- 在專案目錄下使用
SonarScanner.MSBuild.dll結束檢查
進入 SonarQube 網頁,就可看到 Core2 專案已經出現 SonarQube,也顯示剛剛 dotnet build 檢查出的 1 個 code smell 警告。
到目前為止,SonarQube 對 C# 的檢查已經完成,就算只將 SonarQube 裝在本機,也對 C# 程式碼品質的檢查有很大的幫助。
若能搭配 Jenkins 自動執行 SonarQube,那就更好了。
Jenkins
Installation
$ brew install jenkins
使用 Homebrew 安裝 Jenkins。
-
若想在每次 Mac 重開機就自動執行 Jenkins,輸入
brew services start jenkins -
若想自行啟動 Jenkins,輸入
jenkins
啟動 Jenkins
$ jenkins
使用 jenkins 自行啟動 Jenkins。
Unlock Jenkins
localhost:8080 ~/jenkins/secrets/initialAdminPassword Continue
Customize Jenkins
- 選擇
Install suggested plugins即可
Getting Started
安裝 suggested plugin 中。
Create Admin User
Save and Finish
Jenkins is Ready
- Jenkins 安裝完成,按
Start using jenkins開始使用 Jenkins
Welcome to Jenkins
- 進入 Jenkins 管理介面,如此 Jenkins 已經設定成功
Jenkins Job
Create Job
- 按左側
New Item建立新 job
Freestyle project OK
Source Code Management
- 找到
Source Code Management區段 - 將 Git 的
Repository URL設定到https://github.com/oomusou/Core2SonarQubeJenkins
Build Environment
Build Environment Delete workspace before build starts
Build
Build Add build step Execute shell
- 講以下 command 貼上
dotnet /Users/oomusou/SonarScannerMsbuild/SonarScanner.MSBuild.dll begin /k:core2 /n:Core2 /v:1.0 dotnet build dotnet /Users/oomusou/SonarScannerMsbuild/SonarScanner.MSBuild.dll end
- 按
Save儲存設定
- 按左側
Build Now執行 Job
- Job 執行成功會出現
藍燈
剛剛 Jenkins 執行的 Project 出現在 SonarQube 上。
Conclusion
- SonarQube 已經內建 SonarC#,也可以用來檢查 C# 與 .NET Core
- 就算不將 SonarQube 安裝在 server,安裝在本機也能有效的檢查 C# 程式碼品質
- 藉由 Jenkins 幫忙,我們就可以自動化執行 SonarQube
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Numerical Methods and Methods of Approximation in Science and En
Karan Surana / CRC Press / 2018-10-31
ABOUT THIS BOOK Numerical Methods and Methods of Approximation in Science and Engineering prepares students and other readers for advanced studies involving applied numerical and computational anal......一起来看看 《Numerical Methods and Methods of Approximation in Science and En》 这本书的介绍吧!