如何在本地实现git提交后自动执行mvn verify及推送前预执行该命令
mvn verify Before Git Commit & Push + IntelliJ Tips Hey there! Let’s get you set up so you never forget to run mvn verify before committing or pushing code—no more pipeline failures due to missed checks. Here’s how to do it with Git’s built-in hooks, plus some IntelliJ-specific tweaks to make your workflow even smoother.
1. Use Git Hooks for Automatic Checks
Git has built-in hook scripts that run at specific points in the workflow (like before commit or push). These are stored in your repo’s .git/hooks folder—here’s how to configure them:
Pre-Commit Hook (Runs Before git commit)
- Navigate to your repo’s
.git/hooksdirectory. You’ll see sample files likepre-commit.sample—copy this file and rename it topre-commit(remove the.samplesuffix). - Open the new
pre-commitfile in a text editor and replace its content with this script:
#!/bin/sh # Auto-run mvn verify before commit echo "🔍 Running mvn verify to validate build..." mvn verify # Abort commit if mvn verify fails if [ $? -ne 0 ]; then echo "❌ mvn verify failed! Commit aborted. Fix issues before trying again." exit 1 fi echo "✅ mvn verify passed. Proceeding with commit." exit 0
- Make the script executable: run
chmod +x .git/hooks/pre-commitin Git Bash.
Now every time you run git commit, mvn verify will execute automatically. If it fails, your commit gets blocked—perfect for catching issues early!
Pre-Push Hook (Runs Before git push)
To add the same check before pushing, follow similar steps:
- Copy
.git/hooks/pre-push.sampleto.git/hooks/pre-push(remove the.samplesuffix). - Edit the
pre-pushfile with this script:
#!/bin/sh # Auto-run mvn verify before push echo "🔍 Running mvn verify before push..." mvn verify # Abort push if mvn verify fails if [ $? -ne 0 ]; then echo "❌ mvn verify failed! Push aborted. Resolve errors first." exit 1 fi echo "✅ mvn verify passed. Proceeding with push." exit 0
- Make it executable:
chmod +x .git/hooks/pre-push.
Share Hooks with Your Team
If you want everyone on your team to use these hooks, don’t rely on .git/hooks (it’s not tracked by Git). Instead:
- Create a
.git-hooksfolder in your repo’s root. - Move your
pre-commitandpre-pushscripts into this folder. - Run
git config core.hooksPath .git-hooksto tell Git to use this directory for hooks. - Commit the
.git-hooksfolder to your repo so teammates can pull it in.
2. IntelliJ IDEA Configurations & Plugins
If you use IntelliJ, you can integrate these checks directly into your IDE workflow, or use plugins to simplify things:
Built-in IntelliJ Commit Checks
You don’t need plugins to set up pre-commit Maven checks in IntelliJ:
- Open Settings (File > Settings, or press
Ctrl+Alt+Son Windows/Linux,Cmd+,on Mac). - Navigate to Version Control > Git > Commit.
- In the Before commit section, check the box for Run Maven Goal.
- Click the
+button, select your project, and enterverifyas the goal. - Click Apply and OK.
Now whenever you commit via IntelliJ’s commit dialog, mvn verify will run automatically. If it fails, the commit won’t go through.
Recommended Plugins
- Git Hooks Helper: Makes managing Git hooks in IntelliJ visual and easy—no need to edit shell scripts manually. You can add, edit, and enable hooks right from the IDE.
- Maven Helper: While not Git-specific, it streamlines Maven tasks (like running
verifyquickly, examining dependencies, or troubleshooting build errors) to pair seamlessly with your Git workflow. - Commit Checker: Extends pre-commit checks beyond just
mvn verify—you can add rules for code style, test coverage, commit message formatting, and more to enforce consistent quality standards.
Quick Note: Skipping Hooks (Use Sparingly!)
If you ever need to bypass the checks (e.g., an emergency commit where you’re certain the build is fine), you can use:
git commit --no-verifyto skip the pre-commit hook.git push --no-verifyto skip the pre-push hook.
Only do this when you’re 100% sure—otherwise, you risk breaking the pipeline!
内容的提问来源于stack exchange,提问作者Ashish Prajapat

