git 提交代码,推送代码,可以通过在 .git/hooks
目录中的 bash 脚本来做一定的验证工作。
本例使用插件 maven-antrun-plugin
自动输出脚本至 .git/hooks
目录中,在 pom.xml 中的使用示例如下:
xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>gen-gitbash-clean</id>
<phase>clean</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>gen-gitbash-compile</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<!-- configuration没有放在execution里面是让多个execution都能执行 -->
<configuration>
<target>
<!-- 生成commit-msg文件 -->
<echo file="${basedir}/.git/hooks/commit-msg"
message="#!/usr/bin/env bash${line.separator}${line.separator}"/>
<echo file="${basedir}/.git/hooks/commit-msg" append="true"
message="# Color codes${line.separator}red='\033[0;31m'${line.separator}yellow='\033[0;33m'${line.separator}blue='\033[0;34m'${line.separator}NC='\033[0m' # No colors"/>
<echo file="${basedir}/.git/hooks/commit-msg" append="true"
message="${line.separator}${line.separator}"/>
<echo file="${basedir}/.git/hooks/commit-msg" append="true"
message="# Regex & Usage${line.separator}commit_regex="^(\[[A-Za-z]+\-[0-9]+\]|merge|Merge)\s"${line.separator}error_message="Aborting commit. Commit message must start with ${yellow} 开发任务编号"${line.separator}usage="开发任务编号 Commit Message. \n${blue}示例: [TASK-101] 完成A功能开发""/>
<echo file="${basedir}/.git/hooks/commit-msg" append="true"
message="${line.separator}${line.separator}"/>
<echo file="${basedir}/.git/hooks/commit-msg" append="true"
message="if ! grep -qE "$commit_regex" "$1"; then${line.separator}"/>
<echo file="${basedir}/.git/hooks/commit-msg" append="true"
message=" printf "${red}$error_message\n"${line.separator}"/>
<echo file="${basedir}/.git/hooks/commit-msg" append="true"
message=" printf "${yellow}Commit Regex: ${NC}${commit_regex}\n"${line.separator}"/>
<echo file="${basedir}/.git/hooks/commit-msg" append="true"
message=" printf "${red}Usage: ${NC}${usage}\n"${line.separator}"/>
<echo file="${basedir}/.git/hooks/commit-msg" append="true"
message=" exit 1${line.separator}fi"/>
<!-- 生成pre-push文件 -->
<echo file="${basedir}/.git/hooks/pre-push"
message="#!/usr/bin/env bash${line.separator}${line.separator}"/>
<echo file="${basedir}/.git/hooks/pre-push" append="true"
message="# Color codes${line.separator}red='\033[0;31m'${line.separator}yellow='\033[0;33m'"/>
<echo file="${basedir}/.git/hooks/pre-push" append="true"
message="${line.separator}${line.separator}"/>
<echo file="${basedir}/.git/hooks/pre-push" append="true"
message="command="./mvnw.cmd clean test"${line.separator}${command}"/>
<echo file="${basedir}/.git/hooks/pre-push" append="true"
message="${line.separator}${line.separator}"/>
<echo file="${basedir}/.git/hooks/pre-push" append="true"
message="if ! [ $? -eq 0 ]; then${line.separator}"/>
<echo file="${basedir}/.git/hooks/pre-push" append="true"
message=" printf "${red}Make sure successfully execute command: ${yellow}$command${red}."${line.separator}"/>
<echo file="${basedir}/.git/hooks/pre-push" append="true"
message=" exit 1${line.separator}fi"/>
<!-- 设置权限 -->
<chmod dir="${basedir}/.git/hooks" perm="755" includes="**/*"/>
</target>
</configuration>
</plugin>
在执行maven的 clean 或者 compile 时候,插件会自动输出
commit-msg
和pre-push
两个脚本文件
(END)