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. Codeforces 600 E - Lomsat gelral

    E - Lomsat gelral 思路1: 树上启发式合并 代码: #include<bits/stdc++.h> using namespace std; #define fi fir ...

  2. Navicat Premium 12如何激活

    Navicat Premium 12如何激活 一.总结 一句话总结:激活过程中一定要断开网络连接,点电脑的飞行模式没有用,断开网络连接之后才有手动激活的选项 需要断网 点电脑的飞行模式无用 二.Nav ...

  3. layui 日期插件onchange事件失效的方法

    laydate.render({ elem:'#text1',//制定元素 type:'date', //range:true,//开启左右面板 min:'2017-09-1',// max:'201 ...

  4. 雷林鹏分享:jQuery EasyUI 树形菜单 - 创建带复选框的树形菜单

    jQuery EasyUI 树形菜单 - 创建带复选框的树形菜单 easyui 的树(Tree)插件允许您创建一个复选框树.如果您点击一个节点的复选框,这个点击的节点信息将向上和向下继承.例如:点击 ...

  5. 雷林鹏分享:XML 总结 下一步学习什么呢?

    XML 总结 下一步学习什么呢? XML 总结 XML 可用于交换.共享和存储数据. XML 文档形成 树状结构,在"根"和"叶子"的分支机构开始的. XML ...

  6. springboot---->错误: 找不到或无法加载主类

    刚开始是往上面箭头指出的方向去找问题的原因,但是试了各种方法后问题还是没有解决,于是乎我把焦点转去查看eclipsede控制台处: 主要的错误提示如下: Archive for required li ...

  7. java日志及异常错误信息输出的问题

    1.异常信息可以层层传递,直到最后一层再输出日志也来得及 2.错误信息要在发生错误的当时就输出日志,否则到了其它层,很难得到准确的错误信息内容

  8. Spring Boot之实现自动配置

    GITHUB地址:https://github.com/zhangboqing/springboot-learning 一.Spring Boot自动配置原理 自动配置功能是由@SpringBootA ...

  9. NYOJ - 整数划分(四)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=746 要求对一个n的整数插入m个乘号,求最大结果. 构造dp:dp[i][j]表示枚举至j ...

  10. Confluence 6 空间标识

    每一个 Confluence 空间都有一个 空间标识(space key),这个空间标识是简短并且是唯一的,这个标识被用来构建到空间的 URL 中. 当你创建一个站点空间,Confluence 将会为 ...