• maven项目配置findbugs插件 使用git钩子控制代码的提交


    maven项目配置findbugs插件对代码进行静态检测

    当发现代码有bug时,就不让用户commit代码到远程仓库里

    没有bug时才可以commit到远程仓库中

    (1)新建maven项目 ,配置findbugs插件

    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>mvn_findbugs</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>mvn_findbugs</name>
        <url>http://maven.apache.org</url>
      
        <build>
            <finalName>mvn_findbugs</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>
                
                
                 <!-- 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>
                        <!-- 在package 阶段触发执行findbugs检查,比如执行 mvn clean package -->
                        <phase>package</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
                   
                      
            </plugins>
        </build>
        
        
        
        <properties>
            <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>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>        
    </project>
    View Code

    ApplicationDemo.java  (示例类)

    package cn.demo.mvn_findbugs;
    
    public class ApplicationDemo {
        
        public static void main(String[] args){
            System.out.println("Hello World @!!!");
        }
        
        public void add(){
        
            //a.toString();
            System.out.println("Hello World @!!!---------add ()  method");
        }
    }
    View Code

    ApplicationTest.java   (测试类)

    package cn.demo.mvn_findbugs;
    
    import static org.junit.Assert.*;
    
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    public class ApplicationTest {
        
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
            System.out.println("Before()----------before");
        }
    
        @AfterClass
        public static void tearDownAfterClass() throws Exception {
            System.out.println("After()----------after");
        }
    
        @Test
        public void testAdd() {
            ApplicationDemo app = new ApplicationDemo();
            app.add();
            System.out.println("test()----------test---add");
        }
    
    }
    View Code

    (2)将项目拷贝,粘贴到桌面 进入项目根目录 右击 git bush here  执行 git init命令,初始化

    git  bush here

    git init

    (3)使用cd命令打开 .git/hooks文件夹,创建pre-commit文件(没有后缀名的文件)

    cd .git/hooks

    vi pre-commit

    (4)编辑pre-commit 文件,内容如下  (#所标识的内容表示注释,第一行除外)

    #!/bin/sh
    #command for test changed code

    #echo "WELCOME IN HAHAHA "


    #clean the program
    #package the program,test the program
    mvn clean package

    result=$?   #获取当前进程执行结果,mvn clean package命令 会根据maven findbugs的插件的配置执行package的时候 执行 findbugs:findbugs ,如果有bug会返回非0值,如果没有bug会返回0
    echo $result


    if [ $result -ne 0 ]  #这里通过判断返回值来判断项目是否构建成功  -ne 表示不等于
    then
        mvn findbugs:gui    #结果不等于0时,构建失败,打开findbugs的页面,让用户检查错误
        echo "Regretful! BUILD FAILED"
        exit 1      #返回非0结果值,表示提交失败
    else
        echo "Configuration! BUILD SUCCESS"
        exit 0    #返回0结果值,表示提交成功  没有出现bug

    fi

    (5)将项目添加:  git  add .  

    (6)提交文件 :  git commit -m "there is your descripe message"

    执行第六步的时候,会自动调用 pre-commit 脚本,会执行脚本中的内容,根据脚本结果判定是否提交成功

    中间还有一些输出的信息 省略了…………

    从 Fork Value is true  这行开始执行findbugs:findbugs

    BUILD SUCCESS :是构建结果  表示构建成功

    所以下面的最后一圆圈圈起来的 0 ,就是脚本中的 result值,

    出现以上这些就表示commit成功了

    -----------------------------------------------------------------------------------------------------------------------------

    ****************************commit不成功的示例,修改java代码

    ApplicationDemo.java (示例类,故意定义无用的变量 出bug)

    package cn.demo.mvn_findbugs;
    
    public class ApplicationDemo {
        
        String a = null;
        String b = new String();
        String c = new String();
        
        public static void main(String[] args){
            System.out.println("Hello World123 @@@!!! there is no bugs");
        }
        
        public void add(){
            System.out.println("Hello World @!!!---------add ()  method");
        }
    }
    View Code

    其他的配置都和上面保持一致

    执行 git  add .

    然后执行 git commit -m "描述语句,there are some bugs in this demo"

    前面的执行结果没什么差别,当执行findbugs:findbugs命令这里时:

    Fork Value is true :开始执行findbugs

    4:是出现的bug数目

    中间省略一些输出信息…………

    构建结果:BUILD FAILURE  表示构建失败

    下面的小圈圈标识的数字 1 ,返回非0值,这就是shell脚本中的 result值,,构建失败

    然后会执行if判断中的 findbugs:gui命令,自动打开浏览器,进入gui页面

    看到 findbugs:gui页面,表示提交失败, 没有代码可以push到远程仓库

    这就是findbugs插件控制代码提交的(正常/异常)流程了

  • 相关阅读:
    赋值问题
    构造方法的作用
    this的使用
    三目运算符和形参的使用
    构造方法作用:给所有对象进行相同的初始化操作
    成员变量和局部变量
    相关开发的书籍名汇集
    html ---- web sql 例子
    让input表单输入框不记录输入过信息的方法
    css实现两端对齐的3种方法
  • 原文地址:https://www.cnblogs.com/DFX339/p/8259295.html
Copyright © 2020-2023  润新知