隨着工作的要求,devops作爲今年工作的一個重點,由此也引發了自己對於devops相關的工具和技術的學習和實踐。基於上述背景,這個系列將逐步的介紹SVN的安裝和配置、jenkins安裝和配置、reviewboard、findbugs、checkstyle、sonar、testng、mockito等。【每個合格的程序員都是耐操的】
這一節我們來說說Jenkins的集成checkstyle
一、目標:
1、在jenkins中集成checkstyle
2、通過maven 來集成 checkstyle
3、通過ant 來集成 checkstyle
4、瞭解checkstyle 規則
二、環境說明
如前面所述,環境依然是:
1、本機:開發機 192.168.136.1,eclipse
2、虛擬機:win 7 192.168.136.100 SVN服務器
3、虛擬機:centos6.5 192.168.136.144 Jenkins部署服務器
三、實踐過程
基於maven 和基於ant的checkstyle 集成,有所不同, 基於maven的不需要下載checkstyle jar包,maven 就幫你搞定了。基於ant的需要下載jar包,格式文件等。
3.1、 基於maven的方式,集成checkstyle
3.1.1 首先下載checkstyle 插件,參見下圖:
在網絡允許的情況下,可以直接安裝,如果網絡不行,也可以採用下載安裝包,手工加入的方式(網上有介紹)
3.1.2 項目的構建
在項目的構建配置中,在maven處添加 goals項目: checkstyle:checkstyle
3.1.3 構建後的發佈
在構建後的操作中,添加publish ,發佈構建的結果,這裏是發佈 checkstyle的結果:
3.1.4 檢查構建發佈的結果
如果構建成功,如果代碼被檢查出符合規則的bug,就在在構建工程狀態處顯示checkstyle warnnings,如果沒有,說明代碼通過檢測,質量非常好。下圖是checkstyle result 的結果,可以查看檢查出來的問題細節,進行修改。
多次的構建後,還會看到checkstyle的趨勢圖。
3.1.6 自定義規則
可以自己定義檢查的規則,並在編譯配置文件中進行設置: 下圖是在pom.xml 中添加checkstyle的配置項目,紅框處是指定自己的檢查規則文件
這裏提供一下上面圖中的pom.xml完整的代碼:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.newland</groupId>
<artifactId>StudyKafkaMavenTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>kafka-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--checkstyle.config.location>/home/nmc/dev/devops/jenkins/newland-check.xml</checkstyle.config.location-->
</properties>
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.10</artifactId>
<version>0.8.2.0</version>
</dependency>
</dependencies>
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<excludeFilterFile>findbugs-exclude.xml</excludeFilterFile>
<findbugsXmlOutput>true</findbugsXmlOutput>
<findbugsXmlWithMessages>true</findbugsXmlWithMessages>
<xmlOutput>true</xmlOutput>
</configuration>
</plugin>
<!-- here set checkstyle-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.16</version>
<configuration>
<consoleOutput>false</consoleOutput>
<outputFileFormat>xml</outputFileFormat>
<configLocation>/home/nmc/dev/devops/jenkins/newland-check.xml</configLocation>
<linkXRef>false</linkXRef>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<findbugsXmlOutput>true</findbugsXmlOutput>
<findbugsXmlWithMessages>true</findbugsXmlWithMessages>
<xmlOutput>true</xmlOutput>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.16</version>
<configuration>
<configLocation>/home/nmc/dev/devops/jenkins/newland-check.xml</configLocation>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
至於檢查規則,可以自己根據實際情況進行修改和配置:
3.2 通過ant 來集成checkstyle
通過ant ,可以對checkstyle進行集成,由於ant的特性,需要下載 checkstyle相關的幾個文件,不建議下載太高版本
checkstyle-5.6-all.jar
checkstyle-author.xsl
下載後,連同 newland-check.xml,一起部署到jenkins 的workspace下,可以建立一個 CHECKSTYLE_COMM 目錄來存放
3.2.1 編寫編譯配置文件
在這個目錄下,我們需要編寫一個編譯文件,這個編譯文件,只涉及checkstyle自己本身的內容,在jenkins集成任務裏面,在構建的時候,在ant 編譯代碼完成後,可以進行另外一個ant構建,進行checkstyle的編譯,這樣就防止了項目自己的編譯文件與checkstyle的緊耦合。
下面就是這個編譯文件的內容:
<?xml version="1.0" encoding="UTF-8"?>
<project name="check" default="default" basedir=".">
<property name="src.dir" value="${basedir}/src" />
<property name="class.dir" value="${basedir}/WebRoot/WEB-INF/classes" />
<property name="dist.dir" value="${basedir}/dist" />
<property name="checkstyle.dir" value="${basedir}/../CHECKSTYLE_COMM"/>
<taskdef name="checkstyle" classname="com.puppycrawl.tools.checkstyle.CheckStyleTask"
classpath="${checkstyle.dir}/checkstyle-5.6-all.jar" />
<target name="checkstyle" description="Generates a report of code convention violations.">
<mkdir dir="${basedir}/checkstyle" />
<checkstyle config="${checkstyle.dir}/newland-check.xml" failureProperty="checkstyle.failure" failOnViolation="false">
<formatter type="xml" tofile="${basedir}/checkstyle/checkstyle_report.xml" />
<fileset dir="${src.dir}" includes="**/*.java" />
</checkstyle>
<taskdef resource="checkstyletask.properties"
classpath="${checkstyle.dir}/checkstyle-5.6-all.jar"/>
<xslt in="${basedir}/checkstyle/checkstyle_report.xml" out="${basedir}/checkstyle/checkstyle_report.html"
style="${checkstyle.dir}/checkstyle-author.xsl" />
</target>
<target name="default" depends="checkstyle">
</target>
</project>
3.2.2 修改構建內容
在jenkins裏面,修改項目的構建內容,首先,在第一個構建中,添加一個copy動作,將checkstyle的編譯依賴文件從公共目錄拷貝到項目目錄。
然後在構建中,添加第二個ant 調用動作,這樣就將打包和代碼檢查動作解耦
在構建後動作裏面與maven集成類似,一樣是進行check結果的發佈,指定結果文件等等,這裏不在重複
最後,附上一個截圖,說明ant調用打包和checkstyle的日誌打印信息。
上圖中:
1: 是構建的第一個動作,ant 編譯打包
2:是拷貝檢查的ant 配置文件到項目下
3:是構建的第二個ant 動作,只檢查代碼
四、小結
jenkins 可以通過maven 或者ant 來進行代碼走查,併發布檢查結果。利用jenkins 的構建任務的靈活性,可以進行多次構建,將編譯打包和檢查步驟進行解耦,分步執行,既保有項目的靈活性,又能進行走查。