About executing the Findbugs,checkstyle,jacoco plug-in detection code, Githook scripting

Source: Internet
Author: User
Tags prepare git hooks checkstyle

The role of Git hooks: (pre-commit)

Execute the script command in the Pre-commit file before the user executes the git commit-m "xxx" command

In the Pre-commit file, write scripts to perform the various plug-ins configured in Pom.xml to detect the code first

If all plug-ins are detected, the git commit command succeeds before it can proceed with the git push command

Otherwise, the commit fails, and the contents of the Git push are empty.

In short: The control code commits, before the code is submitted to the remote repository before the code is checked (including: Code coverage, static syntax errors, code format specification, etc.)

Only perform findbugs and Jacoco plug-in Pre-commit

#!/bin/Sh#execute Shell before commit,check the CODEMVN clean Install#recieve the Execute result#output the result,ifThe result less or equal 0, it proves ThisProject has bugs,otherwise don ' t.#获取当前进程执行结果, execute findbugs:findbugs# when the install is executed according to the configuration of the Maven FindBugs plugin if there is a bug that returns a non-0 value, if no bug returns 0result=$?Echo $resultif[$result-ne 0] #这里通过判断返回值来判断项目是否构建成功-ne means not equal to then MVN Findbugs:gui #结果不等于0时, build failed, open findbugs page, let user check error echo"Regretful! BUILD FAILURE "Exit1#返回非0结果值, indicating that the commit failedElseEcho"Congraturation! BUILD SUCCESS "Exit0#返回0结果值, indicates successful commit (no bug) fi

Revised Pre-commit file (final version)

#!/bin/Sh#execute Shell before commit,check the CODEMVN clean Package#得到项目打包结果, package successful execution result is 0; package unsuccessful execution result is non-0package_result=$?if[$package _result-eq 0]then Echo"Project execution MVN emptied, packaged successfully, continued to perform findbugs detection"mvn findbugs:check #得到findbugs检测结果, no bug execution result is 0; bug execution result is not 0 Findbugs_result=$?if[$findbugs _result-eq 0] then echo"Project execution FindBugs detect no significant error, continue to execute Checkstyle Instrumentation code Specification"mvn checkstyle:check #得到checkstyle检测结果, no code specification problem execution result is 0; code specification problem execution result is not 0 Checkstyle_result=$?if[$checkstyle _result-eq 0] then echo"Project execution Checkstyle detected successfully, continue to perform Jacoco detection code coverage"mvn Jacoco:check #得到jacoco检测结果, reach the code specified coverage execution result is 0; Code coverage is not reached execution result is not 0 jacoco_result=$?if[$jacoco _result-eq 0] then echo"Successful submission, project build Success!" Findbugs,checkstyle,jacoco detection is passed! Please continue push! "Exit0ElseEcho"Submission failed due to project code coverage not met requirements (MVN Jacoco:check)"Echo"Please view target/site/jacoco/index.html file for details"Exit1fiElseEcho"Commit failed due to project existence Code specification issue (MVN Checkstyle:check)"Echo"Please see the checkstyle-result.html file under the target directory to learn more"Exit1fiElseEcho"Submission failed due to bug in Project (MVN findbugs:check)"Echo"Please view error details from the popup Findbugs:gui interface."mvn Findbugs:gui Echo"Please fix and resubmit again!!!" "Exit1fiElseEcho"Submission failed due to project emptying or packaging failure (MVN clean package)"Exit1fi

Pre-commit File storage location

Stored under the. Git/hooks Directory

