maven+jenkins+sonar集成搭建
轉載地址:http://blog.csdn.net/wzd_0116/article/details/24651333
集成質量管理平臺搭建
?
第一章?Sonar安裝...?2
1.1下載sonar.?2
1.2安裝sonar.?2
1.2.1 JDK安裝配置...?2
1.2.2數據庫安裝...?2
1.3成功界面...?3
1.4配置插件...?3
1.4.1插件介紹...?3
1.4.2常用的插件:...?4
第二章?Maven安裝...?5
2.1下載?Maven.?5
2.2配置Maven.?5
第三章?Jenkins安裝...?5
3.1安裝Jenkins.?5
第四章?Sonar-Runner安裝...?6
4.1下載Sonar-Runner.?6
4.2配置Sonar-Runner環境變量...?6
4.3測試文件的配置...?6
4.4應用sonar監測代碼質量...?7
第五章?環境集成...?7
5.1 Sonar和Jenkins集成...?7
5.2 Jenkins系統配置...?9
5.3 Jenkins項目配置...?10
第六章?常見錯誤...?14
6.1 sonar沒有啟動...?14
6.2 Maven setting.xml配置...?15
6.3??文件POM.xml配置...?16
6.4??0 files indexd.?16
??
第一章?Sonar安裝
1.1下載sonar
?????最新版版本:4.2,穩定版:3.7(本人建議下載3.7穩定版)
?????下載地址:http://www.sonarsource.org/downloads/
下載完成之后,要注意一個問題,官方的說明是不要安裝在以數字開頭的目錄中。所以我們就不要解壓在數字開頭的目錄中。
1.2安裝sonar
1.2.1 JDK安裝配置
這個不予多說,最基本的JAVA開發環境配置,下載JDK,配置其JAVA_HOME,PATH等。
1.2.2數據庫安裝
(1)本人是選擇的MySQL,這里要注意的是如果你下載的是免安裝版,直接解壓使用的話,需要配置my.ini。
(2)在數據庫中創建新用戶sonar?密碼為sonar并給用戶賦權。(用戶和密碼可以自定義,如果自定義需要更改sonar-3.7\conf\sonar.properties配置文件中得sonar.jdbc.username與sonar.jdbc.password,其默認的就是sonar)。
(3)配置sonar.properties文件。
a.配置啟動的http端口:
sonar.web.host:???????????????????????????localhost(或者你的IP)
sonar.web.port:???????????????????????????9000(默認是9000,你可以修改,不然會和Jenkins的端口號沖突)
sonar.web.context:????????????????????????/
????(三句前本來被注釋,取消注釋)
??b.取消MySQL連接的注釋,并輸入自己數據庫的url地址,同時將默認的sonar.jdbc.url屏蔽
#----- MySQL 5.x
# Comment the embeddeddatabase and uncomment the following line to use MySQL
sonar.jdbc.url:?????????????????????????jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
# :mysql是指你要連接的事Mysql數據庫,這里我給Sonar的權限是管理員所以可以用localhost,否則你要使用的你的IP地址,3306是你使用的端口號,test是我測試時創建的數據庫,你需要改成你自己的數據庫名。
# Optionalproperties?
sonar.jdbc.driverClassName:??com.mysql.jdbc.Driver
sonar.jdbc.validationQuery:??select 1
c.進入bin中,選擇適合自己系統,運行sonar.sh,如果是windows運行bat。運行sonar-3.7\bin\windows-x86-64\StartSonar.bat,打開相應的網頁:http://localhost:9000測試是否配置成功,這里的頁面鏈接跟前頭的http配置有關。(第一次運行StartSonar.bat,時需要的時間較長,請耐心等待,切不可關閉窗口重新加載,sonar其需要創建一個和Mysql數據庫相關的migrating數據庫和表,如果中途退出則創建不成功!)
1.3成功界面
登陸用戶名和密碼都是admin
??????????????????????????
1.4配置插件
1.4.1插件介紹
Sonar支持多種插件,插件的下載地址為:http://docs.codehaus.org/display/SONAR/Plugin+Library
將下載后的插件上傳到${SONAR_HOME}extensions\plugins目錄下,重新啟動sonar。
sonar默認集成了Java Ecosystem插件,該插件是一組插件的合集
(1)Java [sonar-java-plugin]:java源代碼解析,計算指標等
(2)Squid [sonar-squid-java-plugin]:檢查違反Sonar定義規則的代碼
(3)Checkstyle [sonar-checkstyle-plugin]:使用CheckStyle檢查違反統一代碼編寫風格的代碼
(4)FindBugs [sonar-findbugs-plugin]:使用FindBugs檢查違反規則的缺陷代碼
(5)PMD [sonar-pmd-plugin]:使用pmd檢查違反規則的代碼
(6)Surefire [sonar-surefire-plugin]:使用Surefire執行單元測試
(7)Cobertura [sonar-cobertura-plugin]:使用Cobertura獲取代碼覆蓋率
(8)JaCoCo [sonar-jacoco-plugin]:使用JaCOCO獲取代碼覆蓋率
?
1.4.2常用的插件:
(1)JavaScript代碼檢查:http://docs.codehaus.org/display/SONAR/JavaScript+Plugin
(2)python代碼檢查:http://docs.codehaus.org/display/SONAR/Python+Plugin
(3)Web頁面檢查(HTML、JSP、JSF、Ruby、PHP等):http://docs.codehaus.org/display/SONAR/Web+Plugin
(4)xml文件檢查:http://docs.codehaus.org/display/SONAR/XML+Plugin
(5)scm源碼庫統計分析:http://docs.codehaus.org/display/SONAR/SCM+Stats+Plugin
(6)文件度量:http://docs.codehaus.org/display/SONAR/Tab+Metrics+Plugin
(7)中文語言包:http://docs.codehaus.org/display/SONAR/Chinese+Pack
(8)時間表顯示度量結果:http://docs.codehaus.org/display/SONAR/Timeline+Plugin
(9)度量結果演進圖:http://docs.codehaus.org/display/SONAR/Motion+Chart+Plugin
(b)插件配置示例(本段內容來自http://www.ibm.com/developerworks/cn/java/j-lo-sonar/
?
?
?
?
?
?
?
?
?
?
?
第二章Maven安裝
2.1下載?Maven
最新版:Apache Maven 3.2.1
下載地址???http://maven.apache.org/download.cgi
2.2配置Maven
Maven的配置也比較簡單:MAVEN_HOME=?${ Maven.home }??PATH=% MAVEN_HOME %\bin
測試maven是否成功?:mvn –v
第三章?Jenkins安裝
3.1安裝Jenkins
下載地址:http://jenkins-ci.org/content/thank-you-downloading-windows-installer
Jenkins安裝比較簡單在這里就不多說了,其最主要的就是Jenkins集成Maven和Sonar。
3.2安裝后打開http://localhost:9000/進行測試
3.3成功界面:
第四章?Sonar-Runner安裝
4.1下載Sonar-Runner
下載地址:http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-runner
4.2配置Sonar-Runner環境變量
(1)SONAR_RUNNER_HOME????????????你的安裝位置
(2)Path????????????${SONAR_RUNNER_HOME}/bin (Unix) or%SONAR_RUNNER_HOME%/bin
(3)修改${SONAR_RUNNER_HOME}/conf/sonar-runner.properties文件,打開database connection, server URL的注釋
4.3測試文件的配置
在每個項目的項目源文件目錄下新建一個文件名為sonar-project.properties的文件,在文件中輸入以下內容:
# required metadata
# My project------修改成你的項目名稱
sonar.projectKey=my:project
sonar.projectName=My project
sonar.projectVersion=1.0
# path to source directories (required)
# srcDir1,srcDir2---------修改成你的源文件夾路徑
sonar.sources=srcDir1,srcDir2??#通常就填src
# path to test source directories (optional)
# testDir1,testDir2--------修改成你的測試文件夾路徑
tests=testDir1,testDir2
# path to project binaries (optional), for example directory of Java#bytecode
# binDir--------修改成你的二進制文件夾路徑
# binaries=binDir????#此為可選項,可以不填
# path to project libraries (optional)
#libraries=junit.jar??????#此為可選項,可以不填
# advanced parameters
sonar.my.property=value
4.4應用sonar監測代碼質量
以上配置完成之后,就可以應用sonar來監測代碼質量了。
(1)先啟動sonar用sonar-3.7\bin\windows-x86-64\StartSonar.bat文件,這時可以查看sonar-3.7\logs\ sonar.log文件,看是否已經啟動sonar
(2)啟動好sonar之后,接著在命令行中切換到項目文件的目錄下,然后輸入sonar-runner,等到運行結束后,進入到http://localhost:9000頁面,查看代碼的質量統計結果。
需進入項目的目錄中:
成功時如圖所示:
第五章?環境集成
5.1Sonar和Jenkins集成
5.1.1通過Maven進行集成
修改maven的主配置文件(${MAVEN_HOME}/conf/settings.xml文件或者~/.m2/settings.xml文件),在其中增加訪問Sonar數據庫及Sonar服務地址,添加如下配置:
?
?
<profile>
<id>sonar</id>
<properties>
<sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url>
<sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>
<sonar.host.url>http://localhost:9000</sonar.host.url><!-- Sonar服務器訪問地址?-->
</properties>
</profile>
<activeProfiles>
<activeProfile>sonar</activeProfile>
</activeProfiles>
?
此處注意sonar.host.url地址應根據sonar部署情況修改
同樣,為了避免內存溢出,推薦增加內存堆棧的大小。設置MAVEN_OPTS環境變量:
set?MAVEN_OPTS=”-Xmx512m -XX:MaxPermSize=256m”
使用Sonar
(1)?運行Sonar服務器;
(2)?通過?mvn sonar:sonar?將代碼注入到Sonar中進行分析處理,并將處理結果以XML的形式保存在數據庫中;
(3)通過瀏覽器訪問,顯示分析結果;
(4)持續運行Maven構建,會迭代顯示分析結果;
(5)可以顯式指定sonar插件的版本,如下:
<project>
???<build>
???????<plugins>
???????????<plugin>
???????????????<groupId>org.codehaus.sonar</groupId>
???????????????<artifactId>sonar-maven-plugin</artifactId>
???????????????<version>3.5.1</version>
???????????</plugin>
??????</plugins>
?????</build>
</project>
?
(6)可以顯式的將sonar綁定到Maven生命周期中,如下:
?
<plugin>?
??<groupId>org.codehaus.sonar</groupId>
??????<artifactId>sonar-maven-plugin</artifactId>
???????<version>3.5.1</version>
???????<executions>
??????????<execution>
????????????<id>sonar</id>
??????????<phase>site</phase>
??????????<goals>
????????????<goal>sonar</goal>
??????????</goals>
?????????</execution>
??</executions>
</plugin>
?
5.1.2下載sonar jenkins plugin
在jenkins的插件管理中選擇安裝sonar jenkins plugin,該插件可以使項目每次構建都調用sonar進行代碼度量。
?
打開Jenkins,選擇系統管理,然后進入管理插件,找到sonar plugin,下載安裝。
?
5.2Jenkins系統配置
5.2.1 JDK配置
5.2.2 sonar runner?配置
?
5.2.3 Maven配置
5.2.4 Sonar?配置
5.3Jenkins項目配置
進入你創建的項目,我這里創建的項目名是scmp,進入后,找到左邊的配置,進入。
5.3.1源碼管理
選擇你的源碼來源,我用的是SVN,選擇?Subversion選擇框,里面要配置你的源碼URL,登錄名和密碼,其中Repository depth選擇infinity,Local moduledirectory是你導出文件的位置,此位置是相對于Jenkins的,在Jenkins的workspace里。如圖所示:
5.3.2 Build
其中的root Pom選擇是相對于workspace的,即是在workspace里里面。?如圖:
5.3.3 Post Steps
如圖操作:
選擇Maven targets
然后里面的設置如圖:
5.3.4?構建后操作
添加構建后操作步驟:
此處只需要填寫下JDK就可以用了,應用程序構建時就會自動觸發Sonar對代碼的檢查
?
成功效果圖:
第六章?常見錯誤
6.1sonar沒有啟動
錯誤如下:
[ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.2:sonar (default-cli) on project scmp: SonarQube server can not be reached at http://localhost:9000. Please check the parameter 'sonar.host.url'. Connection refused: connect -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [JENKINS] Archiving D:\Program Files\Jenkins\workspace\scmp\scmp\pom.xml to com.fnii.scmp/scmp/0.0.1-SNAPSHOT/scmp-0.0.1-SNAPSHOT.pom [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException An attempt to send an e-mail to empty list of recipients, ignored. channel stopped Skipping sonar analysis due to bad build status FAILURE An attempt to send an e-mail to empty list of recipients, ignored. Finished: FAILURE?
解決方法:應先運行sonar,在運行Jenkins進行構建項目
6.2 Maven setting.xml配置
錯誤如下:?
?[ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.3.1:sonar (default-cli) on project scmp: SonarQube server can not be reached at http://localhost:9000. Please check the parameter 'sonar.host.url'. Connection refused: connect -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException ? ?解決方法:因為Maven中找不到sonar-maven-plugin配置,將下面文件直接替換即可:
?
<?xml version="1.0"encoding="UTF-8"?>
<settingsxmlns="http://maven.apache.org/SETTINGS/1.0.0"???????????
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"???????????
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0http://maven.apache.org/xsd/settings-1.0.0.xsd">
?<profiles>???????
?<profile>
???<id>sonar</id>
?????<activation>
??????<activeByDefault>true</activeByDefault>
?????</activation>
?????<properties>
????????<!-- EXAMPLE FOR MYSQL -->
???????<sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8</sonar.jdbc.url>????????????????
???????<sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</sonar.jdbc.driverClassName>????????????????
???????<sonar.jdbc.username>sonar</sonar.jdbc.username>????????????????
???????<sonar.jdbc.password>sonar</sonar.jdbc.password>?
????????????????<!-- SERVER ON A REMOTE HOST-->????????????????
????????<sonar.host.url>http://192.168.5.67:9000</sonar.host.url>
?????</properties>
???</profile>
??</profiles>
</settings>
?
6.3?文件POM.xml配置
[ERROR] There are testfailures.
?
Please refer to D:\ProgramFiles\Jenkins\jobs\scmp\workspace\scmp\target\surefire-reports for the individualtest results.
[JENKINS] Recording test results
log4j:WARN No appenders could befound for logger (org.apache.commons.beanutils.converters.BooleanConverter).
log4j:WARN Please initialize thelog4j system properly.
?
在pom.xml的build標簽中添加下面內容:
?????????<plugins>??
???????<plugin>??
????????<groupId>org.apache.maven.plugins</groupId>??
????????<artifactId>maven-surefire-plugin</artifactId>??
????????<version>2.4.2</version>??
?????????<configuration>??
??????????<skipTests>true</skipTests>??
????????</configuration>??
???????</plugin>??
?????</plugins>
?
6.4?0 files indexd
運行Jenkins構建時出現檢測不到文件:0 files indexd時,該錯誤的出現的可能原因有兩個:
1.數據庫出現問題,把相關數據庫中表刪除,重新構建。
2.導出時Repository depth選擇錯誤,應當選擇infinity,將工程中.repository文件刪除,重新構建
總結
以上是生活随笔為你收集整理的maven+jenkins+sonar集成搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 讳字怎么读 讳字简单介绍
- 下一篇: sonar 不再支持 cobertura