NeuroAgent

Configure OpenAPI Generator Plugin in TOML for Gradle 8.2.0

Learn how to properly configure openapi-generator-gradle-plugin using TOML format in Gradle 8.2.0. Complete guide with examples and troubleshooting tips for modern dependency management.

Question

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:

groovy
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:

toml
[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:

groovy
plugins {
    alias(libs.plugins.openapi.generator)    
}

But Gradle can’t find the dependency. What am I missing in this configuration?

NeuroAgent

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

Your TOML file structure looks correct, but there are a few details to ensure proper functionality. Create gradle/libs.versions.toml with this configuration:

toml
[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.toml and placed in your gradle/ directory
  • Use version.ref to 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:

groovy
plugins {
    alias(libs.plugins.openapi.generator)
}

However, you also need to apply the plugin in your build script. Here’s the complete setup:

groovy
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:

toml
[versions]
openapi-tools = "7.2.0"

[plugins]
openapi-generator = { 
    id = "org.openapi.generator", 
    version.ref = "openapi-tools" 
}

3. build.gradle:

groovy
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.toml file 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:

toml
[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:

groovy
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:

groovy
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

  1. Gradle Version Catalogs Documentation
  2. Maven Repository - openapi-generator-gradle-plugin
  3. Gradle Goodness: Defining Plugin Versions Using Version Catalog
  4. Gradle Version Catalog in Android: A Complete Guide
  5. Stack Overflow - Gradle configuration for OpenAPI Generator

Conclusion

  • The correct TOML configuration requires proper syntax with version.ref referencing the version from the [versions] section
  • Ensure your libs.versions.toml file is located in the gradle/ 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-dependencies if 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.