Devops学习实践(四) jenkins通过maven或ant集成checkstyle

随着工作的要求,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 的构建任务的灵活性,可以进行多次构建,将编译打包和检查步骤进行解耦,分步执行,既保有项目的灵活性,又能进行走查。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章