Resolving Git Hooks Issues in Multi-JDK Environments
The company uses Git commit hooks to perform pre-commit validation, but a multi-JDK environment on local machines (up to JDK 22) prevented the Maven Spotless plugin from running properly.
- 7 min read
- 2026.09.16
When multiple JDKs coexist, Maven Spotless may throw an `InvocationTargetException` due to a higher-version JDK. The solution is to install the required version using SDKMAN, place a `.sdkmanrc` file in the project root directory, and load SDKMAN at the beginning of the `pre-commit` hook to lock down the Java version used for building.
Resolving Issues with Git Hooks in Multi-JDK Environments
🧠 Cause
Our company uses a Git commit hook to perform pre-commit validation on the commit content and code before a Git commit is made. I have multiple JDK versions installed locally, with the latest being JDK 22. As a result, I encountered execution failures when using the Spotless plugin with Maven.
Maven Spotless 插件执行失败
java.lang.reflect. InvocationTargetException: 'com.sun.tools.javac.tree. JCTree com.sun.tools.javac.tree. JCTree$JCImport.getQualifiedIdentifier()'
- Maven spotless plugin execution
- JDK 版本不兼容导致的问题
- Spotless 插件与高版本 JDK 存在兼容性问题
- Git hooks 执行失败,阻止代码提交
Example error log:
[Help 1]
[ERROR] Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.30.0:apply(default-cli) on project qnvip-referral-client: Execution default-cli of goal com.diffplug.spotless:spotless-maven-plugin:2.30.0:apply failed: java.lang.reflect.InvocationTargetException: 'com.sun.tools.javac.tree.JCTree com.sun.tools.javac.tree.JCTree$JCImport.getQualifiedIdentifier()' -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn <args> -rf :qnvip-referral-client
husky - pre-commit script failed (code 1)
💼 Solution
⚙️ Installing SDKMAN
Refer to the SDKMAN Installation and Configuration Guide, which provides detailed instructions for both Mac and Windows environments.
⚙️ Install the Required JDK
Check Available Java Versions
Run the following command to view the Java versions currently available:
sdk list java
Install the JDK Required for Your Project
This environment currently uses JDK 8. Once you’ve identified the required version, run:
sdk install java 8.0.452-amzn
Verify the Installation
After installation, verify the Java version:
java -version
Expected output:
openjdk version "1.8.0_452"
OpenJDK Runtime Environment (build 1.8.0_452-b08)
OpenJDK 64-Bit Server VM (build 25.452-b08, mixed mode)
🔧 Configure SDKMAN in the Project
Project Directory Structure
Assume the project directory structure is as follows:
your-project/
├── .husky/
│ └── pre-commit
├── .sdkmanrc # sdkman配置
├── mvnw
├── mvnw.cmd
├── pom.xml
├── package.json
├── lint-staged.config.js
└── ...
Create the .sdkmanrc Configuration File
Create the .sdkmanrc file in the project root directory and enter the JDK information:
# This file is used by SDKMAN to automatically switch to the correct Java version
# when entering this project directory
#
# 注意:
# 1. 此文件需要 SDKMAN 支持才能生效
# 2. 如果您没有安装 SDKMAN,请参考 [SDKMAN 安装与配置指南](/blog/20250619-install-sdkman)
# 3. Windows 用户需要在 Git Bash 或类似的 Unix-like 环境中使用 SDKMAN
# 4. 如果您使用的是 IDE(如 IntelliJ IDEA),请确保在项目设置中也配置了正确的 JDK
#
# Note:
# 1. This file requires SDKMAN to take effect
# 2. If you haven't installed SDKMAN, please refer to the installation guide
# 3. Windows users need to use SDKMAN in Git Bash or similar Unix-like environment
# 4. If you're using an IDE (like IntelliJ IDEA), make sure to configure the correct JDK in project settings
java=8.0.452-amzn
🔧 Configure the pre-commit Hook
Complete pre-commit Script
Here is a complete pre-commit configuration file that supports cross-platform use:
#!/bin/sh
# Check if running on Windows or Unix-like system
case "$(uname -s)" in
MINGW*|MSYS*|CYGWIN*)
# Windows environment
IS_WINDOWS=true
SDKMAN_DIR="$USERPROFILE/.sdkman"
SDKMAN_INIT="$SDKMAN_DIR/bin/sdkman-init.sh"
;;
*)
# Unix-like environment (macOS, Linux, etc.)
IS_WINDOWS=false
SDKMAN_DIR="$HOME/.sdkman"
SDKMAN_INIT="$SDKMAN_DIR/bin/sdkman-init.sh"
;;
esac
# Check if .sdkmanrc file exists and SDKMAN is installed
if [ -f .sdkmanrc ] && [ -f "$SDKMAN_INIT" ]; then
echo "SDKMAN detected, activating project Java version"
# Load sdkman
source "$SDKMAN_INIT"
# Execute sdkman commands
sdk env
else
echo "SDKMAN not detected or .sdkmanrc not found. Using system Java version."
# Print current Java version for reference
if command -v java >/dev/null 2>&1; then
echo "Current Java version: $(java -version 2>&1 | head -n 1)"
else
echo "Warning: Java not found in PATH"
fi
fi
# Execute other pre-commit hooks
if [ -f "$HOME"/qnvip.gitconfig.sh ]; then
bash "$HOME"/qnvip.gitconfig.sh
fi
npx --no-install node .husky/scripts/modify-author-from-file.js
npx lint-staged --verbose --debug
Script Functionality Description
✅ Verify Configuration
Test Git Commit
After configuration is complete, try making a commit to verify that the configuration is correct:
git add .
git commit -m "test: 验证 SDKMAN 配置"
If you see output similar to the following, the configuration was successful:
SDKMAN detected, activating project Java version
Using java version 8.0.452-amzn in this shell.
Troubleshooting Common Issues
SDKMAN 未检测到
SDKMAN not detected or .sdkmanrc not found
- Project directory
- SDKMAN 未正确安装
- .sdkmanrc 文件不存在
- 需要先安装 SDKMAN
Solution: Refer to the SDKMAN Installation and Configuration Guide
Java 版本仍然不匹配
Java version mismatch in Git hooks
- Git hooks execution
- SDKMAN 未正确加载
- 环境变量配置问题
- 需要检查 shell 配置
Solution: Ensure SDKMAN is correctly loaded in your shell configuration file
🔗 Related Content
Related Blog Posts
- SDKMAN Installation and Configuration Guide - Detailed steps for installing and configuring SDKMAN
- Git Hooks and Node Environment Issues - Resolving Node.js environment issues in Git Hooks
- Multi-Environment Git Configuration Management - Managing Git configurations across multiple environments
Related Tools
- Husky - A Git hooks tool
- Lint-staged - Runs a linter on staged files
- Maven Spotless - Code formatting plugin