How to configure openapi-generator-gradle-plugin in a TOML file for Gradle 8.2.0?
I’m using Gradle 8.2.0 with the openapi-generator-gradle-plugin 7.2.0. The old format works fine:
buildscript {
repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
}
dependencies {
classpath group: "org.openapitools", name: "openapi-generator-gradle-plugin", version: "${openApiToolsVersion}"
}
}
apply plugin: 'org.openapi.generator'
However, I want to use the modern centralized TOML format. In my TOML file, I have:
[versions]
openapi-tools = "7.2.0"
[plugins]
openapi-generator = { id = "org.openapi.generator", version.ref = "openapi-tools" }
And in my build.gradle file, I’m using:
plugins {
alias(libs.plugins.openapi.generator)
}
But Gradle can’t find the dependency. What am I missing in this configuration?
To configure the openapi-generator-gradle-plugin in a TOML file for Gradle 8.2.0, you need to ensure both your TOML configuration and build.gradle file are properly set up. Here’s the complete solution:
Contents
- Correct TOML Configuration
- Build.gradle Setup
- Complete Working Example
- Common Issues and Troubleshooting
- Advanced Configuration Options
Correct TOML Configuration
Your TOML file structure looks correct, but there are a few details to ensure proper functionality. Create gradle/libs.versions.toml with this configuration:
[versions]
openapi-tools = "7.2.0"
[plugins]
openapi-generator = {
id = "org.openapi.generator",
version.ref = "openapi-tools"
}
Key points to verify:
- The file must be named exactly
libs.versions.tomland placed in yourgradle/directory - Use
version.refto reference the version from the[versions]section - The plugin ID must match exactly: “org.openapi.generator”
According to the Gradle documentation on version catalogs, this is the standard syntax for declaring plugins in TOML format.
Build.gradle Setup
In your build.gradle or build.gradle.kts file, use the plugin alias as you attempted:
plugins {
alias(libs.plugins.openapi.generator)
}
However, you also need to apply the plugin in your build script. Here’s the complete setup:
plugins {
alias(libs.plugins.openapi.generator)
}
// Additional configuration for OpenAPI Generator
openApiGenerate {
generatorName = "spring" // or your preferred generator
inputSpec = "$projectDir/src/main/resources/api.yaml"
outputDir = "$buildDir/generated"
apiPackage = "com.example.api"
modelPackage = "com.example.model"
configOptions = [
interfaceOnly: "true",
openApiNullable: "false"
]
}
Complete Working Example
Here’s a complete working setup:
1. Project Structure:
my-project/
├── gradle/
│ └── libs.versions.toml
├── build.gradle
├── settings.gradle
└── src/
└── main/
└── resources/
└── api.yaml
2. gradle/libs.versions.toml:
[versions]
openapi-tools = "7.2.0"
[plugins]
openapi-generator = {
id = "org.openapi.generator",
version.ref = "openapi-tools"
}
3. build.gradle:
plugins {
alias(libs.plugins.openapi.generator)
}
openApiGenerate {
generatorName = "spring"
inputSpec = "$projectDir/src/main/resources/api.yaml"
outputDir = "$buildDir/generated"
apiPackage = "com.example.api"
modelPackage = "com.example.model"
configOptions = [
interfaceOnly: "true",
openApiNullable: "false",
skipDefaultInterface: "true"
]
globalProperties = [
apis: "",
models: ""
]
}
// Add generated sources to the source set
sourceSets.main.java.srcDir "$buildDir/generated/src/main/java"
Common Issues and Troubleshooting
1. Gradle Can’t Find the Plugin:
If you encounter “Could not find plugin ‘org.openapi.generator’”, verify:
- Your
libs.versions.tomlfile is in the correct location (gradle/directory) - The plugin ID is spelled exactly: “org.openapi.generator”
- Gradle has cached the version catalog - try running
./gradlew clean build --refresh-dependencies
2. Version Resolution Issues:
If Gradle can’t resolve the version, ensure:
- The version number in
[versions]section matches available versions - You’re using a supported version of the plugin with Gradle 8.2.0
3. IDE Integration:
According to research from softAai Blogs, for Gradle 8+, version catalogs are enabled by default, but ensure:
- Your IDE has the TOML plugin installed (Android Studio bundles it)
- You’ve refreshed Gradle projects in your IDE after making changes
Advanced Configuration Options
For more complex setups, you can extend your configuration:
1. Multiple Generators:
[versions]
openapi-tools = "7.2.0"
[plugins]
openapi-generator = {
id = "org.openapi.generator",
version.ref = "openapi-tools"
}
[libraries]
openapi-generator-cli = {
group = "org.openapitools",
name = "openapi-generator-cli",
version.ref = "openapi-tools"
}
2. Custom Generator Configuration:
openApiGenerate {
generatorName = "typescript-fetch"
inputSpec = "$projectDir/openapi.yaml"
outputDir = "$projectDir/src/gen/typescript/api"
apiPackage = "api"
modelPackage = "model"
configOptions = [
npmName: "my-api-client",
npmVersion: "1.0.0",
npmRepository: "https://registry.npmjs.org/",
useSingleRequestParameter: "true"
]
}
3. Task Configuration:
tasks.named("openApiGenerate") {
dependsOn("validateOpenApi")
}
task validateOpenApi(type: io.github.swagger2markup.markup.DocumentTask) {
inputSpec = "$projectDir/src/main/resources/api.yaml"
outputDir = projectDir.path + "/docs/overview"
}
Remember to run ./gradlew openApiGenerate after configuration to test your setup. This configuration should work seamlessly with Gradle 8.2.0 and provide you with the modern, centralized dependency management you’re looking for.
Sources
- Gradle Version Catalogs Documentation
- Maven Repository - openapi-generator-gradle-plugin
- Gradle Goodness: Defining Plugin Versions Using Version Catalog
- Gradle Version Catalog in Android: A Complete Guide
- Stack Overflow - Gradle configuration for OpenAPI Generator
Conclusion
- The correct TOML configuration requires proper syntax with
version.refreferencing the version from the[versions]section - Ensure your
libs.versions.tomlfile is located in thegradle/directory with the exact filename - Use
alias(libs.plugins.openapi.generator)in your build.gradle file to reference the plugin - Verify Gradle can resolve dependencies by running with
--refresh-dependenciesif needed - The plugin configuration supports all standard OpenAPI Generator options through the DSL
This modern approach using version catalogs provides better dependency management, reproducible builds, and easier maintenance across your projects.