google-java-formatter
for code formatting
Since google-java-formatter
brings modifications (formatting) to the code, then it changes the code to be commited.
As per google-java-format source code:
google-java-format
is a program that reformats Java source code to comply with Google Java Style.
So, you need a pre-commit
hook.
E.g. you can use pre-commit - A framework for managing and maintaining multi-language pre-commit hooks.
Sample file you can see here
.pre-commit-hooks.yaml:
- id: eclipse-formatter
name: Eclipse Java Formatter
description: This hook formats Java code with the Eclipse formatter.
entry: eclipse-formatter
language: python
types:
- java
- id: google-java-formatter
name: Google Java Formatter
description: This hook formats Java code with Google Java Formatter.
entry: google-java-formatter
language: python
types:
- java
If you do want to integrate git hooks with GitLab
, try to create custom GitLab Server hook
linters vs formatters
As you said:
when committing code I want the CI pipeline to check if the formatting has been done first.
What are you asking - is checkstyle linting
not formatting
So, to check if formatting is already done, you can use some linter.
I.e. from this answer:
- SpotBugs (earlier Findbugs) for finding existing bugs. VERY GOOD!
- PMD for finding patterns that can lead to bugs (e.g. unused variables)
- Checkstyle to enforce coding standards and conventions (e.g. whitespace, Javadoc)
- Error Prone hooks right into your application's compile step
checkstyle
linter usage
There are many guides on that, e.g: GitLab CI/CD Pipeline for Maven-Based Applications – The Blog of Ivan Krizsan
Also, there are 250+ samples of .gitlab-ci.yml
with `checkstyle.
Client_Checkstyle:
stage: test
script:
- mvn checkstyle:checkstyle
- cat checkstyle-result.xml
allow_failure: false