Skip to content

Verify Code Format as well as License Header presence during CI build #168

Closed

Description

@vorburger

I'm raising this issue prompted by https://github.com/google/adk-java/pull/102/files#r2132521541:

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?

@glaforge @Poggecci @cornellgit do you have any thought about / input on this?

Activity

  1. glaforge commented on Jun 6, 2025

    @glaforge
    Contributor

    We could set up checkstyle since committers will be building with Maven?

  2. vorburger commented on Jun 6, 2025

    @vorburger
    MemberAuthor

    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).

  3. glaforge commented on Jun 6, 2025

    @glaforge
    Contributor

    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.

  4. shukladivyansh commented on Jun 6, 2025

    @shukladivyansh
    Contributor

    +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.

  5. vorburger commented on Jun 6, 2025

    @vorburger
    MemberAuthor

    /assign @vorburger #blockedBy #155 -- I'll (only) look further into this after we conclude that.

  6. vorburger commented on Jun 11, 2025

    @vorburger
    MemberAuthor

    #183 (WIP?) takes stab at the license header part of this.

  7. vorburger commented on Jun 20, 2025

    @vorburger
    MemberAuthor

    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.

  8. tbroyer commented on Jun 20, 2025

    @tbroyer

    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

  9. vorburger commented on Jul 18, 2025

    @vorburger
    MemberAuthor

    @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.

  10. kaikreuzer commented on Jul 18, 2025

    @kaikreuzer
    Contributor

    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.

  11. vorburger commented on Jul 18, 2025

    @vorburger
    MemberAuthor

    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:

    1. 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.

    2. 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.

    3. Run git status && git diff --exit-code in 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.

    FWIW, I am using Spotless since many years and it could definitely be a nice complementary tool besides GJF.

    Spotless is definitely awesome - but sometimes "more is less"... the primary PITA was Java format - and that's (hopefully) solved.

    You mention it very prominently in the issue title, though. 😄

    I changed my mind! 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions