You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Verify Code Format as well as License Header presence during CI build #168
Is your feature request related to a problem? Please describe.
IMHO it would make the life of committers (maintainers) easier if this project would verify Code Format as well as missing License Header presence during CI build (and thus cause red build failures).
IMHO it would also make the life of contributors (PR raising community members) easier if this project had a very easy way to auto-format code according to the standards that this projects expects its code to be formatted with.
Describe the solution you'd like
The following is for discussion, but if it was up to me, I would go about this simply by adding a suitable pre-commit hook configuration, for both the Google Java Code Formatters, and the license headers checker/adder. (I have used and done this in a project of mine, and ready to copy/paste this here.)
Describe alternatives you've considered
Waste time of both committers and contributors by leaving "nit pick" comments about such things on PRs.
Using pre-commit would avoid requiring Python to be installed to run the pre-commit, but IMHO nowadays that shouldn't really be an issue. (And one could still locally build the project without it; just without the pre-commit.)
An alternative to avoid ^^^ would be to integrate the Google Java Code Formatters using (one of) its Maven Plugins. But I don't know (haven't looked into) a Maven license header checker, and as far as I know that's harder to easily cleanly integrate nicely as an automagic pre-commit.
Additional context
I'm happy to help by proposing a PR for this, if the project's committers concur that this would be useful?
Checkstyle is obviously great, and I've happily used it for 25 years! But I suspect that contributions to the code in this repository will very likely be expected to be formatted with Google's Java Format (GJF). And https://github.com/google/google-java-format actually really is pretty awesome! (I've even made some, very very minor, contributions to it myself!) And keeping a Checkstyle config manually aligned with the format that Google's Java Format expects is going to be a huge unmaintainable maintenance nightmare, IMHO. So personally I would recommand that we simply adopt (hear, enforce) GJF here in this project. Thoughts?
PS: Just to avoid confusion, the "Java/Building with Maven" vs. https://pre-commit.com (Python) dimension is tehcnically completely orthogonal to GFM or not (because we could integrate GFM validation and autoformat through various mechanisms).
I didn't know GJF! Sounds like it's going to be the best fit for the job here.
Ideally, we should be able to run it from Maven (and potentially from IDEs), and make sure that when GitHub actions build the project, they go through GJF too.
@tbroyer Thank You for pointing to the (great!) spotless-maven-plugin in this context!
The primary motivation was formatting Java code to be compliant with a Google internal mirror of this code.
Formatting pom.xml and *.md, as well as license headers, was much more of a "nice to have" than the code.
The Java format should have been achieved with #210 - it's just interesting to see that there appear to be inconsistencies?! Something like #279 from @kaikreuzer "shouldn't be required"... I'll see if I can find the time to better understand what's going on there.
I'm inclined to close this issue as resolved once that ^^^ is clarified / fixed, and ignore license, MD, POM etc.
@glaforge@shukladivyansh do shout if you disagree with that as the plan for how to wrap this up.
Formatting [...] license headers, was much more of a "nice to have" than the code.
You mention it very prominently in the issue title, though. 😄
FWIW, I am using Spotless since many years and it could definitely be a nice complementary tool besides GJF.
The Java format should have been achieved with #210 - it's just interesting to see that there appear to be inconsistencies?! Something like #279 from @kaikreuzer "shouldn't be required"... I'll see if I can find the time to better understand what's going on there.
This ^^^ was a "valid" manual reformat. To avoid this, our CI should probably fail for ill-formatted Java code?
I've further investigated several options for how this could be done:
Using https://github.com/spotify/fmt-maven-plugin: It CAN do this - but I was not able to make it "format and fail IFF formatted"... at least not nicely and smoothlessly, as I think this needs to be. You can do with a separate Maven goal.. but then folks have to always manually invoke that - which is a PITA, to avoid. If it "just formats during the build", then the check never fails (I just tried it) - because it's ALREADY formatted, at that stage. So No-Go.
Using git status / diff! Initially I ran it with https://www.mojohaus.org/exec-maven-plugin/ ... but that's actually a PITA as well, because during "normal local development", one WILL have dirty files... so No-Go.
Activity
glaforge commentedon Jun 6, 2025
We could set up checkstyle since committers will be building with Maven?
vorburger commentedon Jun 6, 2025
Checkstyle is obviously great, and I've happily used it for 25 years! But I suspect that contributions to the code in this repository will very likely be expected to be formatted with Google's Java Format (GJF). And https://github.com/google/google-java-format actually really is pretty awesome! (I've even made some, very very minor, contributions to it myself!) And keeping a Checkstyle config manually aligned with the format that Google's Java Format expects is going to be a huge unmaintainable maintenance nightmare, IMHO. So personally I would recommand that we simply adopt (hear, enforce) GJF here in this project. Thoughts?
PS: Just to avoid confusion, the "Java/Building with Maven" vs. https://pre-commit.com (Python) dimension is tehcnically completely orthogonal to GFM or not (because we could integrate GFM validation and autoformat through various mechanisms).
glaforge commentedon Jun 6, 2025
I didn't know GJF! Sounds like it's going to be the best fit for the job here.
Ideally, we should be able to run it from Maven (and potentially from IDEs), and make sure that when GitHub actions build the project, they go through GJF too.
shukladivyansh commentedon Jun 6, 2025
+1 to both google-java-format and licence header pre-commit checks.
agree with @vorburger on google-java-format since that's the one we use internally and send cls with.
vorburger commentedon Jun 6, 2025
/assign @vorburger #blockedBy #155 -- I'll (only) look further into this after we conclude that.
vorburger commentedon Jun 11, 2025
#183 (WIP?) takes stab at the license header part of this.
chore: Reformat contrib/langchain4j (re. google#168)
chore: Add google-java-format as Maven plugin (re. google#168)
vorburger commentedon Jun 20, 2025
FTR: I've worked a bit on this today, and raised the x3 PRs shown above.
I'm wondering if spotify/fmt-maven-plugin#184 and spotify/fmt-maven-plugin#197 (with spotify/fmt-maven-plugin#196) will be a problem for us.
tbroyer commentedon Jun 20, 2025
Fwiw, Spotless would fulfill your needs: add missing license headers and run GJF (could also format your POMs and Markdown files)
https://github.com/diffplug/spotless/tree/main/plugin-maven
vorburger commentedon Jul 18, 2025
@tbroyer Thank You for pointing to the (great!)
spotless-maven-pluginin this context!The primary motivation was formatting Java code to be compliant with a Google internal mirror of this code.
Formatting
pom.xmland*.md, as well as license headers, was much more of a "nice to have" than the code.The Java format should have been achieved with #210 - it's just interesting to see that there appear to be inconsistencies?! Something like #279 from @kaikreuzer "shouldn't be required"... I'll see if I can find the time to better understand what's going on there.
I'm inclined to close this issue as resolved once that ^^^ is clarified / fixed, and ignore license, MD, POM etc.
@glaforge @shukladivyansh do shout if you disagree with that as the plan for how to wrap this up.
kaikreuzer commentedon Jul 18, 2025
You mention it very prominently in the issue title, though. 😄
FWIW, I am using Spotless since many years and it could definitely be a nice complementary tool besides GJF.
chore: Fail CI build if there are wrongly formatted files (re. google…
vorburger commentedon Jul 18, 2025
This ^^^ was a "valid" manual reformat. To avoid this, our CI should probably fail for ill-formatted Java code?
I've further investigated several options for how this could be done:
Using https://github.com/spotify/fmt-maven-plugin: It CAN do this - but I was not able to make it "format and fail IFF formatted"... at least not nicely and smoothlessly, as I think this needs to be. You can do with a separate Maven goal.. but then folks have to always manually invoke that - which is a PITA, to avoid. If it "just formats during the build", then the
checknever fails (I just tried it) - because it's ALREADY formatted, at that stage. So No-Go.Using git status / diff! Initially I ran it with https://www.mojohaus.org/exec-maven-plugin/ ... but that's actually a PITA as well, because during "normal local development", one WILL have dirty files... so No-Go.
Run
git status && git diff --exit-codein the GitHub Action workflow AFTER Maven. That makes sense, and IMHO is the simplest. I've proposed this as chore: Fail CI build if there are wrongly formatted files (re. #168) #282.Spotless is definitely awesome - but sometimes "more is less"... the primary PITA was Java format - and that's (hopefully) solved.
I changed my mind! 😆
chore: Fail CI build if there are wrongly formatted files (re. google…