Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

20 Java tools that can be wrapped as pre-commit hooks. Gemini can make mistakes, so double-check it #11320

Open
Assignees
Labels
javaPull requests that update Java code long-term-plan
Milestone
@jbampton

Description

pre-commit hooks are a fantastic way to enforce code quality and standards in Java projects right at the developer's workstation, providing immediate feedback. The key is to select tools that are fast enough not to hinder the commit process, as slower checks are better suited for CI/CD pipelines.

Here are 20 Java tools (or tools commonly used in Java projects) that can be wrapped as pre-commit hooks:

I. Code Formatting & Style:

  • 1. Spotless: A powerful and flexible code formatter that supports various Java formatters (like GoogleJavaFormat, Eclipse JDT). It can automatically apply formatting fixes before a commit, ensuring consistent style.
  • 2. Checkstyle: A static analysis tool that checks Java source code against a set of configurable coding standards. It can prevent commits if the code doesn't adhere to the defined style guidelines.
  • 3. Google Java Format: An opinionated code formatter from Google that formats Java code according to Google's style guide. It's often integrated via Spotless or directly as a hook.
  • 4. Palantir Java Format: Another opinionated Java code formatter, similar to Google Java Format, promoting a specific and consistent code style.

II. Linting & Static Analysis:

  • 5. PMD: A static analysis tool that identifies common programming flaws, dead code, duplicated code, and other potential issues in Java source code. It's highly configurable with various rule sets.
  • 6. SpotBugs: The successor to FindBugs, this tool uses static analysis to detect various bug patterns and potential vulnerabilities in Java bytecode.
  • 7. SonarLint (via SonarQube integration): While SonarLint is an IDE extension, its underlying analysis engine (from SonarQube) can be invoked as part of a pre-commit check to identify code quality and security issues.
  • 8. Error Prone: A static analysis tool developed by Google that catches common Java programming mistakes at compile time. It can be integrated into the build process and triggered by a pre-commit hook.

III. Dependency Management & Security:

  • 9. OWASP Dependency-Check: This Software Composition Analysis (SCA) tool identifies known vulnerabilities in project dependencies by analyzing their CPE identifiers against public vulnerability databases. Essential for catching insecure libraries early.
  • 10. Maven Enforcer Plugin (for Maven projects): This Maven plugin can enforce various rules, such as checking for dependency convergence issues, banning specific dependencies, or ensuring certain build properties are set. It can be integrated into a pre-commit hook by running a specific Maven goal.
  • 11. Gitleaks / TruffleHog: These tools scan for sensitive information (like API keys, passwords, private keys) accidentally committed to the repository. They are critical for security and should be high priority for pre-commit hooks.

IV. Testing (for quick, critical checks):

  • 12. JUnit: While full test suites are typically run in CI, a pre-commit hook can execute a very small, fast-running subset of critical unit tests to prevent obvious breakages before committing. This needs careful consideration to avoid slowing down commits.
  • 13. JaCoCo (via Maven/Gradle plugin): A code coverage library. A pre-commit hook could be configured to fail if code coverage for changed lines drops below a certain threshold, encouraging developers to write tests for new or modified code.

V. General/Utility Tools (applicable to Java projects):

  • 14. GitLint: Ensures that commit messages adhere to a defined format (e.g., Conventional Commits). This is crucial for maintaining a clean commit history and for automated changelog generation.
  • 15. EditorConfig (Validation): While not a tool itself, pre-commit hooks can validate that files adhere to the rules defined in .editorconfig (e.g., indentation style, line endings), ensuring consistency across different editors.
  • 16. ShellCheck: If your Java project includes shell scripts (e.g., build scripts, deployment scripts), ShellCheck can lint these scripts for common errors and bad practices.
  • 17. Prettier (for non-Java files): If your Java project also contains front-end code (JavaScript, CSS, HTML, JSON) or configuration files, Prettier can format them consistently. While not a Java-specific tool, it's often used in polyglot JVM projects.
  • 18. Lombok (via delombok): If you use Lombok, a pre-commit hook could run delombok to expand Lombok annotations before committing, ensuring the committed code is standard Java, which can be beneficial for other tools that don't fully understand Lombok.
  • 19. Jacoco (via build tool check): To ensure newly added or modified code is covered by tests, a pre-commit hook can trigger a check using your build tool's Jacoco plugin (e.g., mvn jacoco:check or gradle jacocoTestReport).
  • 20. java -jar for custom checks: For specific, internal code quality rules unique to your project, you can write a simple Java program and compile it into a JAR. Then, execute this JAR via a pre-commit hook (java -jar your-custom-checker.jar). This allows for highly tailored and project-specific checks.

When implementing these, consider using a pre-commit framework like pre-commit (the Python tool, despite its name, is language-agnostic) which simplifies managing and executing hooks.

Metadata

Metadata

Labels

javaPull requests that update Java code long-term-plan

Type

Projects

Status

Todo

Relationships

None yet

Development

No branches or pull requests

Issue actions

    AltStyle によって変換されたページ (->オリジナル) /