Skip to content

infolektuell/gradle-json-schema-codegen

Repository files navigation

Gradle JSON Schema Code Generation Plugin

Gradle Plugin Portal Version

This Gradle plugin generates Java or Kotlin classes from JSON schemas using the JSON Kotlin Schema Codegen library.

Features

  • Extends Java source sets, so each source set can have its own schemas and config file, and includes the generated sources.
  • Comes with sensible conventions, so it should work out of the box.
  • Generates Java classes by default, but Kotlin classes if Kotlin JVM is present.
  • Compatible with Configuration Cache.
  • Task is cacheable.
  • Composite files as inputs
  • URIs as inputs

Usage

Configure your build

Apply the plugin in build.gradle.kts:

plugins {
    kotlin("jvm")
    id("de.infolektuell.json-schema-codegen") version "0.121"
}

Prepare your schemas

Schemas have to be part of a source set, probably main.

  • Put your schema files under src/<source set name>/schemas where the plugin will find them without further configuration.
  • If you have a config file for the code generator, put it under src/<source set name>/json-schema-codegen.yml or .json.
  • Running gradlew build should generate source files under build/generated-sources.

Customization

Customize the locations of your schemas and config files in the jsonSchemaCodegen source set extension, if needed.

plugins {
    `java-library` // Java classes will be generated
    id("de.infolektuell.json-schema-codegen") version "0.121"
}

java {
    sourceSets.named("main") {
        jsonSchemaCodegen {
            schemas.add(layout.projectDirectory.dir("src/main/resources/schemas"))
            configFile = layout.projectDirectory.file("src/main/resources/codegen-config.yml")
        }
    }
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

Composites

A composite is a jSON file which is not a schema itself but references multiple schema definitions. Such files can be added in the extension to generate the contained definitions:

...
jsonSchemaCodegen {
    composites {
        register("companyAPI") {
            file = layout.projectDirectory.file("src/openapi/company.json")
            pointer = "\$defs"
        }
    }
}
...

License

MIT License

About

Gradle plugin to generate Java and Kotlin classes from JSON schemas

Topics

Resources

License

Stars

Watchers

Forks

Languages