You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在本地实现git提交后自动执行mvn verify及推送前预执行该命令

Automate 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/hooks directory. You’ll see sample files like pre-commit.sample—copy this file and rename it to pre-commit (remove the .sample suffix).
  • Open the new pre-commit file 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-commit in 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.sample to .git/hooks/pre-push (remove the .sample suffix).
  • Edit the pre-push file 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-hooks folder in your repo’s root.
  • Move your pre-commit and pre-push scripts into this folder.
  • Run git config core.hooksPath .git-hooks to tell Git to use this directory for hooks.
  • Commit the .git-hooks folder 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:

  1. Open Settings (File > Settings, or press Ctrl+Alt+S on Windows/Linux, Cmd+, on Mac).
  2. Navigate to Version Control > Git > Commit.
  3. In the Before commit section, check the box for Run Maven Goal.
  4. Click the + button, select your project, and enter verify as the goal.
  5. 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.

  • 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 verify quickly, 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-verify to skip the pre-commit hook.
  • git push --no-verify to skip the pre-push hook.

Only do this when you’re 100% sure—otherwise, you risk breaking the pipeline!

内容的提问来源于stack exchange,提问作者Ashish Prajapat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 12:42:50