A repository demonstrating pre-commit hooks for Java projects with Maven, JUnit 5, Spotless (google-java-format), and Checkstyle.
- Maven project structure with JDK 11 compatibility
- JUnit 5 (latest version) for testing with parameterized tests
- Spotless with google-java-format for automatic code formatting
- Checkstyle with Google Java Style checks
- Pre-commit hooks with lint-staged style functionality (runs only on staged files)
- Dummy test classes to demonstrate the setup
- Java 11 or higher
- Maven 3.6 or higher
- Python 3.6+ (for pre-commit framework)
- pip3 (Python package manager)
Run the automated setup script:
./quick-start.shThis will automatically:
- Install pre-commit (if not already installed)
- Install git hooks
- Build the project
- Run pre-commit hooks on all files
git clone <repository-url>
cd pre-commit-hooks-for-javapip3 install pre-commitpre-commit install./verify-setup.shThis script will:
- Check if pre-commit is installed
- Check if git hooks are installed
- Verify Maven and Java installations
- Run all tests
- Run pre-commit hooks on all files
pre-commit run --all-filespre-commit-hooks-for-java/
βββ src/
β βββ main/
β β βββ java/
β β βββ com/
β β βββ precommit/
β β βββ demo/
β β βββ Calculator.java # Simple calculator class
β β βββ StringUtils.java # String utility class
β βββ test/
β βββ java/
β βββ com/
β βββ precommit/
β βββ demo/
β βββ CalculatorTest.java # JUnit 5 tests for Calculator
β βββ StringUtilsTest.java # JUnit 5 tests for StringUtils
βββ hooks/
β βββ spotless-hook.sh # Spotless formatting check hook
β βββ checkstyle-hook.sh # Checkstyle check hook
βββ pom.xml # Maven configuration
βββ .pre-commit-config.yaml # Pre-commit hooks configuration
βββ verify-setup.sh # Setup verification script
βββ README.md
# Run all tests
mvn test
# Run tests with coverage
mvn clean test
# Run a specific test class
mvn test -Dtest=CalculatorTest# Check formatting
mvn spotless:check
# Apply formatting
mvn spotless:apply# Run checkstyle
mvn checkstyle:check# Clean and compile
mvn clean compile
# Package the application
mvn clean package
# Run full build with all checks
mvn clean verifyThe project uses pre-commit hooks to ensure code quality before commits. The hooks include:
- trailing-whitespace: Removes trailing whitespace
- end-of-file-fixer: Ensures files end with a newline
- check-yaml: Validates YAML file syntax
- check-added-large-files: Prevents committing large files
- check-merge-conflict: Checks for merge conflict markers
- mixed-line-ending: Ensures consistent line endings
- Spotless Format Check: Runs google-java-format on staged Java files only
- Checkstyle: Runs Google Java Style checks on staged Java files only
- When you run
git commit, pre-commit automatically runs configured hooks - Java hooks only check staged files (similar to lint-staged in Node.js)
- If any hook fails, the commit is blocked
- Fix the issues and try committing again
# Run all hooks on all files
pre-commit run --all-files
# Run a specific hook
pre-commit run spotless-format-check --all-files
pre-commit run checkstyle --all-files
# Run hooks on specific files
pre-commit run --files src/main/java/com/precommit/demo/Calculator.java- Make changes to Java files
- Stage your changes:
git add src/main/java/com/precommit/demo/Calculator.java
- Try to commit:
git commit -m "Add new calculation method" - Pre-commit hooks run automatically on staged files
- If formatting issues are found:
mvn spotless:apply git add . git commit -m "Add new calculation method"
A simple calculator demonstrating:
- Basic arithmetic operations (add, subtract, multiply, divide)
- Exception handling (division by zero)
- Javadoc documentation
A string utility class demonstrating:
- String manipulation (reverse, palindrome check)
- Word counting
- Title case conversion
- JUnit 5 annotations (
@Test,@BeforeEach,@DisplayName) - Parameterized tests (
@ParameterizedTest,@CsvSource,@ValueSource) - Assertions (
assertEquals,assertThrows,assertTrue,assertFalse) - Test organization with nested test classes
- maven-compiler-plugin: Compiles Java code with JDK 11
- maven-surefire-plugin: Runs JUnit tests
- spotless-maven-plugin: Formats code with google-java-format
- maven-checkstyle-plugin: Validates code style with Google Java Style
junit-jupiter-api: JUnit 5 API for writing testsjunit-jupiter-engine: JUnit 5 test enginejunit-jupiter-params: Parameterized tests support
- Lint-staged Style: Pre-commit hooks only run on staged files, not the entire codebase
- Fast Feedback: Catches formatting and style issues before they enter version control
- Consistent Code Style: Enforces Google Java Style automatically
- Comprehensive Testing: Demonstrates various JUnit 5 features
- Easy to Extend: Simple to add more hooks or modify existing ones
When contributing to this repository:
- Ensure all tests pass:
mvn test - Format your code:
mvn spotless:apply - Check code style:
mvn checkstyle:check - Commit your changes (pre-commit hooks will run automatically)
See LICENSE file for details.