Git Product home page Git Product logo

pre-commit-java's Introduction

pre-commit-java

pre-commit update

A collection of git hooks for Java to be used with the pre-commit framework.

Requirements

pre-commit-java requires the following to run:

Install

  1. create .pre-commit-config.yaml in you git project
  2. pre-commit install
  3. enjoy :)

example .pre-commit-config.yaml:

- repo: https://github.com/gherynos/pre-commit-java
  rev: v0.2.1  # Use the ref you want to point at
  hooks:
    - id: pmd
      exclude: /test/
    - id: cpd
      exclude: /test/
    - id: checkstyle
      exclude: /test/
    # ...

Available Hooks

Hook name Description
pmd Runs the PMD static code analyzer.
cpd Runs the Copy/Paste Detector (CPD).
checkstyle Runs the Checkstyle static code analysis tool.

Notes about the pmd hook

The default ruleset used is ruleset.xml, which runs all the Java rules, except for LoosePackageCoupling.

To specify a custom ruleset, simply pass the argument to the hook:

    - id: pmd
      args: ["-R", "my_ruleset.xml"]

Furthermore, the default language version passed to PMD is java-17, which can be tweaked using the --use-version argument.

Other CLI arguments are also supported, like --cache for incremental analysis.

Notes about the cpd hook

The default minimum token length is 100.

To change it, simply pass the argument to the hook:

    - id: cpd
      args: ["--minimum-tokens", "50"]

Other CLI arguments are also supported.

Notes about the checkstyle hook

The default configuration used is sun_checks.xml.

To specify a custom one, simply pass the argument to the hook:

    - id: checkstyle
      args: ["-c", "my_checks.xml"]

Other CLI arguments are also supported.

Other configurations part of the distribution can be used like:

    - id: checkstyle
      args: ["-c", "/google_checks.xml"]

Author

GitHub @gherynos

License

pre-commit-java is licensed under the GPLv3 license.

pre-commit-java's People

Contributors

gherynos avatar github-actions[bot] avatar siner308 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pre-commit-java's Issues

CLI options for CPD not working

With the example given in the readme of:

    - id: cpd
      args: ["--minimum-tokens", "50"]

I get the following output:

pre-commit run -a cpd -vv
cpd......................................................................Passed
- hook id: cpd
- duration: 1.37s

[ERROR] No such file --minimum-tokens
[ERROR] No such file 50

Note that this still passes despite the errors.

If I try an older version (v0.3.1) it still doesn't work, but I get a different error:

pre-commit run -a cpd -vv
cpd......................................................................Failed
- hook id: cpd
- duration: 1.27s
- exit code: 1

Can only specify option --minimum-tokens once.
Run with --help for command line help.

I would also like to run cpd over python rather than java.
I would have assumed that something like:

  - repo: https://github.com/gherynos/pre-commit-java
    rev: v0.4.2
    hooks:
      - id: cpd
        args: ["--minimum-tokens", "50", "--language", "python"]
        files: py$ 

should work, but gets similar issues with No such file --language, No such file python or I did also manage to get it to say Can only specify option --language once. which might be because --language java is hardcoded in run_cpd.sh

PMD -language option is deprecated

The -language (-l) option has been deprecated since PMD 6.52.0.

/opt/pmd/bin/run.sh pmd -f textcolor -language java --file-list /tmp/list $pc_args

Results in:

WARNING: Some deprecated options were used on the command-line, including -language
WARNING: Consider replacing it with --use-version

References:

Passing custom pmd ruleset isn't working

The optional -R parameter doesn't work.
Results in pmd error File -R doesn't exist

.pre-commit-config.yaml

  - repo: https://github.com/gherynos/pre-commit-java
    rev: v0.1.0
    hooks:
      - id: pmd
        exclude: /test/
        args: ["-R", "my_ruleset.xml"]

Output

pmd......................................................................Failed
hookid: pmd

Mandatory arguments:
1) A java source code filename or directory
2) A report format
3) A ruleset filename or a comma-delimited string of ruleset filenames

For example:
C:\>pmd-bin-6.34.0\bin\pmd.bat -d c:\my\source\code -f html -R java-unusedcode

Languages and version suported:
apex, ecmascript, java, jsp, modelica, plsql, pom, scala, vf, vm, wsdl, xml, xsl

Available report formats and their configuration properties are:
   codeclimate: Code Climate integration.
   csv: Comma-separated values tabular format.
        problem - Include Problem column   default: true
        package - Include Package column   default: true
        file - Include File column   default: true
        priority - Include Priority column   default: true
        line - Include Line column   default: true
        desc - Include Description column   default: true
        ruleSet - Include Rule set column   default: true
        rule - Include Rule column   default: true
   emacs: GNU Emacs integration.
   empty: Empty, nothing.
   html: HTML format
        linePrefix - Prefix for line number anchor in the source file.
        linkPrefix - Path to HTML source.
        htmlExtension - Replace file extension with .html for the links (default: false)   default: false
   ideaj: IntelliJ IDEA integration.
        classAndMethodName - Class and Method name, pass '.method' when processing a directory.   default:
        sourcePath - Source path.   default:
        fileName - File name.   default:
   json: JSON format.
   sarif: Static Analysis Results Interchange Format (SARIF)
   summaryhtml: Summary HTML format.
        linePrefix - Prefix for line number anchor in the source file.
        linkPrefix - Path to HTML source.
        htmlExtension - Replace file extension with .html for the links (default: false)   default: false
   text: Text format.
   textcolor: Text format, with color support (requires ANSI console support, e.g. xterm, rxvt, etc.).
        color - Enables colors with anything other than 'false' or '0'.   default: yes
   textpad: TextPad integration.
   vbhtml: Vladimir Bossicard HTML format.
   xml: XML format.
        encoding - XML encoding format, defaults to UTF-8.   default: UTF-8
   xslt: XML with a XSL Transformation applied.
        encoding - XML encoding format, defaults to UTF-8.   default: UTF-8
        xsltFilename - The XSLT file name.
   yahtml: Yet Another HTML format.
        outputDir - Output directory.

For example on windows:
C:\>pmd-bin-6.34.0\bin\pmd.bat -dir c:\my\source\code -format text -R rulesets/java/quickstart.xml -version 1.5 -language java -debug
C:\>pmd-bin-6.34.0\bin\pmd.bat -dir c:\my\source\code -f xml -rulesets rulesets/java/quickstart.xml,category/java/codestyle.xml -encoding UTF-8
C:\>pmd-bin-6.34.0\bin\pmd.bat -d c:\my\source\code -rulesets rulesets/java/quickstart.xml -auxclasspath lib\commons-collections.jar;lib\derby.jar
C:\>pmd-bin-6.34.0\bin\pmd.bat -d c:\my\source\code -f html -R rulesets/java/quickstart.xml -auxclasspath file:///C:/my/classpathfile

For example on *nix:
$ pmd-bin-6.34.0/bin/run.sh pmd -dir /home/workspace/src/main/java/code -f html -rulesets rulesets/java/quickstart.xml,category/java/codestyle.xml
$ pmd-bin-6.34.0/bin/run.sh pmd -d ./src/main/java/code -R rulesets/java/quickstart.xml -f xslt -property xsltFilename=my-own.xsl
$ pmd-bin-6.34.0/bin/run.sh pmd -d ./src/main/java/code -f html -R rulesets/java/quickstart.xml -auxclasspath commons-collections.jar:derby.jar
File -R doesn't exist

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.