[Java] Fix generated build.gradle incompatible with Gradle 9
Merged
SE
3mo ago+487+487+012345678901234567890123456789-281-281-012345678901234567890123456789
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/targetCompatibilityin ajava { }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
executetask'smain = System.getProperty('mainClass')(JavaExec.mainwas removed in Gradle 9, replaced bymainClass). Renamed it tomainClassin the same set of libraries (webclient,resttemplate, andrestclientalready usedmainClassand were unaffected).
Verification done
- Rebuilt
openapi-generator-clifrom 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 theuseJackson3/java17/useJakartaEe/useVertx5conditional 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 confirmedcompileJavanow 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 pinnedjackson_versionthat 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.shand confirmed the resultingsamples/**/build.gradlediffs 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/targetCompatibilityin ajava {}extension across all Java client templates (works on Gradle 4โ9). - Renamed
JavaExec.maintomainClassinexecutetasks where needed; templates already usingmainClasswere unchanged.
- Wrapped