4

I am looking to install google-java-formatter across several of my projects.

However when committing code I want the CI pipline to check if the formatting has been done first.

I know I can accomplish this with a .gitlab-ci.yml file in my root directory however I am very unsure how to achieve my goal of checking if all files have been formatted correctly, can anyone help me out with how you could do this with google-java-formatter and gitlab please.

Yasen
  • 4,241
  • 1
  • 16
  • 25
Broken Mind
  • 511
  • 3
  • 8
  • 22
  • You can also use the [spotless plugin](https://github.com/diffplug/spotless) if you use SBT, Maven or Gradle. – flaxel Nov 05 '20 at 22:43

1 Answers1

6

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
Yasen
  • 4,241
  • 1
  • 16
  • 25
  • Im still a bit unsure how to use this in gitlab, I add this file to my root directory and then will the pipeline pick up on this file? I am using inteliJ if that makes any difference to the answer – Broken Mind Apr 21 '20 at 14:21
  • Added some links and ready example piece of `.gitlab-ci.yml`, pls check – Yasen Apr 21 '20 at 14:31
  • @Yasen Is Gitlab necessary for this? – drfalcoew Jan 26 '23 at 17:41