[Java] Fix generated build.gradle incompatible with Gradle 9

Merged
SE
3mo ago
+487-281

Description

Gradle 9 removed the implicit Project.sourceCompatibility / targetCompatibility convenience properties (previously provided via Groovy convention mapping) as well as JavaExec.main. The Java client build.gradle.mustache templates still used both directly, so every generated Gradle project failed to even configure under Gradle 9:

FAILURE: Build failed with an exception.

* Where:
Build file '.../build.gradle' line: 81

* What went wrong:
A problem occurred evaluating root project 'client'.
> Could not set unknown property 'sourceCompatibility' for root project 'client' of type org.gradle.api.Project.

This matches the reproduction and suggested fix in #22084, where @wing328 asked for a PR implementing exactly this template update.

Fix

  • Wrapped sourceCompatibility / targetCompatibility in a java { } extension block (valid on Gradle 4+ through 9) instead of assigning them as top-level project properties, across all Java client libraries: default, apache-httpclient, feign, google-api-client, jersey2, jersey3, native, okhttp-gson, rest-assured, resteasy, retrofit2, vertx.
  • While verifying a full Gradle 9 build of each library, found a second, closely related Gradle 9 removal in the same templates: the execute task's main = System.getProperty('mainClass') (JavaExec.main was removed in Gradle 9, replaced by mainClass). Renamed it to mainClass in the same set of libraries (webclient, resttemplate, and restclient already used mainClass and were unaffected).

Verification done

  • Rebuilt openapi-generator-cli from source and generated a sample client for every affected library (resttemplate, webclient, okhttp-gson, jersey3, native, feign, retrofit2, apache-httpclient, rest-assured, google-api-client, resteasy, jersey2, restclient, vertx), including the useJackson3/java17/useJakartaEe/useVertx5 conditional branches.
  • Reproduced the exact reported failure against a real Gradle 9.0.0 distribution on the unfixed template (Could not set unknown property 'sourceCompatibility'), then confirmed compileJava now succeeds with the fix on 11/13 libraries out of the box. The remaining 2 (resteasy, vertx) fail only on an unrelated, pre-existing dependency resolution problem (a pinned jackson_version that doesn't correspond to a published artifact) โ€” unrelated to this fix and reproducible identically before/after this change.
  • ./mvnw -pl modules/openapi-generator -am test -Dtest=JavaClientCodegenTest -Dsurefire.failIfNoSpecifiedTests=false โ†’ 265 tests, 0 failures/errors.
  • Regenerated all 88 affected sample configs via ./bin/generate-samples.sh and confirmed the resulting samples/**/build.gradle diffs contain exactly this change (no unrelated drift).

Fixes #22084


Summary by cubic

Make generated Java clients compatible with Gradle 9 by moving Java version settings into the java {} block and replacing JavaExec.main with mainClass. Projects now configure and build on Gradle 9 without errors (fixes #22084).

  • Bug Fixes
    • Wrapped sourceCompatibility/targetCompatibility in a java {} extension across all Java client templates (works on Gradle 4โ€“9).
    • Renamed JavaExec.main to mainClass in execute tasks where needed; templates already using mainClass were unchanged.