Maven 項目如何用git hooks
背景
最近上了一個新項目,項目用的是Maven作為構建工具。提交代碼前要手動執行一遍測試mvn clean test
。自從在Gradle 項目和前端的項目中體驗過git hooks 再也不想回到“原始的生活”。
於是簡單研究了一下maven項目中如何使用git hook
原理
前段時間研究過Java構建工具的歷史。
了解到maven是在ant上的一個更新,最重要的特點是引入了中心倉庫的概念。
maven項目中使用git hook要以來ant的plugin去進行實現。
實現
根目錄的pom文件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<inheritable>false</inheritable>
<executions>
<execution>
<id>hooks</id>
<phase>clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target name="check">
<delete file="${basedir}/.git/hooks/pre-push.sample"/>
<chmod file="${basedir}/pre-push" perm ="777"/>
<copy file="${basedir}/pre-push" tofile="${basedir}/.git/hooks/pre-push">
</target>
</configuration>
</execution>
</executions>
</plugin>
項目的根目錄的pre-push file
#!bin/sh
mvn clean test