(1)名称解释(插件的作用)
findbugs:检测代码的不明显的语法错误。例如:用了==去比较字符串,定义了没有用的变量……
checkstyle:检测代码的格式规范。例如:方法没有写注释,类的命名不规范,括号的位置……
jacoco:检测代码覆盖率。例如:类中有10个方法,对应的测试类中只测试了8个方法,这里的方法覆盖率就是80%……
assembly:将需要用得到的脚本文件和jar/war包一起打包到target目录下,一般只需要用到java项目中……
maven-jar-plugin:执行指定jar包的主类中的主方法
(2)插件的配置
2.1 findbugs-plugin (检测代码静态语法错误)
执行插件的命令:mvn findbugs:check,检测出有bug时会使得 maven项目build失败,可以通过mvn findbugs:gui命令弹出错误详情页面
<!-- 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 install,就会执行findbugs:check命令--> <phase>install</phase> <goals> <goal>check</goal> <!--指定执行check命令--> </goals> </execution> </executions> </plugin>
2.2 checkstyle-plugin (检测代码样式规范)
使用该插件需要先配置规则文件,命名为checkstyle.xml 放在项目根目录下
执行插件的命令 mvn checkstyle:check ,检测出有样式错误时 会使得maven项目构建失败,可以打开target目录下的checkstyle-result.xml文件查看错误详情
<!-- 检测代码风格的插件 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> <!--指定执行check命令--> </goals> </execution> </executions> </plugin>
<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>
</properties>
规则文件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="Checker"> <!--<property name="severity" value="warning"/> 这条注释使得所有的错误都变成warning,不影响构建结果--> <module name="TreeWalker"> <module name="JavadocMethod"> <property name="scope" value="public"/> <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="NeedBraces"> <property name="tokens" value="LITERAL_DO,LITERAL_ELSE,LITERAL_FOR,LITERAL_IF,LITERAL_WHILE"/> </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="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>
2.3 jacoco-plugin (检测代码覆盖率)
使用mvn jacoco:check命令执行该插件,在配置该插件时会指定class/method/instraction/line的覆盖率,如果真是测试结果没有达到指定的覆盖率则会构建失败,可以在target/site/jacoco目录下的index.html文件查看详情
<!--检测代码覆盖率的插件 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> <!--检测代码覆盖率的goal-->
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal> <!--生成报告的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> <!--注意:这里的数值都是百分比,只能写0-1之间的值,例如:0.8-->
</limit>
<!-- 指定指令覆盖到40% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.40</minimum>
</limit>
<!-- 指定行覆盖到40% -->
<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>
2.4 assembly-plugin (将脚本文件和jar、war包一起打包到target目录下的插件)
执行这个插件直接和maven项目打包命令一样 mvn package就可以了,这里需要配置三个地方,1、你需要打包的脚本文件 要放到一个文件夹下(文件夹可以直接新建在项目根目录下,也可以放在src目录下) 。2、编写描述文件:你的文件打包规则以及打包好后的存放文件夹名。 3、maven里面的pom.xml中的配置
2.4.1 脚本文件的存放
这里是将脚本文件放在 src/main/resources目录下的script文件夹中,脚本文件命名为 start.sh (script文件夹是自己新建的)
脚本文件示例:start.sh
#!/bin/sh cd /usr/src/myapp #进入jar包存放的目录 #得到原来的进程ID pid,kill该进程 pid=`cat /usr/src/myapp/pid` #得到该目录下 pid文件中的进程id if [ -n "$pid" ] then echo "kill -9 的pid:" $pid kill -9 $pid #kill该进程 fi #执行jar,并将进程挂起,保存进程ID到 pid文件 echo "Execute shell Finish" #执行项目jar包,将进程挂起,然后将进程id写入当前目录下的pid文件中 BUILD_ID=dontKillMe nohup java -jar /usr/src/myapp/JavademoIn7.jar & echo "$!" > pid
2.4.2 描述文件 script.xml (同样放在src/main/resource目录下)
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> <id>script</id> <formats><!--打包的文件格式 zip,这里也可以是别的格式 比如 dir--> <format>zip</format> </formats> <fileSets> <fileSet> <directory>src/main/resources/script</directory><!--需要打包的目录 也就是脚本文件存放的位置--> <outputDirectory>/</outputDirectory> <!-- 打包后输出的路径 输出到arget目录下,会自动命名为 项目名-script.zip --> </fileSet> </fileSets> </assembly>
2.4.3 在pom.xml中的配置
<!-- 将执行项目的脚本文件一起打包 --> <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>
2.5 maven-jar-plugin (执行项目的主类中的主方法)
这个也没有执行命令,直接使用mvn package 或者 mvn install命令就会执行
<!-- 指定执行的主类(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>
(3)完整的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>