This guide wires schema2class into a Kotlin JVM project and generates Kotlin models before compilation.
plugins {
kotlin("jvm") version "1.9.25"
id("ca.esmcelroy.schema2class") version "<schema2class-version>"
}schema2class {
schemas {
create("envelope") {
source = file("schemas/envelope.xsd")
annotationMode = "XMLUTIL"
}
create("payload") {
source = file("schemas/payload.schema.json")
packageName = "com.example.payload"
annotationMode = "KOTLINX_SERIALIZATION"
}
}
}For XSD, packageName is optional. When it is omitted, schema2class derives a
package from targetNamespace and resolves xs:include / xs:import
transitively. For JSON Schema, packageName is required.
./gradlew schema2classGenerate compileKotlinThe plugin adds build/generated/schema2class/kotlin to the main Kotlin source
set when the Kotlin JVM plugin is present.
./gradlew schema2classVerifyGeneratedschema2classVerifyGenerated regenerates into a build-temp directory and fails
if the configured generated output differs. Use it in CI when generated sources
are checked in or when you want an explicit schema drift gate.
schema2class generate \
--input schemas/envelope.xsd \
--input schemas/payload.schema.json=com.example.payload \
--annotation-mode kotlinx \
--output build/generated/schema2class/kotlin