checkstyle配置的官方网站:http://checkstyle.sourceforge.net/config.html

(1)新建maven项目,配置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>cn.demo</groupId>
    <artifactId>JavademoIn7</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging> <!-- 打包成jar包 -->
    <name>JavademoIn7</name>
    <url>http://maven.apache.org</url>
  
    <build>
        <finalName>JavademoIn7</finalName>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${compiler.source}</source>
                    <target>${compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            
    <!-- 检测代码风格的插件 checkstyle(要在项目根目录下配置规则文件checkstyle.xml),然后使用mvn checkstyle::check命令验证-->
    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-checkstyle-plugin</artifactId>
       <version>3.0.0</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>
            
            <!-- 指定执行的主类(main方法所在的类)-->
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-jar-plugin</artifactId>  
                <version>2.6</version>  
                <configuration>  
                    <archive>  
                    <!-- 添加index则不从mainfest中读取classpath,而是从Index.list中读取 -->  
                    <!-- <index>true</index> -->  
                        <manifest>  
                            <mainClass>cn.demo.JavademoIn7.application.ApplicationMain</mainClass>  
                        </manifest>  
                       
                    </archive>  
                </configuration>  
            </plugin>  
            
            <!-- 将执行项目的脚本文件一起打包 -->
             <plugin>
               <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <id>${project.version}</id><!--名字任意 -->
                        <phase>package</phase>   <!-- 绑定到package生命周期阶段上 -->
                        <goals>
                            <goal>single</goal>   <!-- 只运行一次 -->   
                        </goals>
                        
                        <configuration>
                            <descriptors>   <!--描述文件路径--> 
                                <descriptor>src/main/resources/script.xml</descriptor>
                            </descriptors>
                            <!--这样配置后,mvn deploy不会把assembly打的zip包上传到nexus-->
                            <attach>false</attach>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            
        <!-- findbugs插件 :静态检查代码的错误-->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>findbugs-maven-plugin</artifactId>
            <version>3.0.4</version>
            <configuration>
                <!-- 设置分析工作的等级,可以为Min、Default和Max -->
                <effort>Low</effort>
                <!-- Low、Medium和High (Low最严格) -->
                <threshold>Medium</threshold>
                <failOnError>true</failOnError>
                <includeTests>true</includeTests>
                <!--findbugs需要忽略的错误的配置文件-->
               <!--  <excludeFilterFile>compile.bat</excludeFilterFile> -->
            </configuration> 
            <executions>
                <execution>
                    <id>run-findbugs</id>
                    <!-- 在install 阶段触发执行findbugs检查,比如执行 mvn clean package-->
                    <phase>install</phase>  
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        
        <!--检测代码覆盖率的插件 jacoco-->
         <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.8</version>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                          <id>check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
                
                <!-- Configuration 里面写配置信息 -->
                <configuration>
                <!-- rules里面指定覆盖规则 -->
                <rules>
                  <rule implementation="org.jacoco.maven.RuleConfiguration">
                    <element>BUNDLE</element>
                    <limits>
                         <!-- 指定方法覆盖到80% -->
                      <limit implementation="org.jacoco.report.check.Limit">
                        <counter>METHOD</counter>
                        <value>COVEREDRATIO</value>
                        <minimum>0.50</minimum>
                      </limit>
                        <!-- 指定指令覆盖到80% -->
                      <limit implementation="org.jacoco.report.check.Limit">
                        <counter>INSTRUCTION</counter>
                        <value>COVEREDRATIO</value>
                        <minimum>0.40</minimum>
                      </limit>
                       <!-- 指定行覆盖到80% -->
                      <limit implementation="org.jacoco.report.check.Limit">
                        <counter>LINE</counter>
                        <value>COVEREDRATIO</value>
                        <minimum>0.40</minimum>
                      </limit>
                      <!-- 指定类覆盖到100%,不能遗失任何类 -->
                      <limit implementation="org.jacoco.report.check.Limit">
                        <counter>CLASS</counter>
                        <value>MISSEDCOUNT</value>
                            <maximum>0</maximum>
                      </limit>
                      
                    </limits>
                  </rule>
                </rules>
                </configuration>
            </plugin>
            
    </plugins>
    </build>
    
    <reporting>
        <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-checkstyle-plugin</artifactId>
               <version>3.0.0</version>
            </plugin>
        </plugins>
    </reporting>
    <properties>
        <checkstyle.config.location>checkstyle.xml</checkstyle.config.location> <!--这里指定了代码规范检查的规则文件,已经位置在项目根目录下-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <compiler.source>1.7</compiler.source>
        <compiler.target>1.7</compiler.target>
        <junit.version>4.12</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>        
</project>

 

(2)配置checkstyle的规则定义文件

在项目根目录下添加checkstyle.xml文件,内容如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN" "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">

<!--
    This configuration file was written by the eclipse-cs plugin configuration editor
-->
<!--
    Checkstyle-Configuration: JENKIS
    Description: none
-->
<!-- 以下所有 module 指定的name,没有添加property的配置,都是选择官方网站上默认的配置参数。--> <module name="Checker"> <!--<property name="severity" value="warning"/> 这条注释使得所有的错误都变成warning,不影响构建结果-->
   <module name="TreeWalker">
  <!--对方法的注释,这里表示必须注释每一个方法,可以不对方法里面的参数进行注释 --> <module name="JavadocMethod"> <property name="scope" value="puplic"/> <property name="allowUndeclaredRTE" value="true"/> <property name="allowMissingParamTags" value="true"/> <property name="allowMissingJavadoc" value="false"/> </module>
    <!-- 对常量的命名规范--> <module name="ConstantName"/> <module name="LocalFinalVariableName"/> <module name="MemberName"/>

<!-- 方法名的命名规范--> <module name="MethodName"/>
  <!--包名的命名规范 --> <module name="PackageName"/>
    <!-- 静态变量的命名规范--> <module name="StaticVariableName"/>
    <!-- 类名的命名规范--> <module name="TypeName"/> <module name="AvoidStarImport"/> <module name="IllegalImport"/> <module name="RedundantImport"/> <module name="UnusedImports"/> <module name="LineLength"/> <module name="MethodLength"/> <module name="ParameterNumber"> <property name="tokens" value="METHOD_DEF"/> </module> <module name="EmptyForIteratorPad"/> <module name="GenericWhitespace"/> <module name="MethodParamPad"/> <module name="NoWhitespaceAfter"/> <module name="NoWhitespaceBefore"/> <module name="OperatorWrap"/> <module name="ParenPad"/> <module name="TypecastParenPad"/> <module name="WhitespaceAfter"/> <module name="WhitespaceAround"/> <module name="ModifierOrder"/> <module name="RedundantModifier"/> <module name="AvoidNestedBlocks"/> <module name="EmptyBlock"/>
    <!-- 左边花括号的位置,必须另起一行--> <module name="LeftCurly"> <property name="option" value="nl"/> <property name="tokens" value="ANNOTATION_DEF,LITERAL_CATCH,CLASS_DEF,CTOR_DEF,LITERAL_DO,LITERAL_ELSE,ENUM_CONSTANT_DEF,ENUM_DEF,LITERAL_FINALLY,LITERAL_FOR,LITERAL_IF,INTERFACE_DEF,METHOD_DEF,LITERAL_SWITCH,LITERAL_SYNCHRONIZED,LITERAL_TRY,LITERAL_WHILE"/> </module> <module name="NeedBraces"> <property name="tokens" value="LITERAL_DO,LITERAL_ELSE,LITERAL_FOR,LITERAL_IF,LITERAL_WHILE"/> </module>
    <!-- 右边花括号的位置,必须独占一行--> <module name="RightCurly"> <property name="option" value="alone"/> <property name="tokens" value="LITERAL_CATCH,LITERAL_ELSE,LITERAL_FINALLY,LITERAL_IF,LITERAL_TRY"/> </module> <module name="AvoidInlineConditionals"/> <module name="EmptyStatement"/> <module name="EqualsHashCode"/> <module name="HiddenField"> <property name="tokens" value="VARIABLE_DEF"/> <property name="ignoreConstructorParameter" value="true"/> <property name="ignoreSetter" value="true"/> <property name="setterCanReturnItsClass" value="true"/> <property name="ignoreAbstractMethods" value="true"/> </module> <module name="IllegalInstantiation"/> <module name="InnerAssignment"/>
    <!-- 魔术数字的配置--> <module name="MagicNumber"> <property name="ignoreHashCodeMethod" value="true"/> <property name="ignoreAnnotation" value="true"/> <property name="ignoreFieldDeclaration" value="true"/> <property name="constantWaiverParentToken" value="DIV,MINUS,UNARY_MINUS,STAR,PLUS,UNARY_PLUS,ASSIGN,ARRAY_INIT,EXPR,ELIST,METHOD_CALL,LITERAL_NEW,TYPECAST"/> </module> <module name="MissingSwitchDefault"/> <module name="SimplifyBooleanExpression"/> <module name="SimplifyBooleanReturn"/> <module name="FinalClass"/> <module name="HideUtilityClassConstructor"/> <module name="InterfaceIsType"/> <module name="VisibilityModifier"/> <module name="ArrayTypeStyle"/> <module name="TodoComment"/> <module name="UpperEll"/> <module name="LeftCurly"> <property name="option" value="nl"/> </module> <module name="RightCurly"> <property name="option" value="alone_or_singleline"/> <property name="tokens" value="LITERAL_CATCH,LITERAL_ELSE,LITERAL_FINALLY,LITERAL_IF,LITERAL_TRY"/> </module> <module name="LeftCurly"> <property name="option" value="nl"/> <property name="tokens" value="ANNOTATION_DEF,LITERAL_CATCH,CLASS_DEF,CTOR_DEF,LITERAL_DO,LITERAL_ELSE,ENUM_CONSTANT_DEF,ENUM_DEF,LITERAL_FINALLY,LITERAL_FOR,LITERAL_IF,INTERFACE_DEF,METHOD_DEF,LITERAL_SWITCH,LITERAL_SYNCHRONIZED,LITERAL_TRY,LITERAL_WHILE"/> </module> <module name="EmptyLineSeparator"> <property name="tokens" value="CLASS_DEF,CTOR_DEF,ENUM_DEF,IMPORT,INSTANCE_INIT,INTERFACE_DEF,METHOD_DEF,PACKAGE_DEF,STATIC_INIT,VARIABLE_DEF"/> </module> </module> <module name="Translation"/> <module name="FileLength"/> </module>

(3)执行checkstyle:check命令,完成代码规范检查

mvn checkstyle:check  #这条命令发现有错误时会使得构建失败

 

 posted on 2018-02-01 18:40  阿叮339  阅读(6497)  评论(0编辑  收藏  举报