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>

关于执行findbugs,checkstyle,jacoco插件检测代码,GitHook的脚本编写的更多相关文章

  1. maven项目使用jacoco插件检测代码覆盖率详细配置

    使用maven构建项目(java项目或者web项目都可以) jacoco插件的配置参考官方网址:http://www.eclemma.org/jacoco/trunk/doc/maven.html ( ...

  2. jenkins使用jacoco插件检测代码覆盖率(八)

    代码覆盖率:类覆盖,方法覆盖,行覆盖,指令覆盖……(简而言之,就是判断有没有被执行) 覆盖率 = 已经执行的代码 / 总代码 (1)创建maven项目,配置pom.xml如下 pom.xml < ...

  3. maven配置checkstyle插件对代码规范进行静态检查

    checkstyle配置的官方网站:http://checkstyle.sourceforge.net/config.html (1)新建maven项目,配置checkstyle插件 pom.xml ...

  4. Jenkins 配置 FindBugs,Checkstyle,PMD 实现代码的静态检查 (14)

    一.插件介绍 FindBugs:静态分析工具,它检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题.利用这个工具,就可以在不实际运行程序的情况对软件进行分析.它可以帮助改进代码的 ...

  5. jenkins+findbugs+checkstyle+PMD静态代码检查(二)

    可以根据自己的需求选中对应的插件进行配置(不一定非要同时配置三个插件) jenkins:持续集成的工具 fundbugs:检测代码静态错误的插件  例如:定义了没有用到的对象,string类型的比较使 ...

  6. IDEA上安装和使用checkstyle,findbugs,visualVM,PMD插件

    ##安装插件步骤: 1.打开settings 2.选择plugins 3.点击"Browse repositories" 4.搜索对应内插件,点击"install&quo ...

  7. DEA上安装和使用checkstyle,findbugs,visualVM,PMD插件

    ##安装插件步骤: 1.打开settings 2.选择plugins 3.点击"Browse repositories" 4.搜索对应内插件,点击"install&quo ...

  8. 一款检测代码中TODO的eslint插件

    一款检测代码中TODO的eslint插件 前言 看了我标题进来的同学应该也知道我做的是个啥东西 没错是一个eslint插件,前端魔法师们日常所使用的工具之一 什么?你不知道eslint是干嘛的--吃鲸 ...

  9. 使用 Gradle 插件进行代码分析(转)

    代码分析在大多数项目中通常是作为最后一个步骤(如果做了的话)完成的.其通常难以配置及与现有代码整合. 本文旨在勾勒出使用 Gradle 整合 PMD 与 FindBugs 的步骤,并将其与一个现有的 ...

随机推荐

  1. word中的交叉引用

    分别使用“交叉引用”依次插入所需应用文献编号范围的第一个和最后一个. 所需引用处出现“[1][3]”   在引用处对两个编号操作:点击鼠标右键-选择“切换域代码”. [1]变为” {REF _Ref4 ...

  2. NexT 个性化设置

    NexT 主题添加分类页面 新建页面 在本地使用终端 cd 到 blog 文件夹下,执行如下命令: $ cd Documents/blog $ hexo new page categories 设置页 ...

  3. p1465 Preface Numbering

    用这个函数转成罗马数字统计就行了. #include <iostream> #include <cstdio> #include <cmath> #include ...

  4. DAG最长路问题 hdu-1224

    用DFS+记忆化写了一下,拓扑排序+DP的我还没弄明白.据说Codeforces 721C就是这类题目,因为有费用限制,DFS不太好写,有时间把DP法想明白来. #include <iostre ...

  5. Django用户认证组件

    用户认证 主要分两部分: 1.auth模块   from django.contrib import auth 2.User对象 from django.contrib.auth.models imp ...

  6. Raspbian安装Opencv3

    如果旧版本使用apt-get安装 sudo apt-get remove libopencv sudo apt-get remove opencv 如果旧版本使用编译安装 sudo make unin ...

  7. 漏洞复现——bash远程解析命令执行漏洞

    漏洞描述:Bash脚本在解析某些特殊字符串时出现逻辑错误导致可以执行后面的命令,在一些cgi脚本中,数据是通过环境变量来传递的,这样就会形成该漏洞 漏洞原理:bash通过以函数名作为环境变量名,以“( ...

  8. spring boot(十二)打包部署

    有很多网友会时不时的问我,spring boot项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下spring boot 如何开发.调试.打包到最后的投产上线. 开发阶段 单元 ...

  9. introsort(内省排序)

    本文转载于:https://blog.csdn.net/sky453589103/article/details/51116264 快速排序是一种很快的算法,它平均的时间复杂度WieO(nlgn), ...

  10. [转载]完全理解Python迭代对象、迭代器、生成器

    译文地址:liuzhijun 在了解Python的数据结构时,容器(container).可迭代对象(iterable).迭代器(iterator).生成器(generator).列表/集合/字典推导 ...