Video in progress

Staff 🌿 Git

Should generated code (an OpenAPI client, protobuf, jOOQ classes) be stored in Git?

Usually not: the source (spec, .proto, migrations) is in Git and the code is generated at build time. But if generation is expensive, unreliable or depends on an external resource, or if seeing how the output changes in review matters, committing it pays off, with a "is it up to date" check in CI.

📖 Answer

If you do not store it:

  • the source and the output never drift apart;
  • the diffs are clean, with no thousands of automatic lines in PRs;
  • merge conflicts do not happen in generated files.

When storing pays off:

  • jOOQ generation needs a running DB, so every build needs Testcontainers or a ready schema;
  • the reviewer sees exactly what a spec change leads to in code (for example, that updating the OpenAPI generator changed every client signature);
  • the IDE works without a build, which is simpler for a new developer.

If you choose to commit it, two things are mandatory:

# CI: generate, then check there is no difference
./mvnw -q generate-sources
git diff --exit-code -- src/generated/ || { echo "Generated kod eskirgan"; exit 1; }

and src/generated/** linguist-generated=true in .gitattributes, so GitHub collapses them in the PR diff.

The same goes for vendored dependencies (copying an external library into the repo): in Java it is usually not needed, since there is Maven/Gradle and an internal proxy (Nexus). If you only need a patched fork, it is better to publish it as a separate artifact than to drop it inside the repo.

My usual decision: protobuf and OpenAPI are generated in the build; jOOQ depends on the project and is often committed, because building without a DB is very convenient.