Jenkins安裝、配置,常用中介軟體、外掛構建例項
1. 安裝前準備
JDK、Resin、Weblogic、Maven
2. 安裝配置Jenkins
2.1 安裝Jenkins
部署jenkins.war到Tomcat、Resin等中介軟體,本文以Resion為例,複製jenkins.war到resin的webapps中進行部署。
2.2 系統設定
啟動Resin,在瀏覽器中訪問http://ip:埠/jenkins/。
點選【系統管理】-【系統設定】,進行系統設定。
主目錄路徑可通過新建JENKINS_HOME系統變數修改
為避免構建後啟動的子程序關閉,可通過設定全域性環境變數BUILD_ID=dontKillMe禁止關掉子程序,該變數也可設定在具體某個專案中。
配置Maven Filepath
配置Jenkins URL
配置SVN版本,1.7以上版本選擇1.7即可。
2.3 外掛管理
點選【系統管理】-【管理外掛】
在外掛管理頁面可看到【可更新】、【可選外掛】、【已安裝】、【高階】四個選項卡,用於管理外掛。
安裝外掛的兩種方式:
一、線上安裝
在可選外掛中選擇要安裝的外掛,點選【直接安裝】,線上安裝外掛。
二、離線安裝
外掛下載地址:http://updates.jenkins-ci.org/download/plugins/,下載需要的離線外掛,副檔名為hpi。
在高階-上傳外掛中,點選【選擇檔案】選擇下載的離線外掛,點選【上傳】,上傳並安裝離線外掛。
2.4 建立List View
預設檢視的型別是“ALL”,列不能編輯,想要可編輯列,需要建立型別為“List View”的檢視。
新增檢視顯示的Job
刪除不需要顯示的列
新增需要顯示的列
設定預設檢視,只有非預設的檢視才可以刪除。
選擇預設檢視
刪除非預設檢視
3. Jenkins配置例項
以下例項均在Windows+Maven+SVN環境中配置。
3.1 構建Resin工程
左側選單中點選【新建】
輸入專案名稱,選擇“構建一個maven專案”
勾選“丟棄舊的構建”,保持構建的天數和最大個數根據需要設定。
原始碼管理中選擇Subversion,在Repositroy URL中輸入svn連結,第一次輸入時會提示無法訪問,點選【enter credential】,進入驗證頁面。
選擇Username/password authentication,輸入svn賬戶和密碼。
在Pre Steps中點選Addpre-build step,選擇Execute Windows batch command,輸入在maven build前要執行的命令。
::停止server為xxx的例項
D:\programs\resin-3.1.15\httpd.exe -server xxx stop
::延遲40秒,等待例項停止結束
C:\Windows\System32\ping -n 40 127.0.0.1>nul
在Build中輸入執行的maven命令
在Post Steps中點選Addpost-build step,選擇Execute Windows batch command,輸入在maven build後要執行的命令。
::刪除部署目錄下的web應用
rd/s/q D:\programs\resin-3.1.15\webapps-xxx\xxx
::複製maven build後的war到resin的部署路徑
copyD:\programs\jenkins\workspace\xxx-windows-resin\target\xxx.war
D:\programs\resin-3.1.15\webapps-xxx\xxx.war
::啟用server為xxx的例項
D:\programs\resin-3.1.15\httpd.exe -server xxxstart
::延遲40秒等待例項啟動成功
C:\Windows\System32\ping -n 40 127.0.0.1>nul
在Jenkins首頁點選如下圖示即可構建專案。
3.2 構建Weblogic工程
在外掛管理中搜索Publish Over SSH並安裝
在系統設定中配置Publish over SSH資訊
新建專案,除Pre Steps、Post Steps外,其他配置與構建Resin一致,可參考【3.1 構建Resin工程】。
Pre Steps設定:
Name:系統設定中Publish over SSH中的Name。
Exec command:要執行的命令
#切換至weblogic域目錄
cd/usr/oracle/weblogic/user_projects/domains/xxx_domain/bin
#停止服務
./stopWebLogic.sh
#刪除應用目錄xxx
rm -rf /var/xxx
#刪除xxx.war
rm -f /var/xxx.war
Post Steps設定:
Name:系統設定中Publish over SSH中的Name。
Source files:要上傳到Liunx的檔案,主要要有路徑。
Remove prefix:Source files中檔名以外的內容。
Remote directory:要上傳至Linux的目錄。
Exec command:要執行的命令
#切換至var
cd /var
#解壓war至xxx目錄
unzip -oq xxx.war -d xxx
#切換至weblogic域目錄
cd /usr/oracle/weblogic/user_projects/domains/xxx_domain
#啟動weblogic服務
./startWebLogic.sh
3.3 構建Tomcat工程
新建專案,除Pre Steps、Post Steps外,其他配置與構建Resin一致,可參考【3.1 構建Resin工程】。
Pre Steps設定:
::停止服務
D:\programs\tomcat\tomcat-instances\xxx-ins\stop.bat
::延遲30秒,等待停止完成
C:\Windows\System32\ping-n 30 127.0.0.1>nul
Post Steps設定:
::刪除舊xxx應用目錄
rd/s/q D:\programs\tomcat\tomcat-instances\xxx-ins\webapps\xxx
::延遲10秒,等待刪除完成
C:\Windows\System32\ping-n 10 127.0.0.1>nul
::複製svn中的xxx至部署目錄,uncopy.txt為排除複製的檔案列表檔案
echo d |C:\Windows\System32\xcopy
D:\programs\jenkins\workspace\xxx-windows-tomcat\target\xxx D:\programs\tomcat\tomcat-instances\xxx-ins\webapps\xxx/e/q/y/exclude:D:\programs\jenkins\workspace\xxx-windows-tomcat\uncopy.txt
::延遲10秒,等待複製完成
C:\Windows\System32\ping-n 10 127.0.0.1>nul
::啟動服務
D:\programs\tomcat\tomcat-instances\xxx-ins\start.bat
::延遲30秒,等待啟動完成
C:\Windows\System32\ping-n 30 127.0.0.1>nul
3.4 CheckStyle外掛安裝配置
在pom.xml中新增checkstyle外掛<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.15</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<configuration>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.3</version>
</plugin>
</plugins>
</reporting>
在【系統管理】-【管理外掛】中新增Checkstyle Plug-in。
專案設定除下面2項外,其它請參考【3.1 構建Resin工程】配置,
Build中輸入checkstyle:checkstyle
構建設定中勾選“Publish Checkstyle analysis results”
構建後點擊【Checkstyle Warnings】即可看到Checkstyle檢查結果
3.5 FindBugs外掛安裝配置
在pom.xml中新增findbugs外掛<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<effort>Default</effort>
<findbugsXmlOutputDirectory>target/site</findbugsXmlOutputDirectory>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.5.5</version>
</plugin>
</plugins>
</reporting>
在【系統管理】-【管理外掛】中新增FindBugs Plug-in。
Build中輸入clean compile findbugs:findbugs
構建設定中勾選“Publish FindBugs analysis results”
構建後點擊【FindBugs Warnings】即可看到FindBugs檢查結果
3.6 PMD外掛安裝配置
在pom.xml中新增PMD外掛
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.2</version>
<configuration>
<rulesets>
<ruleset>${basedir}/src/test/resources/code-analysis-rules/pmd-java.xml</ruleset>
</rulesets>
</configuration>
</plugin>
</plugins>
</pluginManagement>
在【系統管理】-【管理外掛】中新增PMD Plug-in。
Build中輸入clean pmd:pmd
構建設定中勾選“PublishPMD analysis results”
構建後點擊【PMDWarnings】即可看到FindBugs檢查結果
3.7 Static Analysis Collector外掛安裝配置
Static Analysis Collector 可以整合Checkstyle、FindBugs和PMD的檢查結果。在【系統管理】-【管理外掛】中新增Static Analysis Collector Plug-in。
Build中輸入clean compile findbugs:findbugs checkstyle:checkstyle pmd:pmd
在增加構建後操作步驟中選擇“Publish combined analysis results”
在Publish combined analysis results中勾選Checkstyle、FindBugs、PMD warnings
構建後點擊【Static Analysis Warnings】即可看到整合後的檢查結果
3.8 構建Sonar
執行Sonar
Build中輸入clean sonar:sonar
瀏覽器中輸入http://ip:埠,訪問sonar,即可看到分析結果。
3.9 JaCoCo外掛安裝配置
在pom.xml中新增JaCoCo外掛</plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.4.201502262128</version>
</plugin>
</plugins>
</pluginManagement>
在【系統管理】-【管理外掛】中新增JaCoCo外掛。
在Pre Steps中輸入如下命令:
::刪除不做統計的xxx包的java程式碼
rd/s/qD:\programs\jenkins\workspace\xxx-junit\src\main\java\com\xxx\xxx\xxx
在Build中輸入clean install
在增加構建後操作步驟中選擇Record JaCoCo coverage report。
根據需要新增配置健康報告閾值
構建後點擊【Coverage Report】即可看到覆蓋率報告