This Gradle plugin generates Java or Kotlin classes from JSON schemas using the JSON Kotlin Schema Codegen library.
- 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
Apply the plugin in build.gradle.kts:
plugins {
kotlin("jvm")
id("de.infolektuell.json-schema-codegen") version "0.121"
}Schemas have to be part of a source set, probably main.
- Put your schema files under
src/<source set name>/schemaswhere 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.ymlor.json. - Running
gradlew buildshould generate source files underbuild/generated-sources.
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)
}
}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"
}
}
}
...