Git钩子的作用: (pre-commit )
在用户执行 git commit -m "xxx" 命令之前,先执行pre-commit文件中的脚本命令
在pre-commit文件中,编写脚本 执行pom.xml中配置的各种插件 对代码先进行检测
如果所有插件都检测通过,git commit 命令才能执行成功,然后才能继续执行 git push 命令
否则 commit失败,git push的内容会为空。
简而言之:就是控制代码的提交,在代码提交到远程仓库之前会先对代码进行检查(检查内容包括:代码的覆盖率,静态语法错误,代码格式规范等等)
只执行findbugs和jacoco插件的 pre-commit
#!/bin/sh #execute shell before commit,check the code mvn clean install #recieve the execute result #output the result ,if the result less or equal 0 ,it proves this project has bugs,otherwise don‘t. #获取当前进程执行结果,根据maven findbugs的插件的配置执行install的时候 执行 findbugs:findbugs #如果有bug会返回非0值,如果没有bug会返回0 result=$? echo $result if [ $result -ne 0 ] #这里通过判断返回值来判断项目是否构建成功 -ne 表示不等于 then mvn findbugs:gui #结果不等于0时,构建失败,打开findbugs的页面,让用户检查错误 echo "REGRETFUL! BUILD FAILURE" exit 1 #返回非0结果值,表示提交失败 else echo "CONGRATURATION! BUILD SUCCESS" exit 0 #返回0结果值,表示提交成功 (没有出现bug) fi
修正后的pre-commit 文件(最终使用版)
#!/bin/sh #execute shell before commit,check the code mvn clean package #得到项目打包结果,打包成功 执行结果为0;打包不成功 执行结果为非0 package_result=$? if [ $package_result -eq 0 ] then echo "项目执行mvn清空、打包成功,继续执行findbugs检测" mvn findbugs:check #得到findbugs检测结果,没有bug 执行结果为0;有bug 执行结果为非0 findbugs_result=$? if [ $findbugs_result -eq 0 ] then echo "项目执行findbugs检测没有明显错误,继续执行checkstyle检测代码规范" mvn checkstyle:check #得到checkstyle检测结果,没有代码规范问题 执行结果为0;有代码规范问题 执行结果为非0 checkstyle_result=$? if [ $checkstyle_result -eq 0 ] then echo "项目执行checkstyle检测成功,继续执行jacoco检测代码覆盖率" mvn jacoco:check #得到jacoco检测结果,达到代码指定的覆盖率 执行结果为0;没有达到代码覆盖率 执行结果为非0 jacoco_result=$? if [ $jacoco_result -eq 0 ] then echo "提交成功,项目build成功!findbugs,checkstyle,jacoco检测都通过!请继续push!" exit 0 else echo "提交失败,源于项目代码覆盖率没达到要求(mvn jacoco:check)" echo "请查看target/site/jacoco/index.html文件得知详情" exit 1 fi else echo "提交失败,源于项目存在代码规范问题(mvn checkstyle:check)" echo "请查看target目录下的checkstyle-result.html文件得知详情" exit 1 fi else echo "提交失败,源于项目存在bug(mvn findbugs:check)" echo "请从弹出的findbugs:gui界面中查看错误详情" mvn findbugs:gui echo "请修正后重新提交!!!" exit 1 fi else echo "提交失败,源于项目清空或打包失败(mvn clean package)" exit 1 fi
pre-commit文件存放位置
存放在 .git/hooks 目录下
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>
原文地址:https://www.cnblogs.com/DFX339/p/8404196.html
时间: 2024-10-23 19:23:57