Sonar6.0應用之二:Sonar Web介面配置及與Runner、Scanner整合進行命令列程式碼分析

來源:互聯網
上載者:User

標籤:sonar   runner   scanner   

 

一、安裝好了SonarQube服務端後,在其它電腦的瀏覽器上登陸,開始安裝其它程式設計語言檢測外掛程式

650) this.width=650;" title="image" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/89/13/wKiom1gG7ALQV7knAACQ0Rhva1k861.png" width="1093" height="672" />

650) this.width=650;" title="image" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/89/10/wKioL1gG7APgTNNLAAECchzKu3s111.png" width="1079" height="660" />

系統已經裝好的語言外掛程式:

650) this.width=650;" title="image" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M02/89/10/wKioL1gG7AWB_VtNAAEWsJ2QN3U317.png" width="1078" height="664" />

650) this.width=650;" title="image" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/89/13/wKiom1gG7AfiN_2HAAGQSl0l7hA667.png" width="1079" height="660" />

下載了軟體項目中常用的語言:Android、CSS、Web、XML

JAVA相關的:Checkstyle、Findbugs、PMD

Java 靜態分析工具分析對象

應用技術

Checkstyle

Java 源檔案,缺陷模式比對

FindBugs

位元組碼,缺陷模式比對;資料流分析

PMD

Java 原始碼,缺陷模式比對

下載完分析語言規則後,重啟服務

650) this.width=650;" title="image" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;margin:0px;padding-right:0px;border-top-width:0px;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M02/89/13/wKiom1gG7AnDT0TNAAFGj3TKGOs414.png" width="1077" height="667" />

 

二、安裝命令列分析端

sonar的命令列分析端軟體有兩種分別是Runner和Scanner,官網文檔中寫的是Scanner,但Runner和它安裝、使用都基本一致。

1、在CentOS上安裝sonar-runner-dist-2.4

cd /usr/local/src/

wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip

unzip sonar-runner-dist-2.4.zip

mv sonar-runner-2.4/ /usr/local/

  • 配置PATH路徑

vim /etc/profile

在檔案最後加入如下內容,儲存並退出。

PATH=$PATH:/usr/local/sonar-runner-2.4/bin  
export PATH

  • 配置sonar-runner啟動設定檔

vim /usr/local/sonar-runner-2.4/conf/sonar-runner.properties

把下面內容前#號去掉或增加後,儲存並退出

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8  
sonar.jdbc.username=sonar  
sonar.jdbc.password=sonar  
sonar.host.url=http://192.168.1.190    
sonar.login=admin  
sonar.password=admin

  • 安裝成功後重啟伺服器,在命令列運行以上命令並回顯,表示運行成功。

[[email protected] local]# sonar-runner -h  
INFO:    
INFO: usage: sonar-runner [options]    
INFO:    
INFO: Options:    
INFO:  -D,--define <arg>     Define property    
INFO:  -e,--errors           Produce execution error messages    
INFO:  -h,--help             Display help information    
INFO:  -v,--version          Display version information    
INFO:  -X,--debug            Produce execution debug output

 

2、在CentOS上安裝sonar-scanner2.8

cd /usr/local/src/

wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip

unzip sonar-scanner-2.8.zip

mv sonar-scanner-2.8/ /usr/local/

  • 配置PATH路徑

vim /etc/profile

在檔案最後加入如下內容,儲存並退出。

PATH=$PATH:/usr/local/sonar-runner-2.4/bin:/usr/local/sonar-scanner-2.8/bin  
export PATH

  • 配置sonar-scanner啟動設定檔

vim /usr/local/sonar-scanner-2.8/conf/sonar-scanner.properties

把下面內容前#號去掉或增加後,儲存並退出

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8  
sonar.jdbc.username=sonar  
sonar.jdbc.password=sonar  
sonar.host.url=http://192.168.1.190    
sonar.login=admin  
sonar.password=admin

  • 安裝成功後重啟伺服器,在命令列運行以上命令並回顯,表示運行成功。

[[email protected] local]# sonar-scanner -h  
INFO:    
INFO: usage: sonar-scanner [options]    
INFO:    
INFO: Options:    
INFO:  -D,--define <arg>     Define property    
INFO:  -h,--help             Display help information    
INFO:  -v,--version          Display version information    
INFO:  -X,--debug            Produce execution debug output    
INFO:  -i,--interactive      Run interactively

 

三、把開發程式的原始碼打包成zip檔案上傳到安裝有Runner或Scanner的伺服器上

650) this.width=650;" title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/89/13/wKiom1gG7AyC0hQ3AAHuasZl-Js249.png" height="748" />

解壓上傳的原始碼:

cd /usr/local/

unzip whale.zip

 

四、使用sonar-scanner進行代碼品質分析

1、在伺服器上建立一個準備用Scanner執行的設定檔

cd whale/

vim sonar-project.properties

2、建立檔案內容如下:

# must be unique in a given SonarQube instance  
sonar.projectKey=whale:scanner        
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.    
sonar.projectName=whale-scanner    
sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.  
# Since SonarQube 4.2, this property is optional if sonar.modules is set.    
# If not set, SonarQube starts looking for source code from the directory containing    
# the sonar-project.properties file.    
sonar.sources=.

# Encoding of the source code. Default is default system encoding  
#sonar.sourceEncoding=UTF-8

3、儲存並退出後運行命令進行分析(分析中不能執行Findbugs3.4.3分析,在web端卸載這個規則後可以正常分析):

sonar-scanner

4、在web中查看Scanner代碼品質分析的結果。

650) this.width=650;" title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/89/13/wKiom1gG7A7irTvAAAE14Gi2lzo261.png" height="880" />

 

五、使用sonar-Runner進行代碼品質分析

1、修改下Scanner執行時的設定檔

cd /usr/local/whale/

vim sonar-project.properties

2、修改檔案內容如下:

# must be unique in a given SonarQube instance  
sonar.projectKey=whale:runner    
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.    
sonar.projectName=whale-runner    
sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.  
# Since SonarQube 4.2, this property is optional if sonar.modules is set.    
# If not set, SonarQube starts looking for source code from the directory containing    
# the sonar-project.properties file.    
sonar.sources=.

# Encoding of the source code. Default is default system encoding  
#sonar.sourceEncoding=UTF-8

3、儲存並退出後運行命令進行分析(分析中不能執行Findbugs3.4.3分析,在web端卸載這個規則後可以正常分析):

sonar-runner

4、在web中查看runner代碼品質分析的結果。

650) this.width=650;" title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/89/10/wKioL1gG7A_C24xiAAE-vS5vSEU518.png" height="904" />

 

結果一樣,證明Runner和Scanner功能差不多。

本文出自 “堅強的技術交流blog” 部落格,請務必保留此出處http://newthink.blog.51cto.com/872263/1863334

Sonar6.0應用之二:Sonar Web介面配置及與Runner、Scanner整合進行命令列程式碼分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.