checkstyle+eclipse/jenkins对代码规范检查的应用

一、checkstyle+eclipse

1. 认识Checkstyle

CheckStyleSourceForge下的一个项目,提供了一个帮助Java开发人员遵守某些编码规范的工具。它能够自动化代码规范检查过程,从而使得开发人员从这项重要但枯燥的任务中解脱出来。它可以根据设置好的编码规则来检查代码。比如符合规范的变量命名,方法体的最大行数,重复代码检查等等。

2. Checkstyle插件安装

安装完成后,根据提示重启eclipsecheckstyle即安装完成。

3.Checkstyle全局配置

Window -->Preferences,选择checkstyle菜单,增加项目组统一的规则文件,并设置为默认规则。如下图:

到此全局配置已经完成。

4. Checkstyle项目配置

项目 --> 右键Properties

到此项目配置已经完成。成功的话,checkstyle已经开始工作了。

此次发现项目上有很多红叉叉,说明是代码不符合规范造成的。

根据提示信息修改后,则没有有红色的提示。

5. 其它操作

在项目右键菜单中,checkstyle还有一些快捷操作,如下图所示:

6.checkStyle 使用

 

选中工程,右键选择checkstyle->check code withcheckstyle,检查错误即可

 

Checkstyle的结果输出

序号     输出内容意义

1 Type is missing a javadoc commentClass 缺少类型说明

2“{” should be on the previous line “{” 应该位于前一行

3Methos is missing a javadoc comment方法前面缺少javadoc注释

4Expected @throws tag for “Exception”在注释中希望有@throws的说明

5“.” Is preceeded with whitespace “.” 前面不能有空格

6“.” Is followed by whitespace“.” 后面不能有空格

7“=” is not preceeded with whitespace“=” 前面缺少空格

8“=” is not followed with whitespace“=” 后面缺少空格

9“}” should be on the same line“}” 应该与下条语句位于同一行

10Unused @param tag for “unused”没有参数“unused”,不需注释

11Variable “CA” missing javadoc变量“CA”缺少javadoc注释

12Line longer than 80characters行长度超过80

13Line contains a tab character行含有”tab” 字符

14Redundant “Public” modifier冗余的“public” modifier

15Final modifier out of order with the JSL suggestionFinalmodifier的顺序错误

16Avoid using the “.*” form of importImport格式避免使用“.*”

17Redundant import from the same package从同一个包中Import内容

18Unusedimport-java.util.listImport进来的java.util.list没有被使用

19Duplicate import to line 13重复Import同一个内容

20Import from illegal package从非法包中 Import内容

21“while” construct must use “{}”“while” 语句缺少“{}”

22Variable “sTest1” must be private and have accessormethod变量“sTest1”应该是private的,并且有调用它的方法

23Variable “ABC” must match pattern“^[a-z][a-zA-Z0-9]*$”变量“ABC”不符合命名规则“^[a-z][a-zA-Z0-9]*$”

24“(” is followed by whitespace“(” 后面不能有空格 25“)” is proceededby whitespace“)” 前面不能有空格

 

二、checkstyle+jenkins 基于Linux

1、jdk的安装与配置 略 我的版本:java version "1.8.0_144"

2、jenkins的安装与初始化 略 我的版本:Jenkins2.121.1

参考:http://www.cnblogs.com/zhaoxd07/p/4956637.html

3、ant的安装与配置 略 我的版本:Apache Ant(TM) version 1.9.9

参考:https://www.cnblogs.com/sell/archive/2013/07/24/3210198.html

4、jenkins上的checkstyle项目配置

只需要几个文件:

checkstyle-5.6-all.jar:必须有,checkstyle应用包,也可以是其他版本

checkstyle.xml:必须有,规范文件,可以有google版本、sun版,或者其他(比如华为版、自定义版)

checkstyle-author.xsl:可以没有,html模板文件,用于将checkstyle生成的xml文件转换为可视化的html报告,可用于邮件发送;可不使用该模板,而使用jenkins自带的style插件,生成对应报告,不过如果代码量太大,jenkins主机配置不高,jenkins生成报告耗时较长。

build.xml:必须有,ant的默认构建脚本,如果是其他名字,需要使用ant -file xxx.xml指定;文件内指定了前述几个文件的路径、文件名、待检测源代码路径、代码文件名通配符表达式等,参考如下:

<?xml version="1.0" encoding="UTF-8"?> 
<project name="checkstyle" default="checkstyle" basedir="/var/lib/jenkins/workspace/checkstyle"> 
<!-- 检查源码的路径 ,每个作业不同-->
    <target name="init">
        <tstamp/>
        <!-- 设置作业工作目录,每个作业不同 -->
        <property name="project.dir" value="/var/lib/jenkins/workspace/checkstyle"/>
        <!-- 输出报告的路径  -->
        <property name="project.checkstyle.report.dir" value="${project.dir}/checkstyle_report"/>    
        <property name="project.checkstyle.result.name" value="checkstyle-result.xml"/>
        <property name="project.checkstyle.report.name" value="checkstyle-report.html"/>
        <!-- 检测规则存放路径  -->
        <property name="checkstyle.config" value="/var/lib/jenkins/workspace/checkstyle/config/huawei-checkstyle.xml"/>
        <property name="checkstyle.report.style" value="/var/lib/jenkins/workspace/checkstyle/config/checkstyle-author.xsl"/>    
        <property name="checkstyle.result" value="${project.checkstyle.report.dir}/${project.checkstyle.result.name}"/>    
        <property name="checkstyle.report" value="${project.checkstyle.report.dir}/${project.checkstyle.report.name}"/>    
        <mkdir dir="${project.checkstyle.report.dir}"/>
    </target>
    
    <taskdef resource="checkstyletask.properties" classpath="/var/lib/jenkins/workspace/checkstyle/checkstyle-5.6-all.jar" />   
    <target name="checkstyle"  depends="init" description="check java code and report." >          
         <checkstyle config="${checkstyle.config}" failureProperty="checkstyle.failure"  failOnViolation="false">    
            <formatter type="xml"   tofile="${checkstyle.result}" />      
            <fileset dir="${project.dir}/../" includes="**/*.java" /> <!-- 检查源代码的存放路径 -->
        </checkstyle> 
        <!-- 通过指定的xsl模版文件生成一份html的报告,这里生成的文件用于邮件发送时附加上,另外Jenkins插件也会生成可视化的结果  --> 
        <style in="${checkstyle.result}" out="${checkstyle.report}" style="${checkstyle.report.style}" /> 
    </target> 
</project> 

jenkins配置参考:

检查报告:

参考文档:

http://qa.blog.163.com/blog/static/19014700220133351850624/

https://www.cnblogs.com/helloIT/p/6657483.html

https://blog.csdn.net/youjianbo_han_87/article/details/7348498

http://www.cnblogs.com/zhaoxd07/p/4956637.html

https://blog.csdn.net/csolo/article/details/73087223

猜你喜欢

转载自blog.csdn.net/qq_40809549/article/details/81504067