Configuration of the 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> &lt ;version>0.0.1-snapshot</version> <packaging>jar</packaging> <!--packaged in Jar-&LT;NAME&G T Javademoin7</name> <url>http://maven.apache.org</url><build> <finalName>JavademoIn7</finalName> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactid&gt ;maven-compiler-plugin</artifactid> <version>3.1</version> <configurati On> <source>${compiler.source}</source> <target>${compiler.targ Et}</target> <encoding>${project.build.sourceEncoding}</encoding> < /configuration> </plugin> <!--detect code-style plug-ins Checkstyle (to configure the rule file Checkstyle.xml under the project root), and then Use MVN checkstyle::check command Verification-<plugin> <groupId>org.apache.maven.plugins</groupId> &L T;artifactid>maven-checkstyle-plugin</artifactid> <version>3.0.0</version> <executions           > <execution> <id>validate</id> <phase>validate</phase> <configuration&gT <encoding>UTF-8</encoding> <consoleOutput>true</consoleOutput> <failsOnError>true</failsOnError> <linkXRef>false</linkXRef> </configuration> <goals> <goal>check</goal> </goals> </execution> </executions> </plugin> <! --Specify the main class of execution (the class where the Main method resides)-<plugin> <groupid>org.apache.maven.plugins</groupi D> <artifactId>maven-jar-plugin</artifactId> <version>2.6</versi On> <configuration> <archive> <!--adding index does not Read Classpath in Mainfest, but read from Index.list <!--<index>true</index> <manifest> <mainclass>cn.demo.java                                             Demoin7.application.applicationmain</mainclass> </manifest>                          </archive> </configuration> </plugin> <!--Package The script file that executes the project together-<plugin> <groupId>org.apache.maven.plugins< /groupid> <artifactId>maven-assembly-plugin</artifactId> <version>2.4. 1</version> <executions> <execution> <id> ${project.version}</id><!--Name-<phase> Package</phase> <!--binding to the package lifecycle stage--<goals> <goal                                                >single</goal> <!--run only once---</goals>                                 <configuration> <descriptors> <!--description file path-- <descriptor>src/main/resources/script.xml</descriptor> </descripto Rs> <!--This configuration, mvn deploy does not upload the assembly Zip package to nexus--> <at Tach>false</attach> </configuration> </execution> </ex            Ecutions> </plugin> <!--findbugs plugin: Static check code error--<plugin> <groupId>org.codehaus.mojo</groupId> &LT;ARTIFACTID&GT;FINDBUGS-MAVEN-PLUGIN&LT;/ARTIFACTID&G            T <version>3.0.4</version> <configuration> <!--to set the level of analysis work for Min, default, and Max                -<effort>Low</effort> <!--low, medium, and high (the most stringent)- <threshold>Medium</threshold> <failOnError>true</failOnError> <includeTests>true</includeTests> <!--findbugs need to ignore the wrong profile--<!--<excludefilterfile>com                Pile.bat</excludefilterfile>-</configuration> <executions> <execution> <id>run-findbugs</id> <!--trigger execution of findbugs checks during the install phase such as executing mvn clean. Package-<phase>install</phase> <goals> &lt ;goal>check</goal> </goals> </execution> </executions > </plugin> <!--plug-in for detecting code coverage jacoco--> <plugin> <groupi D>org.jacoco</groupid> <artifactId>jacoco-maven-plugin</artifactId> &lt                        ;version>0.7.8</version> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepa re-agent</goal> </goals> </execution> <e                            Xecution> <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 information in the config-<configuration> <!--rules Specify the override rule--                    > <rules> <rule implementation= "Org.jacoco.maven.RuleConfiguration" > <element>BUNDLE</element> <limits> <!--specified method cover Cover to 80%--<limit implementation= "Org.jacoco.report.check.Limit" > < Counter>method</counter> <value>COVEREDRATIO</value> &L                      T;minimum>0.50</minimum> </limit> <!--specify instruction overlay to 80% <liMIT implementation= "Org.jacoco.report.check.Limit" > <counter>INSTRUCTION</counter>                      <value>COVEREDRATIO</value> <minimum>0.40</minimum> </limit> <!--specified line overlay to 80%-<limit implement                        ation= "Org.jacoco.report.check.Limit" > <counter>LINE</counter>                      <value>COVEREDRATIO</value> <minimum>0.40</minimum> </limit> <!--the specified class is covered to 100%, you cannot lose any class--<limit implementation= "ORG.J Acoco.report.check.Limit "> <counter>CLASS</counter> <value >MISSEDCOUNT</value> <maximum>0</maximum> </limit&                     Gt                     </limits> </rule> </rules> </conf        iguration> </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.loc Ation>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> <dependenci  Es> <dependency>          <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.8</version> </dependency> <dependency> <groupid&            Gt;org.apache.maven.plugins</groupid> <artifactId>maven-clean-plugin</artifactId> <version>2.5</version> </dependency> <dependency> <groupid>junit&lt ;/groupid> <artifactId>junit</artifactId> <version>${junit.version}</version > <scope>test</scope> </dependency> </dependencies> </project&gt ;

About executing the Findbugs,checkstyle,jacoco plug-in detection code, Githook scripting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.