本文共 6800 字,大约阅读时间需要 22 分钟。
前面介绍过Jenkins+Gerrit+Git搭建CI系统,其实在CI系统中有很多有用的插件,这些插件可以帮助开发人员发现代码中的bug或者不规范的地方。下面就介绍下CI相关的插件
FindBugs:FindBugs是一个静态分析工具,它检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题。有了静态分析工具,就可以在不实际运行程序的情况对软件进行分析
PMD:PMD是一款静态代码分析工具,它能够自动检测各种潜在缺陷以及不安全或未优化的代码。PMD 通过其内置的编码规则对 Java 代码进行静态检查,主要包括对潜在的 bug,未使用的代码,重复的代码,循环体创建新对象等问题的检验
CheckStyle:CheckStyle是一个帮助JAVA开发人员遵守某些编码规范的工具。通过检查代码编码格式,命名约定,Javadoc,类设计等方面进行代码规范和风格的检查,从而有效约束开发人员更好地遵循代码编写规范。
插件的用法:
1、项目中配置:
在Goals and options中加入指令 findbugs:findbugs pmd:pmd checkstyle:checkstyle即可
2、pom.xml文件中加入插件
findbugs插件,加入到reporting session中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | < reporting > < plugins > < plugin > < groupId >org.codehaus.mojo</ groupId > < artifactId >findbugs-maven-plugin</ artifactId > < version >2.5.2</ version > < configuration > < findbugsXmlOutput >true</ findbugsXmlOutput > < findbugsXmlWithMessages >true</ findbugsXmlWithMessages > < xmlOutput >true</ xmlOutput > </ configuration > </ plugin > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-jxr-plugin</ artifactId > < version >2.3</ version > </ plugin > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-pmd-plugin</ artifactId > < version >2.5</ version > </ plugin > </ plugins > </ reporting > |
checkstyle插件,加入到plugin session中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-checkstyle-plugin</ artifactId > < version >2.17</ version > < executions > < execution > < id >validate</ id > < phase >validate</ phase > < configuration > < configLocation >checkstyle.xml</ configLocation > < encoding >UTF-8</ encoding > < consoleOutput >true</ consoleOutput > < failsOnError >true</ failsOnError > < linkXRef >false</ linkXRef > </ configuration > < goals > < goal >check</ goal > </ goals > </ execution > </ executions > </ plugin > |
checkstyle模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | <? 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 = "Checker" > < property name = "severity" value = "warning" /> < module name = "TreeWalker" > < 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" /> < 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 name = "FileTabCharacter" /> </ module > |
pmd插件可以不用再pom.xml文件中添加
设置Use rank as priority
findbugs报告