Is there a maven-git-checkstyle plugin that runs checkstyle goal on git staged files alone?
Asked Answered
S

1

1

We are working on a legacy app code base and doing incremental development to improve its code quality. When we ran the maven checkstyle goal on the project we got tons of errors. To address the issues incrementally, we are taking an approach to run checkstyle only on the files which each developer is editing and before committing.
The options I ran into while search for a solution are pre-commit hooks which executes a maven goal before committing the code.
References:
https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
https://hdpe.me/post/maven-checkstyle-pre-commit-hook/

The disadvantage of the above approach is it cannot be enforced on each developers machine. Since the hooks are not uploaded into the repository. We have our own limitations(team politics) in implementing server side hooks.

IntelliJ checkstyle plugin has the exact functionality which I am looking for (Check All Modified Files).
Check All Modified Files

But, not all developers in our team use IntelliJ and It cannot be enforced during the build process.

I guess what I am trying to ask is, is there a way to trigger the pre-commit hook(logic) as a maven goal?

Stag answered 25/4, 2018 at 7:46 Comment(0)
S
0

Is there a maven-git-checkstyle plugin that runs checkstyle goal on git staged files alone?

There is no plugin that does this that I am aware of, but you have 2 options that I can think of to achieve this goal.

1) Checkstyle can be extended so you can implement your own Filter. Filters are what allows you to ignore and hide specific violations, instead of disabling the whole check. Integrate JGit into your Filter and you will be able to access the list of staged files and any commits for the repository. Just have the filter suppress all violations for any files that don't appear in the list of staged files. You can customize the filter further for your specific needs.

2) Create a list of files you wish to suppress using SuppressionFilter and store them into a unique legacy suppression file. You can customize the list for the type of suppression you want (Ex: by file only, by file and check name, etc...). As your developers work on the project, remove the specific suppressions for the files they modify. You can enforce this by a UT if you, again, integrate JGit, and ensure none of the modified files are listed in the legacy suppression file.

Either route you take, once you are done upgrading all legacy files, I recommend removing these legacy suppressions and always run against the full workspace.

Sipes answered 25/4, 2018 at 17:4 Comment(1)
Thank you for your reply. I'll try it out before accepting your answer.Stag

© 2022 - 2024 — McMap. All rights reserved.