├── .gitignore ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── .travis.yml ├── LICENSE ├── README.md ├── documentation ├── logo.png └── social-media-preview.png ├── etc └── checkstyle │ └── checkstyle.xml ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main └── java │ └── com │ └── rudikershaw │ └── gitbuildhook │ ├── GitConfigMojo.java │ ├── InitialiseMojo.java │ ├── InstallMojo.java │ ├── hook │ └── type │ │ ├── GitHookType.java │ │ └── package-info.java │ ├── package-info.java │ ├── threadsafety │ ├── ClassLock.java │ └── package-info.java │ └── validation │ ├── GitRepositoryValidator.java │ └── package-info.java └── test ├── java └── com │ └── rudikershaw │ └── gitbuildhook │ ├── AbstractMojoTest.java │ ├── GitConfigMojoTest.java │ ├── InitializeMojoTest.java │ └── InstallMojoTest.java └── resources ├── com └── rudikershaw │ └── gitbuildhook │ └── tests │ └── cphooks │ ├── hook-to-install-from-cp.sh │ ├── reinstall-hook-origional.sh │ └── reinstall-hook-updated.sh ├── default-test-project └── pom.xml ├── test-project-configure └── pom.xml ├── test-project-initialise └── pom.xml ├── test-project-install-hooks ├── hook-to-install.sh └── pom.xml ├── test-project-invalid-hook ├── hook-to-install.sh └── pom.xml └── test-project-reinstall-hooks ├── hook-to-install.sh ├── hook-to-reinstall.sh ├── pom.xml └── pom2.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | target/ 3 | *.iml 4 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rudikershaw/git-build-hook/71f1a9844e0d379f8c6d2747718feac3a614184d/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.0/apache-maven-3.9.0-bin.zip 18 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar 19 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - openjdk8 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Rudi Kershaw 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | The Git Build Hook Maven Plugin Logo 2 | 3 | [![MIT Licence][licence-image]][licence-url] 4 | [![Build Status][travis-image]][travis-url] 5 | [![Maven Central][maven-central-image]][maven-central-url] 6 | 7 | # Git Build Hook Maven Plugin 8 | 9 | A Maven plugin used to add configuration, install git hooks, and initialize the local project's git repository. It is common for a team or project to need to manage client side git configuration. For example, you may need to install pre-commit hooks for all your developers, or insist on a particular `core.autoclrf` policy. This plugin allows you to setup configuration for every developer working on the project the first time they run your build. 10 | 11 | ## Key Features 12 | 13 | * Set arbitrary project specific git configuration. 14 | * Install client side (local) git hooks for the project. 15 | * Fail the build if your project is not being managed by Git. 16 | * Use with Maven archetypes to initialise Git repository with the first build. 17 | 18 | ## Basic Usage 19 | 20 | A common use-case might be to install local git hooks by setting the `core.hooksPath` configuration. Put all your Git hooks in a directory in your project, then configure your `pom.xml` to include the following plugin declaration, goal, and configuration. 21 | 22 | ```xml 23 | 24 | 25 | 26 | com.rudikershaw.gitbuildhook 27 | git-build-hook-maven-plugin 28 | 3.5.0 29 | 30 | 31 | 32 | hooks-directory/ 33 | 34 | true 35 | 36 | 37 | 38 | 39 | 40 | 41 | configure 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | ``` 50 | 51 | When you run your project build the plugin will configure git to run hooks out of the directory specified. This will effectively set up the hooks in that directory for everyone working on your project. If you would prefer to install individual git hooks into the default hooks directory, then you can use the `install` goal with configuration for each hook you wish to install like so; 52 | 53 | ```xml 54 | ... 55 | 56 | 57 | 58 | file_path/to/your/hook.sh 59 | class_path/package/hook.sh 60 | 61 | 62 | 63 | 64 | my.company 65 | company-git-hooks 66 | [1.2.3,) 67 | 68 | 69 | ... 70 | 71 | 72 | install 73 | 74 | ... 75 | ``` 76 | 77 | With both of the above goals, the build will fail if the project is not managed by Git. If you would prefer the plugin to, instead of failing, initialize a new Git repository at the root of the project you can do the following; 78 | 79 | ```xml 80 | ... 81 | 82 | 83 | initialize 84 | configure 85 | 86 | ... 87 | ``` 88 | 89 | ### Wait, but why? 90 | 91 | Many web-based hosting services for version control using Git, do not allow server side hooks. Server side hooks are extremely useful for enforcing certain styles of commit message, restricting the kind and types of actions that can be performed against certain branches, providing useful feedback or advice during certain actions in Git, and much more. This kind of quick feedback is advantageous when managing any large group of developers. 92 | 93 | If you cannot perform these kind of actions server side, what else can be done? Well, the hooks can be installed on the developers local machines. But it can be difficult to organise groups of people to install these hooks and even more difficult to get updates out to everyone. 94 | 95 | If only there was some way that the hooks could be managed in your project repository and installed automatically during your build. Well, that is what this plugin is for. 96 | 97 | [licence-image]: http://img.shields.io/npm/l/gulp-rtlcss.svg?style=flat 98 | [licence-url]: https://tldrlegal.com/license/mit-license 99 | [travis-image]: https://app.travis-ci.com/rudikershaw/git-build-hook.svg?branch=develop 100 | [travis-url]: https://app.travis-ci.com/rudikershaw/git-build-hook?branch=develop 101 | [maven-central-image]: https://maven-badges.herokuapp.com/maven-central/com.rudikershaw.gitbuildhook/git-build-hook-maven-plugin/badge.svg 102 | [maven-central-url]: https://maven-badges.herokuapp.com/maven-central/com.rudikershaw.gitbuildhook/git-build-hook-maven-plugin 103 | -------------------------------------------------------------------------------- /documentation/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rudikershaw/git-build-hook/71f1a9844e0d379f8c6d2747718feac3a614184d/documentation/logo.png -------------------------------------------------------------------------------- /documentation/social-media-preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rudikershaw/git-build-hook/71f1a9844e0d379f8c6d2747718feac3a614184d/documentation/social-media-preview.png -------------------------------------------------------------------------------- /etc/checkstyle/checkstyle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Apache Maven Wrapper startup batch script, version 3.1.1 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | # e.g. to debug Maven itself, use 32 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | # ---------------------------------------------------------------------------- 35 | 36 | if [ -z "$MAVEN_SKIP_RC" ] ; then 37 | 38 | if [ -f /usr/local/etc/mavenrc ] ; then 39 | . /usr/local/etc/mavenrc 40 | fi 41 | 42 | if [ -f /etc/mavenrc ] ; then 43 | . /etc/mavenrc 44 | fi 45 | 46 | if [ -f "$HOME/.mavenrc" ] ; then 47 | . "$HOME/.mavenrc" 48 | fi 49 | 50 | fi 51 | 52 | # OS specific support. $var _must_ be set to either true or false. 53 | cygwin=false; 54 | darwin=false; 55 | mingw=false 56 | case "`uname`" in 57 | CYGWIN*) cygwin=true ;; 58 | MINGW*) mingw=true;; 59 | Darwin*) darwin=true 60 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 61 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 62 | if [ -z "$JAVA_HOME" ]; then 63 | if [ -x "/usr/libexec/java_home" ]; then 64 | JAVA_HOME="`/usr/libexec/java_home`"; export JAVA_HOME 65 | else 66 | JAVA_HOME="/Library/Java/Home"; export JAVA_HOME 67 | fi 68 | fi 69 | ;; 70 | esac 71 | 72 | if [ -z "$JAVA_HOME" ] ; then 73 | if [ -r /etc/gentoo-release ] ; then 74 | JAVA_HOME=`java-config --jre-home` 75 | fi 76 | fi 77 | 78 | # For Cygwin, ensure paths are in UNIX format before anything is touched 79 | if $cygwin ; then 80 | [ -n "$JAVA_HOME" ] && 81 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 82 | [ -n "$CLASSPATH" ] && 83 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 84 | fi 85 | 86 | # For Mingw, ensure paths are in UNIX format before anything is touched 87 | if $mingw ; then 88 | [ -n "$JAVA_HOME" ] && 89 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 90 | fi 91 | 92 | if [ -z "$JAVA_HOME" ]; then 93 | javaExecutable="`which javac`" 94 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 95 | # readlink(1) is not available as standard on Solaris 10. 96 | readLink=`which readlink` 97 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 98 | if $darwin ; then 99 | javaHome="`dirname \"$javaExecutable\"`" 100 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 101 | else 102 | javaExecutable="`readlink -f \"$javaExecutable\"`" 103 | fi 104 | javaHome="`dirname \"$javaExecutable\"`" 105 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 106 | JAVA_HOME="$javaHome" 107 | export JAVA_HOME 108 | fi 109 | fi 110 | fi 111 | 112 | if [ -z "$JAVACMD" ] ; then 113 | if [ -n "$JAVA_HOME" ] ; then 114 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 115 | # IBM's JDK on AIX uses strange locations for the executables 116 | JAVACMD="$JAVA_HOME/jre/sh/java" 117 | else 118 | JAVACMD="$JAVA_HOME/bin/java" 119 | fi 120 | else 121 | JAVACMD="`\\unset -f command; \\command -v java`" 122 | fi 123 | fi 124 | 125 | if [ ! -x "$JAVACMD" ] ; then 126 | echo "Error: JAVA_HOME is not defined correctly." >&2 127 | echo " We cannot execute $JAVACMD" >&2 128 | exit 1 129 | fi 130 | 131 | if [ -z "$JAVA_HOME" ] ; then 132 | echo "Warning: JAVA_HOME environment variable is not set." 133 | fi 134 | 135 | # traverses directory structure from process work directory to filesystem root 136 | # first directory with .mvn subdirectory is considered project base directory 137 | find_maven_basedir() { 138 | if [ -z "$1" ] 139 | then 140 | echo "Path not specified to find_maven_basedir" 141 | return 1 142 | fi 143 | 144 | basedir="$1" 145 | wdir="$1" 146 | while [ "$wdir" != '/' ] ; do 147 | if [ -d "$wdir"/.mvn ] ; then 148 | basedir=$wdir 149 | break 150 | fi 151 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 152 | if [ -d "${wdir}" ]; then 153 | wdir=`cd "$wdir/.."; pwd` 154 | fi 155 | # end of workaround 156 | done 157 | printf '%s' "$(cd "$basedir"; pwd)" 158 | } 159 | 160 | # concatenates all lines of a file 161 | concat_lines() { 162 | if [ -f "$1" ]; then 163 | echo "$(tr -s '\n' ' ' < "$1")" 164 | fi 165 | } 166 | 167 | BASE_DIR=$(find_maven_basedir "$(dirname $0)") 168 | if [ -z "$BASE_DIR" ]; then 169 | exit 1; 170 | fi 171 | 172 | MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}; export MAVEN_PROJECTBASEDIR 173 | if [ "$MVNW_VERBOSE" = true ]; then 174 | echo $MAVEN_PROJECTBASEDIR 175 | fi 176 | 177 | ########################################################################################## 178 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 179 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 180 | ########################################################################################## 181 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 182 | if [ "$MVNW_VERBOSE" = true ]; then 183 | echo "Found .mvn/wrapper/maven-wrapper.jar" 184 | fi 185 | else 186 | if [ "$MVNW_VERBOSE" = true ]; then 187 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 188 | fi 189 | if [ -n "$MVNW_REPOURL" ]; then 190 | wrapperUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar" 191 | else 192 | wrapperUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar" 193 | fi 194 | while IFS="=" read key value; do 195 | case "$key" in (wrapperUrl) wrapperUrl="$value"; break ;; 196 | esac 197 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 198 | if [ "$MVNW_VERBOSE" = true ]; then 199 | echo "Downloading from: $wrapperUrl" 200 | fi 201 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 202 | if $cygwin; then 203 | wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` 204 | fi 205 | 206 | if command -v wget > /dev/null; then 207 | QUIET="--quiet" 208 | if [ "$MVNW_VERBOSE" = true ]; then 209 | echo "Found wget ... using wget" 210 | QUIET="" 211 | fi 212 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 213 | wget $QUIET "$wrapperUrl" -O "$wrapperJarPath" 214 | else 215 | wget $QUIET --http-user="$MVNW_USERNAME" --http-password="$MVNW_PASSWORD" "$wrapperUrl" -O "$wrapperJarPath" 216 | fi 217 | [ $? -eq 0 ] || rm -f "$wrapperJarPath" 218 | elif command -v curl > /dev/null; then 219 | QUIET="--silent" 220 | if [ "$MVNW_VERBOSE" = true ]; then 221 | echo "Found curl ... using curl" 222 | QUIET="" 223 | fi 224 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 225 | curl $QUIET -o "$wrapperJarPath" "$wrapperUrl" -f -L 226 | else 227 | curl $QUIET --user "$MVNW_USERNAME:$MVNW_PASSWORD" -o "$wrapperJarPath" "$wrapperUrl" -f -L 228 | fi 229 | [ $? -eq 0 ] || rm -f "$wrapperJarPath" 230 | else 231 | if [ "$MVNW_VERBOSE" = true ]; then 232 | echo "Falling back to using Java to download" 233 | fi 234 | javaSource="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 235 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" 236 | # For Cygwin, switch paths to Windows format before running javac 237 | if $cygwin; then 238 | javaSource=`cygpath --path --windows "$javaSource"` 239 | javaClass=`cygpath --path --windows "$javaClass"` 240 | fi 241 | if [ -e "$javaSource" ]; then 242 | if [ ! -e "$javaClass" ]; then 243 | if [ "$MVNW_VERBOSE" = true ]; then 244 | echo " - Compiling MavenWrapperDownloader.java ..." 245 | fi 246 | # Compiling the Java class 247 | ("$JAVA_HOME/bin/javac" "$javaSource") 248 | fi 249 | if [ -e "$javaClass" ]; then 250 | # Running the downloader 251 | if [ "$MVNW_VERBOSE" = true ]; then 252 | echo " - Running MavenWrapperDownloader.java ..." 253 | fi 254 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 255 | fi 256 | fi 257 | fi 258 | fi 259 | ########################################################################################## 260 | # End of extension 261 | ########################################################################################## 262 | 263 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 264 | 265 | # For Cygwin, switch paths to Windows format before running java 266 | if $cygwin; then 267 | [ -n "$JAVA_HOME" ] && 268 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 269 | [ -n "$CLASSPATH" ] && 270 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 271 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 272 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 273 | fi 274 | 275 | # Provide a "standardized" way to retrieve the CLI args that will 276 | # work with both Windows and non-Windows executions. 277 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 278 | export MAVEN_CMD_LINE_ARGS 279 | 280 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 281 | 282 | exec "$JAVACMD" \ 283 | $MAVEN_OPTS \ 284 | $MAVEN_DEBUG_OPTS \ 285 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 286 | "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 287 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 288 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Apache Maven Wrapper startup batch script, version 3.1.1 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 28 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending 29 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 30 | @REM e.g. to debug Maven itself, use 31 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 32 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 33 | @REM ---------------------------------------------------------------------------- 34 | 35 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 36 | @echo off 37 | @REM set title of command window 38 | title %0 39 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' 40 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 41 | 42 | @REM set %HOME% to equivalent of $HOME 43 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 44 | 45 | @REM Execute a user defined script before this one 46 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 47 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 48 | if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* 49 | if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* 50 | :skipRcPre 51 | 52 | @setlocal 53 | 54 | set ERROR_CODE=0 55 | 56 | @REM To isolate internal variables from possible post scripts, we use another setlocal 57 | @setlocal 58 | 59 | @REM ==== START VALIDATION ==== 60 | if not "%JAVA_HOME%" == "" goto OkJHome 61 | 62 | echo. 63 | echo Error: JAVA_HOME not found in your environment. >&2 64 | echo Please set the JAVA_HOME variable in your environment to match the >&2 65 | echo location of your Java installation. >&2 66 | echo. 67 | goto error 68 | 69 | :OkJHome 70 | if exist "%JAVA_HOME%\bin\java.exe" goto init 71 | 72 | echo. 73 | echo Error: JAVA_HOME is set to an invalid directory. >&2 74 | echo JAVA_HOME = "%JAVA_HOME%" >&2 75 | echo Please set the JAVA_HOME variable in your environment to match the >&2 76 | echo location of your Java installation. >&2 77 | echo. 78 | goto error 79 | 80 | @REM ==== END VALIDATION ==== 81 | 82 | :init 83 | 84 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 85 | @REM Fallback to current working directory if not found. 86 | 87 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 88 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 89 | 90 | set EXEC_DIR=%CD% 91 | set WDIR=%EXEC_DIR% 92 | :findBaseDir 93 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 94 | cd .. 95 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 96 | set WDIR=%CD% 97 | goto findBaseDir 98 | 99 | :baseDirFound 100 | set MAVEN_PROJECTBASEDIR=%WDIR% 101 | cd "%EXEC_DIR%" 102 | goto endDetectBaseDir 103 | 104 | :baseDirNotFound 105 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 106 | cd "%EXEC_DIR%" 107 | 108 | :endDetectBaseDir 109 | 110 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 111 | 112 | @setlocal EnableExtensions EnableDelayedExpansion 113 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 114 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 115 | 116 | :endReadAdditionalConfig 117 | 118 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 121 | 122 | set WRAPPER_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar" 123 | 124 | FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 125 | IF "%%A"=="wrapperUrl" SET WRAPPER_URL=%%B 126 | ) 127 | 128 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 129 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 130 | if exist %WRAPPER_JAR% ( 131 | if "%MVNW_VERBOSE%" == "true" ( 132 | echo Found %WRAPPER_JAR% 133 | ) 134 | ) else ( 135 | if not "%MVNW_REPOURL%" == "" ( 136 | SET WRAPPER_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar" 137 | ) 138 | if "%MVNW_VERBOSE%" == "true" ( 139 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 140 | echo Downloading from: %WRAPPER_URL% 141 | ) 142 | 143 | powershell -Command "&{"^ 144 | "$webclient = new-object System.Net.WebClient;"^ 145 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ 146 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ 147 | "}"^ 148 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%WRAPPER_URL%', '%WRAPPER_JAR%')"^ 149 | "}" 150 | if "%MVNW_VERBOSE%" == "true" ( 151 | echo Finished downloading %WRAPPER_JAR% 152 | ) 153 | ) 154 | @REM End of extension 155 | 156 | @REM Provide a "standardized" way to retrieve the CLI args that will 157 | @REM work with both Windows and non-Windows executions. 158 | set MAVEN_CMD_LINE_ARGS=%* 159 | 160 | %MAVEN_JAVA_EXE% ^ 161 | %JVM_CONFIG_MAVEN_PROPS% ^ 162 | %MAVEN_OPTS% ^ 163 | %MAVEN_DEBUG_OPTS% ^ 164 | -classpath %WRAPPER_JAR% ^ 165 | "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ 166 | %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 167 | if ERRORLEVEL 1 goto error 168 | goto end 169 | 170 | :error 171 | set ERROR_CODE=1 172 | 173 | :end 174 | @endlocal & set ERROR_CODE=%ERROR_CODE% 175 | 176 | if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost 177 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 178 | if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" 179 | if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" 180 | :skipRcPost 181 | 182 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 183 | if "%MAVEN_BATCH_PAUSE%"=="on" pause 184 | 185 | if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% 186 | 187 | cmd /C exit /B %ERROR_CODE% 188 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.rudikershaw.gitbuildhook 7 | git-build-hook-maven-plugin 8 | 3.6.0-SNAPSHOT 9 | maven-plugin 10 | 11 | Git Build Hook Maven Plugin 12 | A plugin for installing local hooks and running basic git related configuration during your Maven build. 13 | http://www.rudikershaw.com 14 | 15 | 16 | 17 | MIT License 18 | http://www.opensource.org/licenses/mit-license.php 19 | 20 | 21 | 22 | 23 | 24 | Rudi A. Kershaw 25 | alexander_kershaw@hotmail.co.uk 26 | None 27 | https://www.rudikershaw.com 28 | 29 | 30 | 31 | 32 | scm:git:git://github.com/rudikershaw/git-build-hook.git 33 | scm:git:ssh://github.com:rudikershaw/git-build-hook.git 34 | http://github.com/rudikershaw/git-build-hook/tree/main 35 | 36 | 37 | 38 | 3.0.0 39 | 40 | 41 | 42 | 1.8 43 | UTF-8 44 | ${java.version} 45 | ${java.version} 46 | 3.9.0 47 | 48 | 49 | 50 | 51 | org.apache.maven 52 | maven-plugin-api 53 | ${maven.version} 54 | provided 55 | 56 | 57 | org.apache.maven 58 | maven-compat 59 | ${maven.version} 60 | test 61 | 62 | 63 | org.apache.maven.plugin-tools 64 | maven-plugin-annotations 65 | 3.6.4 66 | provided 67 | 68 | 69 | junit 70 | junit 71 | 4.13.2 72 | test 73 | 74 | 75 | org.apache.maven.plugin-testing 76 | maven-plugin-testing-harness 77 | 3.3.0 78 | test 79 | 80 | 81 | org.eclipse.jgit 82 | org.eclipse.jgit 83 | 5.13.1.202206130422-r 84 | 85 | 86 | org.apache.maven 87 | maven-project 88 | 2.2.1 89 | provided 90 | 91 | 92 | org.apache.maven.shared 93 | maven-verifier 94 | 1.7.2 95 | test 96 | 97 | 98 | org.slf4j 99 | slf4j-api 100 | 2.0.0 101 | test 102 | 103 | 104 | org.slf4j 105 | slf4j-simple 106 | 2.0.0 107 | test 108 | 109 | 110 | commons-io 111 | commons-io 112 | 2.11.0 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | org.apache.maven.plugins 122 | maven-jar-plugin 123 | 3.2.2 124 | 125 | 126 | package-for-testing 127 | process-test-resources 128 | 129 | TEST 130 | 131 | 132 | jar 133 | 134 | 135 | 136 | package-cphooks-for-testing 137 | process-test-resources 138 | 139 | CPHOOKS 140 | 141 | com/rudikershaw/gitbuildhook/tests/cphooks/** 142 | 143 | 144 | 145 | test-jar 146 | 147 | 148 | 149 | 150 | 151 | org.apache.maven.plugins 152 | maven-install-plugin 153 | 3.0.1 154 | 155 | 156 | install-for-testing 157 | 158 | ${project.basedir}/target/${project.artifactId}-${project.version}-TEST.jar 159 | ${project.basedir}/target/test-repo 160 | 161 | process-test-classes 162 | 163 | install-file 164 | 165 | 166 | 167 | install-cphooks-for-testing 168 | 169 | com.rudikershaw.gitbuildhook 170 | testing-cp-hooks 171 | 1.2.3 172 | ${project.basedir}/target/${project.artifactId}-${project.version}-CPHOOKS.jar 173 | ${project.basedir}/target/test-repo 174 | 175 | process-test-classes 176 | 177 | install-file 178 | 179 | 180 | 181 | 182 | 183 | com.rudikershaw 184 | remove-attached-artifact-maven-plugin 185 | 1.1.0 186 | 187 | 188 | remove-attached-artifacts 189 | 190 | remove 191 | 192 | 193 | 194 | ${project.groupId}:${project.artifactId}:jar:TEST:${project.version} 195 | ${project.groupId}:${project.artifactId}:test-jar:CPHOOKS:${project.version} 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | org.apache.maven.plugins 204 | maven-plugin-plugin 205 | 3.6.4 206 | 207 | 208 | org.apache.maven.plugins 209 | maven-checkstyle-plugin 210 | 3.1.2 211 | 212 | etc/checkstyle/checkstyle.xml 213 | 214 | 215 | 216 | 217 | check 218 | 219 | 220 | 221 | 222 | 223 | com.github.spotbugs 224 | spotbugs-maven-plugin 225 | 4.7.1.1 226 | 227 | 228 | 229 | check 230 | 231 | 232 | 233 | 234 | 235 | org.apache.maven.plugins 236 | maven-pmd-plugin 237 | 3.17.0 238 | 239 | true 240 | 100 241 | ${java.version} 242 | true 243 | false 244 | true 245 | 246 | 247 | 248 | test 249 | 250 | pmd 251 | cpd 252 | cpd-check 253 | check 254 | 255 | 256 | 257 | 258 | 259 | org.apache.maven.plugins 260 | maven-source-plugin 261 | 3.2.1 262 | 263 | 264 | attach-sources 265 | 266 | jar 267 | 268 | 269 | 270 | 271 | 272 | org.apache.maven.plugins 273 | maven-javadoc-plugin 274 | 3.4.1 275 | 276 | 277 | attach-javadocs 278 | 279 | jar 280 | 281 | 282 | 283 | 284 | 285 | org.sonatype.plugins 286 | nexus-staging-maven-plugin 287 | 1.6.13 288 | true 289 | 290 | ossrh 291 | https://oss.sonatype.org/ 292 | true 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | ossrh 301 | https://oss.sonatype.org/content/repositories/snapshots 302 | 303 | 304 | 305 | 306 | 307 | deploy 308 | 309 | 310 | deploy 311 | true 312 | 313 | 314 | 315 | 316 | 317 | org.apache.maven.plugins 318 | maven-gpg-plugin 319 | 3.0.1 320 | 321 | 322 | sign-artifacts 323 | verify 324 | 325 | sign 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | -------------------------------------------------------------------------------- /src/main/java/com/rudikershaw/gitbuildhook/GitConfigMojo.java: -------------------------------------------------------------------------------- 1 | package com.rudikershaw.gitbuildhook; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.util.Map; 6 | 7 | import com.rudikershaw.gitbuildhook.threadsafety.ClassLock; 8 | import org.apache.maven.plugin.AbstractMojo; 9 | import org.apache.maven.plugin.MojoFailureException; 10 | import org.apache.maven.plugins.annotations.LifecyclePhase; 11 | import org.apache.maven.plugins.annotations.Mojo; 12 | import org.apache.maven.plugins.annotations.Parameter; 13 | import org.apache.maven.project.MavenProject; 14 | import org.eclipse.jgit.api.Git; 15 | import org.eclipse.jgit.lib.StoredConfig; 16 | import org.eclipse.jgit.storage.file.FileRepositoryBuilder; 17 | 18 | import com.rudikershaw.gitbuildhook.validation.GitRepositoryValidator; 19 | 20 | /** Mojo for specifying a custom git config settings for your Git repository. */ 21 | @Mojo(name = "configure", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true) 22 | public class GitConfigMojo extends AbstractMojo implements GitRepositoryValidator { 23 | 24 | /** Injected MavenProject containing project related information such as base directory. */ 25 | @Parameter(defaultValue = "${project}", readonly = true, required = true) 26 | private MavenProject project; 27 | 28 | /** The git config to set and the values to set them to. */ 29 | @Parameter 30 | private Map gitConfig; 31 | 32 | /** Skip specifying custom git config settings. */ 33 | @Parameter(property = "gitbuildhook.gitconfig.skip", defaultValue = "false") 34 | private boolean skip; 35 | 36 | @Override 37 | public void execute() throws MojoFailureException { 38 | if (skip) { 39 | getLog().debug("Skipping"); 40 | return; 41 | } 42 | 43 | // This goal requires the project to have a git repository initialized. 44 | validateGitRepository(project); 45 | 46 | final FileRepositoryBuilder repoBuilder = new FileRepositoryBuilder(); 47 | repoBuilder.findGitDir(project.getBasedir()); 48 | 49 | final String worktreePath = ".git" + File.separator + "worktrees" + File.separator; 50 | if (repoBuilder.getGitDir().getAbsolutePath().contains(worktreePath)) { 51 | getLog().warn( 52 | "The plugin appears to be running in a Git worktree. " 53 | + "No configuration changes made." 54 | ); 55 | return; 56 | } 57 | 58 | synchronized (ClassLock.class) { 59 | try (Git git = Git.open(repoBuilder.getGitDir())) { 60 | final StoredConfig config = git.getRepository().getConfig(); 61 | for (final Map.Entry entry : gitConfig.entrySet()) { 62 | final String[] conf = stringToConfigArray(entry.getKey()); 63 | config.setString(conf[0], conf[1], conf[2], entry.getValue()); 64 | getLog().info("Git config '" + entry.getKey() + "' set to - " + entry.getValue()); 65 | } 66 | config.save(); 67 | } catch (final IOException ioe) { 68 | failBuildBecauseRepoCouldNotBeFound(ioe); 69 | } 70 | } 71 | } 72 | 73 | /** 74 | * Takes a git config key string (e.g. core.hooksPath) and splits it into section, subsection, and name. 75 | * The former two are set to null is missing from the provided string. 76 | * 77 | * @param string a git config key string. 78 | * @return an array representing git config key section, subsection, and name respectively. 79 | * 80 | * @throws MojoFailureException if the git config key string is invalid. 81 | */ 82 | private String[] stringToConfigArray(final String string) throws MojoFailureException { 83 | final String[] split = string.split("\\."); 84 | final byte sections = 3; 85 | if (split.length > sections || split.length < 2) { 86 | throw new MojoFailureException("Git config '" + string + "' must include 1-2 sections separated by stops."); 87 | } 88 | 89 | final String name = split[split.length - 1]; 90 | final String subsection = split.length == sections ? split[1] : null; 91 | final String section = split[0]; 92 | 93 | return new String[]{section, subsection, name}; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/rudikershaw/gitbuildhook/InitialiseMojo.java: -------------------------------------------------------------------------------- 1 | package com.rudikershaw.gitbuildhook; 2 | 3 | import com.rudikershaw.gitbuildhook.threadsafety.ClassLock; 4 | import com.rudikershaw.gitbuildhook.validation.GitRepositoryValidator; 5 | import org.apache.maven.plugin.AbstractMojo; 6 | import org.apache.maven.plugin.MojoFailureException; 7 | import org.apache.maven.plugins.annotations.LifecyclePhase; 8 | import org.apache.maven.plugins.annotations.Mojo; 9 | import org.apache.maven.plugins.annotations.Parameter; 10 | import org.apache.maven.project.MavenProject; 11 | import org.eclipse.jgit.api.Git; 12 | import org.eclipse.jgit.api.errors.GitAPIException; 13 | 14 | /** Mojo for initializing a Git repository if one does not already exist. */ 15 | @Mojo(name = "initialize", defaultPhase = LifecyclePhase.INITIALIZE, threadSafe = true) 16 | public class InitialiseMojo extends AbstractMojo implements GitRepositoryValidator { 17 | 18 | /** Injected MavenProject containing project related information such as base directory. */ 19 | @Parameter(defaultValue = "${project}", readonly = true, required = true) 20 | private MavenProject project; 21 | 22 | /** Skip initialising the git repo. */ 23 | @Parameter(property = "gitbuildhook.init.skip", defaultValue = "false") 24 | private boolean skip; 25 | 26 | @Override 27 | public void execute() throws MojoFailureException { 28 | if (skip) { 29 | getLog().debug("Skipping"); 30 | return; 31 | } 32 | 33 | if (!isGitRepoInitialised(project)) { 34 | initialiseGitRepository(); 35 | } else { 36 | getLog().info("A Git repository is already initialized."); 37 | } 38 | } 39 | 40 | /** 41 | * Initialise a new git repository in the Maven project base directory. 42 | * 43 | * @throws MojoFailureException to fail the build and with details of the failure. 44 | */ 45 | private void initialiseGitRepository() throws MojoFailureException { 46 | try { 47 | synchronized (ClassLock.class) { 48 | Git.init().setDirectory(project.getBasedir()).call(); 49 | } 50 | } catch (final GitAPIException e) { 51 | if (!isGitRepoInitialised(project)) { 52 | throw new MojoFailureException("Could not initialise a local git repository.", e); 53 | } else { 54 | getLog().warn("Tried to initialize a Git repository, but a repository already exists."); 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/rudikershaw/gitbuildhook/InstallMojo.java: -------------------------------------------------------------------------------- 1 | package com.rudikershaw.gitbuildhook; 2 | 3 | import java.io.File; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.net.URL; 7 | import java.nio.file.Files; 8 | import java.nio.file.Paths; 9 | import java.nio.file.StandardCopyOption; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | import java.util.Objects; 13 | 14 | import org.apache.commons.io.IOUtils; 15 | import org.apache.maven.plugin.AbstractMojo; 16 | import org.apache.maven.plugin.MojoFailureException; 17 | import org.apache.maven.plugins.annotations.LifecyclePhase; 18 | import org.apache.maven.plugins.annotations.Mojo; 19 | import org.apache.maven.plugins.annotations.Parameter; 20 | import org.apache.maven.project.MavenProject; 21 | import org.eclipse.jgit.storage.file.FileRepositoryBuilder; 22 | 23 | import com.rudikershaw.gitbuildhook.hook.type.GitHookType; 24 | import com.rudikershaw.gitbuildhook.validation.GitRepositoryValidator; 25 | 26 | /** Mojo for installing Git hooks. */ 27 | @Mojo(name = "install", defaultPhase = LifecyclePhase.PROCESS_SOURCES) 28 | public class InstallMojo extends AbstractMojo implements GitRepositoryValidator { 29 | 30 | /** The location of git hooks to install into the default hooks directory. */ 31 | @Parameter 32 | private final Map installHooks = new HashMap<>(); 33 | 34 | /** Injected MavenProject containing project related information such as base directory. */ 35 | @Parameter(defaultValue = "${project}", readonly = true, required = true) 36 | private MavenProject project; 37 | 38 | /** Skip installing hooks. */ 39 | @Parameter(property = "gitbuildhook.install.skip", defaultValue = "false") 40 | private boolean skip; 41 | 42 | @Override 43 | public void execute() throws MojoFailureException { 44 | if (skip) { 45 | getLog().debug("Skipping"); 46 | return; 47 | } 48 | 49 | // This goal requires the project to have a git repository initialized. 50 | validateGitRepository(project); 51 | ensureGitHooksDirectoryExists(); 52 | 53 | final FileRepositoryBuilder repoBuilder = new FileRepositoryBuilder(); 54 | repoBuilder.findGitDir(project.getBasedir()); 55 | final String hooksDirectory = repoBuilder.getGitDir().toString() + File.separator + "hooks"; 56 | 57 | for (final Map.Entry hook : installHooks.entrySet()) { 58 | final String hookName = hook.getKey(); 59 | if (GitHookType.isValidHookName(hookName)) { 60 | installGitHook(hookName, hook.getValue(), hooksDirectory); 61 | } else { 62 | throw new MojoFailureException("'" + hookName + "' is not a valid hook file name."); 63 | } 64 | } 65 | } 66 | 67 | /** 68 | * Create .git/hooks directory if one does not already exist. 69 | * 70 | * @throws MojoFailureException if the hooks directory could not be created. 71 | */ 72 | private void ensureGitHooksDirectoryExists() throws MojoFailureException { 73 | final FileRepositoryBuilder repoBuilder = new FileRepositoryBuilder(); 74 | repoBuilder.findGitDir(project.getBasedir()); 75 | final String hooksDirPath = repoBuilder.getGitDir().toString() 76 | + File.separator 77 | + "hooks"; 78 | final File hooksDirFile = new File(hooksDirPath); 79 | if (!hooksDirFile.exists() && !hooksDirFile.mkdirs()) { 80 | throw new MojoFailureException("Could not create .git/hooks directory."); 81 | } 82 | } 83 | 84 | /** 85 | * Take the file in the provided location and install it as a Git hook of the provided type. 86 | * 87 | * @param hookName the type of hook to install. 88 | * @param filePath the location of the file to install as a hook. 89 | * @param hooksDirectory the directory in which to install the hook. 90 | */ 91 | private void installGitHook(final String hookName, final String filePath, final String hooksDirectory) { 92 | if (Objects.nonNull(filePath)) { 93 | final String gitHookPathStr = hooksDirectory + File.separator + hookName; 94 | if (Paths.get(filePath).toFile().isFile()) { 95 | copyFromFile(filePath, gitHookPathStr); 96 | } else { 97 | copyFromClasspath(filePath, gitHookPathStr); 98 | } 99 | } 100 | } 101 | 102 | /** 103 | * Copies the specified file from the file system into the default hooks directory. 104 | * 105 | * @param filePath path to the file to use as the hook. 106 | * @param gitHookPathStr the location to move the file to. 107 | */ 108 | private void copyFromFile(final String filePath, final String gitHookPathStr) { 109 | try { 110 | Files.copy(Paths.get(filePath), Paths.get(gitHookPathStr), StandardCopyOption.REPLACE_EXISTING); 111 | } catch (final IOException e) { 112 | getLog().warn("Could not move file into .git/hooks directory", e); 113 | } 114 | } 115 | 116 | /** 117 | * Copies the specified file from the classpath into the the default hooks directory. 118 | * 119 | * @param filePath path to the file to use as the hook. 120 | * @param gitHookPathStr the location to move the file to. 121 | */ 122 | private void copyFromClasspath(final String filePath, final String gitHookPathStr) { 123 | final URL resource = this.getClass().getClassLoader().getResource(filePath); 124 | if (Objects.isNull(resource)) { 125 | getLog().warn("Could not find file on filesystem or classpath"); 126 | } else { 127 | try { 128 | final File gitHookFile = Paths.get(gitHookPathStr).toFile(); 129 | IOUtils.copy(resource.openStream(), new FileOutputStream(gitHookFile)); 130 | gitHookFile.setExecutable(true); 131 | } catch (final IOException e) { 132 | getLog().warn("Could not move file from classpath into .git/hooks directory", e); 133 | } 134 | } 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /src/main/java/com/rudikershaw/gitbuildhook/hook/type/GitHookType.java: -------------------------------------------------------------------------------- 1 | package com.rudikershaw.gitbuildhook.hook.type; 2 | 3 | import java.util.Arrays; 4 | 5 | /** Enumerated types of Git hook. */ 6 | public enum GitHookType { 7 | 8 | /** Pre commit hook. */ 9 | PRE_COMMIT("pre-commit"), 10 | 11 | /** Pre merge commit hook. */ 12 | PRE_MERGE_COMMIT("pre-merge-commit"), 13 | 14 | /** Pre push hook. */ 15 | PRE_PUSH("pre-push"), 16 | 17 | /** Pre rebase hook. */ 18 | PRE_REBASE("pre-rebase"), 19 | 20 | /** Commit message hook. */ 21 | COMMIT_MSG("commit-msg"), 22 | 23 | /** Prepare commit message. */ 24 | PREPARE_COMMIT_MSG("prepare-commit-msg"), 25 | 26 | /** The update hook. */ 27 | UPDATE("update"), 28 | 29 | /** The post update hook. */ 30 | POST_UPDATE("post-update"), 31 | 32 | /** The apply patch message hook. */ 33 | APPLYPATCH_MSG("applypatch-msg"), 34 | 35 | /** The pre apply patch hook. */ 36 | PRE_APPLYPATCH("pre-applypatch"), 37 | 38 | /** The post merge hook. */ 39 | POST_MERGE("post-merge"), 40 | 41 | /** Post apply patch hook. */ 42 | POST_APPLYPATCH("post-applypatch"), 43 | 44 | /** Post commit hook. */ 45 | POST_COMMIT("post-commit"), 46 | 47 | /** Post checkout hook. */ 48 | POST_CHECKOUT("post-checkout"), 49 | 50 | /** Pre receive hook. */ 51 | PRE_RECEIVE("pre-receive"), 52 | 53 | /** Proc receive hook. */ 54 | PROC_RECEIVE("proc-receive"), 55 | 56 | /** Post receive hook. */ 57 | POST_RECEIVE("post-receive"), 58 | 59 | /** Reference transaction hook. */ 60 | REFERENCE_TRANSACTION("reference-transaction"), 61 | 62 | /** Push to checkout hook. */ 63 | PUSH_TO_CHECKOUT("push-to-checkout"), 64 | 65 | /** Pre auto gc hook. */ 66 | PRE_AUTO_GC("pre-auto-gc"), 67 | 68 | /** Post rewrite hook. */ 69 | POST_REWRITE("post-rewrite"), 70 | 71 | /** Send email validate hook. */ 72 | SENDEMAIL_VALIDATE("sendemail-validate"), 73 | 74 | /** FS monitor watchman hook. */ 75 | FSMONITOR_WATCHMAN("fsmonitor-watchman"), 76 | 77 | /** P4 changelist hook. */ 78 | P4_CHANGELIST("p4-changelist"), 79 | 80 | /** P4 prepare changelist hook. */ 81 | P4_PREPARE_CHANGELIST("p4-prepare-changelist"), 82 | 83 | /** P4 post changelist hook. */ 84 | P4_POST_CHANGELIST("p4-post-changelist"), 85 | 86 | /** P4 pre submit hook. */ 87 | P4_PRE_SUBMIT("p4-pre-submit"), 88 | 89 | /** Post index change hook. */ 90 | POST_INDEX_CHANGE("post-index-change"); 91 | 92 | /** The pre-specified name of the file of this hook type. */ 93 | private final String hookFileName; 94 | 95 | /** 96 | * Constructor. 97 | * 98 | * @param fileName the name of the file for the hook of this type. 99 | */ 100 | GitHookType(final String fileName) { 101 | hookFileName = fileName; 102 | } 103 | 104 | /** 105 | * Gets the pre-specified name of the file for hooks of this type. 106 | * 107 | * @return the hook file name. 108 | */ 109 | public String getHookFileName() { 110 | return hookFileName; 111 | } 112 | 113 | /** 114 | * Takes a string filename and returns true if, and only if, the string is a valid hook name. 115 | * 116 | * @param filename the filename to check. 117 | * @return true if the provided string is a valid hook name, otherwise false. 118 | */ 119 | public static boolean isValidHookName(final String filename) { 120 | return Arrays.stream(values()).anyMatch(h -> h.getHookFileName().equals(filename)); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/main/java/com/rudikershaw/gitbuildhook/hook/type/package-info.java: -------------------------------------------------------------------------------- 1 | /** Package containing classes that relate to the different types of Git hook. */ 2 | package com.rudikershaw.gitbuildhook.hook.type; 3 | -------------------------------------------------------------------------------- /src/main/java/com/rudikershaw/gitbuildhook/package-info.java: -------------------------------------------------------------------------------- 1 | /** The Git Build Hook Maven Plugin's main package where the plugin's goals are defined. */ 2 | package com.rudikershaw.gitbuildhook; 3 | -------------------------------------------------------------------------------- /src/main/java/com/rudikershaw/gitbuildhook/threadsafety/ClassLock.java: -------------------------------------------------------------------------------- 1 | package com.rudikershaw.gitbuildhook.threadsafety; 2 | 3 | /** 4 | * Class to act as a universal lock for non-thread safe operations of the plugin. 5 | */ 6 | public class ClassLock { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/rudikershaw/gitbuildhook/threadsafety/package-info.java: -------------------------------------------------------------------------------- 1 | /** Package containing classes for aiding in the thread safety of the plugin. */ 2 | package com.rudikershaw.gitbuildhook.threadsafety; 3 | -------------------------------------------------------------------------------- /src/main/java/com/rudikershaw/gitbuildhook/validation/GitRepositoryValidator.java: -------------------------------------------------------------------------------- 1 | package com.rudikershaw.gitbuildhook.validation; 2 | 3 | import org.apache.maven.plugin.MojoFailureException; 4 | import org.apache.maven.project.MavenProject; 5 | import org.eclipse.jgit.storage.file.FileRepositoryBuilder; 6 | 7 | /** Interface containing the logic required to fail a goal if there is no valid git repository. */ 8 | public interface GitRepositoryValidator { 9 | 10 | /** 11 | * Check that a project has a git repository initialized. 12 | * 13 | * @param project the Maven project to check. 14 | * @throws MojoFailureException if no git repository could be found. 15 | */ 16 | default void validateGitRepository(final MavenProject project) throws MojoFailureException { 17 | if (!isGitRepoInitialised(project)) { 18 | failBuildBecauseRepoCouldNotBeFound(null); 19 | } 20 | } 21 | 22 | /** 23 | * Returns true if there is already a valid git repository, otherwise false. 24 | * 25 | * @param project the Maven project to check. 26 | * @return whether a git repository is initialized. 27 | */ 28 | default boolean isGitRepoInitialised(MavenProject project) { 29 | final FileRepositoryBuilder repoBuilder = new FileRepositoryBuilder(); 30 | repoBuilder.findGitDir(project.getBasedir()); 31 | return repoBuilder.getGitDir() != null; 32 | } 33 | 34 | /** 35 | * Throws a MojoFailureException to fail the build. 36 | * Notifies the user that a git repository could not be found. 37 | * 38 | * @param e an exception that caused the build to fail. 39 | * @throws MojoFailureException to fail the build and with details of the failure. 40 | */ 41 | default void failBuildBecauseRepoCouldNotBeFound(final Exception e) throws MojoFailureException { 42 | final String message = "Could not find or initialise a local git repository. A repository is required."; 43 | throw new MojoFailureException(message, e); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/rudikershaw/gitbuildhook/validation/package-info.java: -------------------------------------------------------------------------------- 1 | /** The Git Build Hook Maven Plugin's validation package for logic that performs validation common between goals. */ 2 | package com.rudikershaw.gitbuildhook.validation; 3 | -------------------------------------------------------------------------------- /src/test/java/com/rudikershaw/gitbuildhook/AbstractMojoTest.java: -------------------------------------------------------------------------------- 1 | package com.rudikershaw.gitbuildhook; 2 | 3 | import org.apache.commons.io.FileUtils; 4 | import org.apache.maven.it.VerificationException; 5 | import org.apache.maven.it.Verifier; 6 | import org.apache.maven.plugin.testing.MojoRule; 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Rule; 10 | import org.junit.rules.TemporaryFolder; 11 | 12 | import java.io.File; 13 | import java.io.IOException; 14 | import java.nio.file.Files; 15 | import java.nio.file.Paths; 16 | import java.nio.file.StandardCopyOption; 17 | 18 | import static org.junit.Assert.assertTrue; 19 | 20 | /** Abstract test for Mojos. */ 21 | public class AbstractMojoTest { 22 | 23 | private final MojoRule rule = new MojoRule(); 24 | 25 | private final TemporaryFolder folder = new TemporaryFolder(); 26 | 27 | /** 28 | * Returns the rule. 29 | * 30 | * @return the rule. 31 | */ 32 | @Rule 33 | public MojoRule getRule() { 34 | return rule; 35 | } 36 | 37 | /** 38 | * Returns the folder. 39 | * 40 | * @return the folder. 41 | */ 42 | @Rule 43 | public TemporaryFolder getFolder() { 44 | return folder; 45 | } 46 | 47 | @Before 48 | public void setup() throws IOException { 49 | FileUtils.copyDirectory(new File("./.mvn"), new File(folder.getRoot().getAbsolutePath() + "/.mvn")); 50 | Files.copy(Paths.get("mvnw"), Paths.get(folder.getRoot().getAbsolutePath() + "/mvnw")); 51 | Files.copy(Paths.get("mvnw.cmd"), Paths.get(folder.getRoot().getAbsolutePath() + "/mvnw.cmd")); 52 | } 53 | 54 | @After 55 | public void teardown() throws IOException { 56 | FileUtils.deleteDirectory(folder.getRoot()); 57 | } 58 | 59 | /** 60 | * Move a file for a specified test folder into a temporary directory for testing. 61 | * 62 | * @param testName the name of the test directory in which the files are kept. 63 | * @param fileName the name of the file to move into the temporary directory. 64 | * @throws IOException if moving the file in question fails. 65 | */ 66 | protected void moveToTempTestDirectory(final String testName, final String fileName) throws IOException { 67 | moveToTempTestDirectory(testName, fileName, fileName); 68 | } 69 | 70 | /** 71 | * Move a file for a specified test folder into a temporary directory for testing. 72 | * 73 | * @param testName the name of the test directory in which the files are kept. 74 | * @param fileName the name of the file to move into the temporary directory. 75 | * @throws IOException if moving the file in question fails. 76 | */ 77 | protected void moveToTempTestDirectory(final String testName, final String fileName, final String newFileName) throws IOException { 78 | Files.copy(Paths.get("target/test-classes/" + testName + "/" + fileName), 79 | Paths.get(folder.getRoot().getAbsolutePath() + "/" + newFileName), 80 | StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES); 81 | } 82 | 83 | /** 84 | * Gets a verifier configured to pull dependencies to a test repo that the project is installed to before tests. 85 | * 86 | * @param project the directory containing the pom.xml for testing. 87 | * @return a verifier. 88 | * @throws VerificationException 89 | */ 90 | protected Verifier getVerifier(final String project) throws VerificationException { 91 | final Verifier verifier = new Verifier(project); 92 | final File testRepsotiroyDirectory = new File("target/test-repo"); 93 | assertTrue("Plugin must be installed into a local repo for tests", testRepsotiroyDirectory.exists()); 94 | verifier.setLocalRepo(testRepsotiroyDirectory.getAbsolutePath()); 95 | return verifier; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/test/java/com/rudikershaw/gitbuildhook/GitConfigMojoTest.java: -------------------------------------------------------------------------------- 1 | package com.rudikershaw.gitbuildhook; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertTrue; 5 | 6 | import java.io.File; 7 | 8 | import org.apache.maven.it.Verifier; 9 | import org.eclipse.jgit.api.Git; 10 | import org.eclipse.jgit.storage.file.FileRepositoryBuilder; 11 | import org.junit.Test; 12 | 13 | /** Unit and integration tests for the GitBuildHookMojo. */ 14 | public class GitConfigMojoTest extends AbstractMojoTest { 15 | 16 | /** 17 | * Tests that the hook, installed by changing the hooks directory, prevents a commit. 18 | * 19 | * @throws Exception if a temp project cannot be created for testing. 20 | */ 21 | @Test 22 | public void testConfigureGitHooksDirectory() throws Exception { 23 | moveToTempTestDirectory("test-project-configure", "pom.xml"); 24 | final File rootFolder = getFolder().getRoot(); 25 | assertTrue(rootFolder.exists()); 26 | 27 | final Verifier verifier = getVerifier(rootFolder.toString()); 28 | verifier.executeGoal("install"); 29 | verifier.verifyErrorFreeLog(); 30 | verifier.assertFilePresent(".git"); 31 | verifier.resetStreams(); 32 | 33 | final FileRepositoryBuilder repoBuilder = new FileRepositoryBuilder(); 34 | repoBuilder.findGitDir(rootFolder); 35 | 36 | try (Git git = Git.open(repoBuilder.getGitDir())) { 37 | assertEquals("hooks-path/", git.getRepository().getConfig().getString("core", null, "hooksPath")); 38 | assertEquals("custom", git.getRepository().getConfig().getString("custom", "config", "name")); 39 | } 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /src/test/java/com/rudikershaw/gitbuildhook/InitializeMojoTest.java: -------------------------------------------------------------------------------- 1 | package com.rudikershaw.gitbuildhook; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import java.io.File; 6 | import java.io.IOException; 7 | 8 | import org.apache.maven.it.Verifier; 9 | import org.eclipse.jgit.api.Git; 10 | import org.junit.Test; 11 | 12 | /** Unit and integration tests for the GitBuildHookMojo. */ 13 | public class InitializeMojoTest extends AbstractMojoTest { 14 | 15 | /** 16 | * Tests that a new repo is initialised if none exists and the initialise flag is configured. 17 | * 18 | * @throws IOException if a temp project cannot be created for testing. 19 | */ 20 | @Test 21 | public void testInitialiseNewGitRepo() throws Exception { 22 | moveToTempTestDirectory("test-project-initialise", "pom.xml"); 23 | 24 | final File rootFolder = getFolder().getRoot(); 25 | assertTrue(rootFolder.exists()); 26 | 27 | verifyRepositoryInitializedWithoutErrors(rootFolder); 28 | } 29 | 30 | /** 31 | * Tests that the initialize goal does not fail the build when a git repository is already present. 32 | * 33 | * @throws IOException if a temp project cannot be created for testing. 34 | */ 35 | @Test 36 | public void testInitialiseOnExistingRepo() throws Exception { 37 | moveToTempTestDirectory("test-project-initialise", "pom.xml"); 38 | 39 | final File rootFolder = getFolder().getRoot(); 40 | assertTrue(rootFolder.exists()); 41 | Git.init().setDirectory(rootFolder).call(); 42 | 43 | verifyRepositoryInitializedWithoutErrors(rootFolder); 44 | } 45 | 46 | private void verifyRepositoryInitializedWithoutErrors(final File projectDir) throws Exception { 47 | final Verifier verifier = getVerifier(projectDir.toString()); 48 | verifier.executeGoal("install"); 49 | verifier.verifyErrorFreeLog(); 50 | verifier.assertFilePresent(".git"); 51 | verifier.resetStreams(); 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /src/test/java/com/rudikershaw/gitbuildhook/InstallMojoTest.java: -------------------------------------------------------------------------------- 1 | package com.rudikershaw.gitbuildhook; 2 | 3 | import static org.junit.Assert.assertNotNull; 4 | import static org.junit.Assert.assertTrue; 5 | 6 | import java.io.File; 7 | import java.io.IOException; 8 | import java.util.List; 9 | 10 | import org.apache.commons.io.FileUtils; 11 | import org.apache.maven.it.Verifier; 12 | import org.apache.maven.plugin.MojoFailureException; 13 | import org.junit.Test; 14 | 15 | /** Unit and integration tests for the GitBuildHookMojo. */ 16 | public class InstallMojoTest extends AbstractMojoTest { 17 | 18 | /** 19 | * Test the basic running of the plugin. 20 | * 21 | * @throws Exception if anything goes wrong. 22 | */ 23 | @Test 24 | public void testDefaultSuccessfulRun() throws Exception { 25 | final File pom = new File("target/test-classes/default-test-project/"); 26 | assertNotNull(pom); 27 | assertTrue(pom.exists()); 28 | 29 | final InstallMojo installMojo = (InstallMojo) getRule().lookupConfiguredMojo(pom, "install"); 30 | assertNotNull(installMojo); 31 | installMojo.execute(); 32 | } 33 | 34 | /** 35 | * Tests that the plugin fails the build when the project is not managed by Git. 36 | * 37 | * @throws IOException if a temp project cannot be created for testing. 38 | */ 39 | @Test(expected = MojoFailureException.class) 40 | public void testFailureFromLackingGitRepo() throws Exception { 41 | moveToTempTestDirectory("default-test-project", "pom.xml"); 42 | 43 | final File rootFolder = getFolder().getRoot(); 44 | assertTrue(rootFolder.exists()); 45 | final InstallMojo installMojo = (InstallMojo) getRule().lookupConfiguredMojo(rootFolder, "install"); 46 | assertNotNull(installMojo); 47 | installMojo.execute(); 48 | } 49 | 50 | /** 51 | * Tests that a new repo is initialised if none exists and the initialise flag is configured. 52 | * 53 | * @throws IOException if a temp project cannot be created for testing. 54 | */ 55 | @Test 56 | public void testInstallTwoHooks() throws Exception { 57 | moveToTempTestDirectory("test-project-install-hooks", "pom.xml"); 58 | moveToTempTestDirectory("test-project-install-hooks", "hook-to-install.sh"); 59 | 60 | final File rootFolder = getFolder().getRoot(); 61 | assertTrue(rootFolder.exists()); 62 | final Verifier verifier = getVerifier(rootFolder.toString()); 63 | verifier.executeGoal("install"); 64 | verifier.verifyErrorFreeLog(); 65 | verifier.assertFilePresent(".git/hooks/pre-commit"); 66 | verifier.assertFilePresent(".git/hooks/pre-push"); 67 | verifier.assertFilePresent(".git/hooks/pre-rebase"); 68 | verifier.assertFilePresent(".git/hooks/commit-msg"); 69 | verifier.assertFilePresent(".git/hooks/prepare-commit-msg"); 70 | verifier.assertFilePresent(".git/hooks/update"); 71 | verifier.assertFilePresent(".git/hooks/post-update"); 72 | verifier.assertFilePresent(".git/hooks/applypatch-msg"); 73 | verifier.assertFilePresent(".git/hooks/pre-applypatch"); 74 | verifier.resetStreams(); 75 | } 76 | 77 | @Test 78 | public void testMissingHooksDirectory() throws Exception { 79 | moveToTempTestDirectory("test-project-install-hooks", "pom.xml"); 80 | moveToTempTestDirectory("test-project-install-hooks", "hook-to-install.sh"); 81 | 82 | final File rootFolder = getFolder().getRoot(); 83 | assertTrue(rootFolder.exists()); 84 | final Verifier verifier = getVerifier(rootFolder.toString()); 85 | verifier.executeGoal("install"); 86 | verifier.verifyErrorFreeLog(); 87 | verifier.resetStreams(); 88 | verifier.assertFilePresent(".git/hooks"); 89 | 90 | final File hooksFolder = rootFolder.toPath() 91 | .resolve(".git") 92 | .resolve("hooks") 93 | .toFile(); 94 | FileUtils.deleteDirectory(hooksFolder); 95 | verifier.assertFileNotPresent(".git/hooks"); 96 | 97 | verifier.executeGoal("install"); 98 | verifier.verifyErrorFreeLog(); 99 | verifier.assertFilePresent(".git/hooks/pre-commit"); 100 | verifier.assertFilePresent(".git/hooks/pre-push"); 101 | verifier.assertFilePresent(".git/hooks/pre-rebase"); 102 | verifier.assertFilePresent(".git/hooks/commit-msg"); 103 | verifier.assertFilePresent(".git/hooks/prepare-commit-msg"); 104 | verifier.assertFilePresent(".git/hooks/update"); 105 | verifier.assertFilePresent(".git/hooks/post-update"); 106 | verifier.assertFilePresent(".git/hooks/applypatch-msg"); 107 | verifier.assertFilePresent(".git/hooks/pre-applypatch"); 108 | verifier.resetStreams(); 109 | } 110 | 111 | /** 112 | * Test that you can update hooks by installing one file over another. 113 | * 114 | * @throws Exception if a temp project cannot be created for testing. 115 | */ 116 | @Test 117 | public void testUpdateHooks() throws Exception { 118 | moveToTempTestDirectory("test-project-reinstall-hooks", "pom.xml"); 119 | moveToTempTestDirectory("test-project-reinstall-hooks", "hook-to-install.sh"); 120 | moveToTempTestDirectory("test-project-reinstall-hooks", "hook-to-reinstall.sh"); 121 | 122 | final File rootFolder = getFolder().getRoot(); 123 | Verifier verifier = getVerifier(rootFolder.toString()); 124 | verifier.executeGoal("install"); 125 | verifier.verifyErrorFreeLog(); 126 | verifier.assertFilePresent(".git/hooks/pre-commit"); 127 | verifier.assertFilePresent(".git/hooks/commit-msg"); 128 | 129 | List lines = verifier.loadFile(new File(rootFolder, ".git/hooks/pre-commit"), false); 130 | assertTrue(lines.contains("install")); 131 | List origionalCommitMsgLines = verifier.loadFile(new File(rootFolder, ".git/hooks/commit-msg"), false); 132 | assertTrue(origionalCommitMsgLines.contains("origional hook")); 133 | 134 | moveToTempTestDirectory("test-project-reinstall-hooks", "pom2.xml", "pom.xml"); 135 | verifier = getVerifier(rootFolder.toString()); 136 | verifier.executeGoal("install"); 137 | verifier.verifyErrorFreeLog(); 138 | verifier.assertFilePresent(".git/hooks/pre-commit"); 139 | verifier.assertFilePresent(".git/hooks/commit-msg"); 140 | 141 | lines = verifier.loadFile(new File(rootFolder, ".git/hooks/pre-commit"), false); 142 | assertTrue(lines.contains("reinstall")); 143 | List updatedCommitMsgLines = verifier.loadFile(new File(rootFolder, ".git/hooks/commit-msg"), false); 144 | assertTrue(updatedCommitMsgLines.contains("updated hook")); 145 | } 146 | 147 | /** 148 | * Tests that the plugin fails when we specify installing an invalid named hook. 149 | * 150 | * @throws IOException if a temp project cannot be created for testing. 151 | */ 152 | @Test(expected = MojoFailureException.class) 153 | public void testFailureFromInvalidHookNames() throws Exception { 154 | moveToTempTestDirectory("test-project-invalid-hook", "pom.xml"); 155 | 156 | final File rootFolder = getFolder().getRoot(); 157 | assertTrue(rootFolder.exists()); 158 | final InstallMojo installMojo = (InstallMojo) getRule().lookupConfiguredMojo(rootFolder, "install"); 159 | assertNotNull(installMojo); 160 | installMojo.execute(); 161 | } 162 | 163 | } 164 | -------------------------------------------------------------------------------- /src/test/resources/com/rudikershaw/gitbuildhook/tests/cphooks/hook-to-install-from-cp.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | echo "A git thing happened installed from classpath 2" 3 | -------------------------------------------------------------------------------- /src/test/resources/com/rudikershaw/gitbuildhook/tests/cphooks/reinstall-hook-origional.sh: -------------------------------------------------------------------------------- 1 | origional hook -------------------------------------------------------------------------------- /src/test/resources/com/rudikershaw/gitbuildhook/tests/cphooks/reinstall-hook-updated.sh: -------------------------------------------------------------------------------- 1 | updated hook -------------------------------------------------------------------------------- /src/test/resources/default-test-project/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.rudikershaw.gitbuildhook 8 | test-artifact 9 | 1.0.0-SNAPSHOT 10 | jar 11 | Test MyMojo 12 | 13 | 14 | 15 | 16 | com.rudikershaw.gitbuildhook 17 | git-build-hook-maven-plugin 18 | 3.6.0-SNAPSHOT 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/test/resources/test-project-configure/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.rudikershaw.gitbuildhook 8 | test-artifact 9 | 1.0.0-SNAPSHOT 10 | jar 11 | Test MyMojo 12 | 13 | 14 | 15 | 16 | com.rudikershaw.gitbuildhook 17 | git-build-hook-maven-plugin 18 | 3.6.0-SNAPSHOT 19 | 20 | 21 | hooks-path/ 22 | custom 23 | 24 | 25 | 26 | 27 | 28 | initialize 29 | configure 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/test/resources/test-project-initialise/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.rudikershaw.gitbuildhook 8 | test-artifact 9 | 1.0.0-SNAPSHOT 10 | jar 11 | Test MyMojo 12 | 13 | 14 | 15 | 16 | com.rudikershaw.gitbuildhook 17 | git-build-hook-maven-plugin 18 | 3.6.0-SNAPSHOT 19 | 20 | 21 | 22 | initialize 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/test/resources/test-project-install-hooks/hook-to-install.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | echo "A git thing happened" 3 | -------------------------------------------------------------------------------- /src/test/resources/test-project-install-hooks/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.rudikershaw.gitbuildhook 8 | test-artifact 9 | 1.0.0-SNAPSHOT 10 | jar 11 | Test MyMojo 12 | 13 | 14 | 15 | 16 | com.rudikershaw.gitbuildhook 17 | git-build-hook-maven-plugin 18 | 3.6.0-SNAPSHOT 19 | 20 | 21 | hook-to-install.sh 22 | hook-to-install.sh 23 | hook-to-install.sh 24 | hook-to-install.sh 25 | hook-to-install.sh 26 | hook-to-install.sh 27 | com/rudikershaw/gitbuildhook/tests/cphooks/hook-to-install-from-cp.sh 28 | com/rudikershaw/gitbuildhook/tests/cphooks/hook-to-install-from-cp.sh 29 | com/rudikershaw/gitbuildhook/tests/cphooks/hook-to-install-from-cp.sh 30 | com/rudikershaw/gitbuildhook/tests/cphooks/hook-to-install-from-cp.sh 31 | 32 | 33 | 34 | 35 | com.rudikershaw.gitbuildhook 36 | testing-cp-hooks 37 | 1.2.3 38 | 39 | 40 | 41 | 42 | 43 | initialize 44 | install 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /src/test/resources/test-project-invalid-hook/hook-to-install.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | echo "A git thing happened" 3 | -------------------------------------------------------------------------------- /src/test/resources/test-project-invalid-hook/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.rudikershaw.gitbuildhook 8 | test-artifact 9 | 1.0.0-SNAPSHOT 10 | jar 11 | Test MyMojo 12 | 13 | 14 | 15 | 16 | com.rudikershaw.gitbuildhook 17 | git-build-hook-maven-plugin 18 | 3.6.0-SNAPSHOT 19 | 20 | 21 | hook-to-install.sh 22 | 23 | 24 | 25 | 26 | 27 | initialize 28 | install 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/test/resources/test-project-reinstall-hooks/hook-to-install.sh: -------------------------------------------------------------------------------- 1 | install 2 | -------------------------------------------------------------------------------- /src/test/resources/test-project-reinstall-hooks/hook-to-reinstall.sh: -------------------------------------------------------------------------------- 1 | reinstall 2 | -------------------------------------------------------------------------------- /src/test/resources/test-project-reinstall-hooks/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.rudikershaw.gitbuildhook 8 | test-artifact 9 | 1.0.0-SNAPSHOT 10 | jar 11 | Test MyMojo 12 | 13 | 14 | 15 | 16 | com.rudikershaw.gitbuildhook 17 | git-build-hook-maven-plugin 18 | 3.6.0-SNAPSHOT 19 | 20 | 21 | hook-to-install.sh 22 | com/rudikershaw/gitbuildhook/tests/cphooks/reinstall-hook-origional.sh 23 | 24 | 25 | 26 | 27 | com.rudikershaw.gitbuildhook 28 | testing-cp-hooks 29 | 1.2.3 30 | 31 | 32 | 33 | 34 | 35 | initialize 36 | install 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/test/resources/test-project-reinstall-hooks/pom2.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.rudikershaw.gitbuildhook 8 | test-artifact 9 | 1.0.0-SNAPSHOT 10 | jar 11 | Test MyMojo 12 | 13 | 14 | 15 | 16 | com.rudikershaw.gitbuildhook 17 | git-build-hook-maven-plugin 18 | 3.6.0-SNAPSHOT 19 | 20 | 21 | hook-to-reinstall.sh 22 | com/rudikershaw/gitbuildhook/tests/cphooks/reinstall-hook-updated.sh 23 | 24 | 25 | 26 | 27 | com.rudikershaw.gitbuildhook 28 | testing-cp-hooks 29 | 1.2.3 30 | 31 | 32 | 33 | 34 | 35 | initialize 36 | install 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | --------------------------------------------------------------------------------