# KSP Processor Configuration Generate JSON schemas at compile time with zero runtime overhead using the `kotlinx-schema` KSP processor. ## Setup Configure the KSP processor directly in your Gradle build script, Maven pom.xml, or use the dedicated Gradle plugin. ### Google KSP gradle plugin Add the [Google KSP plugin](https://kotlinlang.org/docs/ksp-quickstart.html) and processor dependency to your project. #### Multiplatform projects ```kotlin plugins { kotlin("multiplatform") id("com.google.devtools.ksp") version "" } dependencies { add("kspCommonMainMetadata", "org.jetbrains.kotlinx:kotlinx-schema-ksp:") implementation("org.jetbrains.kotlinx:kotlinx-schema-annotations:") } kotlin { sourceSets.commonMain.kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin") } // Ensure KSP runs before compilation tasks.withType>().all { if (name != "kspCommonMainKotlinMetadata") dependsOn("kspCommonMainKotlinMetadata") } ksp { arg("kotlinx.schema.rootPackage", "com.example") } ``` Check out an [example project](https://github.com/Kotlin/kotlinx-schema/tree/main/examples/gradle-google-ksp). #### JVM-only projects ```kotlin plugins { kotlin("jvm") id("com.google.devtools.ksp") version "" } dependencies { ksp("org.jetbrains.kotlinx:kotlinx-schema-ksp:") implementation("org.jetbrains.kotlinx:kotlinx-schema-annotations:") } sourceSets.main.kotlin.srcDir("build/generated/ksp/main/kotlin") ``` ### Kotlinx-Schema gradle plugin The plugin automatically handles KSP configuration, source set registration, and task dependencies, and provides additional configuration options (DSL) for schema generation. 1. Register the plugin in `settings.gradle.kts`: ```kotlin pluginManagement { repositories { google() mavenCentral() } resolutionStrategy { eachPlugin { if (requested.id.id == "org.jetbrains.kotlinx.schema.ksp") { useModule("org.jetbrains.kotlinx:kotlinx-schema-gradle-plugin:") } } } } ``` 2. Apply the plugin and dependencies in your `build.gradle.kts`: #### Multiplatform projects ```kotlin plugins { kotlin("multiplatform") id("org.jetbrains.kotlinx.schema.ksp") } kotlin { sourceSets.commonMain.dependencies { implementation("org.jetbrains.kotlinx:kotlinx-schema-annotations:") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:") // Required for withSchemaObject } } kotlinxSchema { rootPackage.set("com.example") } ``` #### JVM-only projects ```kotlin plugins { kotlin("jvm") id("org.jetbrains.kotlinx.schema.ksp") } dependencies { implementation("org.jetbrains.kotlinx:kotlinx-schema-annotations:") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:") } kotlinxSchema { rootPackage.set("com.example") } ``` **Notes:** - You do NOT need to apply the KSP plugin yourself — the Gradle plugin does it. - You do NOT need to add generated source directories — the plugin does it. - For an example project, see [gradle-plugin-integration-tests](https://github.com/Kotlin/kotlinx-schema/tree/main/gradle-plugin-integration-tests). ### Maven Plugin You may also run schema generation with KSP in your Maven projects. Add the [`ksp-maven-plugin`](https://github.com/kpavlov/ksp-maven-plugin) with the processor dependency and include the annotations library in your project. ```xml me.kpavlov.ksp.maven ksp-maven-plugin 0.3.0 true org.jetbrains.kotlinx kotlinx-schema-ksp ${kotlinx-schema.version} com.example org.jetbrains.kotlinx kotlinx-schema-annotations-jvm ${kotlinx-schema.version} 0.0.5 ``` Check out an [example project](https://github.com/Kotlin/kotlinx-schema/tree/main/examples/maven-ksp). ## Configuration options Options can be set globally in your build configuration or overridden per-class via `@Schema`. ### Options reference | Option | Type | Default | Description | |:-------------------|:----------|:--------|:-----------------------------------------------------------------------------------| | `enabled` | `Boolean` | `true` | Enable or disable schema generation. | | `rootPackage` | `String` | `null` | Limit processing to this package and its subpackages. Improves build performance. | | `withSchemaObject` | `Boolean` | `false` | Generate `jsonSchema: JsonObject` property. Requires `kotlinx-serialization-json`. | | `visibility` | `String` | `""` | Visibility modifier for generated extensions (`public`, `internal`, etc.). | ### Option priority 1. **Annotation Parameter** (highest) — `@Schema(withSchemaObject = true)` 2. **KSP Argument** — Global processor options (e.g., `arg()` in Gradle or `` in Maven) 3. **Gradle Option** (Plugin only) — `kotlinxSchema { withSchemaObject.set(true) }` 4. **Default Value** (lowest) > [!TIP] > Use an empty string `visibility.set("")` (default) for Multiplatform projects targeting Native > to avoid "redundant visibility modifier" warnings. ## Generated Code For each `@Schema`-annotated class, the processor generates extension properties: ```kotlin @Schema(withSchemaObject = true) data class User(val name: String) // Access generated extensions val jsonString: String = User::class.jsonSchemaString val jsonObject: JsonObject = User::class.jsonSchema ``` For each `@Schema`-annotated function, the processor generates additional top-level or extension function: ```kotlin @Schema(withSchemaObject = true) internal fun calculateArea(shape: Shape): Double = TODO("only signature matters") // Access generated functions val functionCallSchemaString: String = calculateAreaJsonSchemaString() // + "jsonSchemaString()" val functionCallSchema: JsonObject = calculateAreaJsonSchema() // + "jsonSchema()" ``` ## See Also - [Annotation Reference](../README.md#using-schema-and-description-annotations) — `@Schema` and `@Description` usage - [Runtime Schema Generation](../README.md#runtime-schema-generation) — Alternative using Reflection - [Function Calling Schemas](../README.md#function-calling-schema-generation-for-llms) — Generate LLM function schemas - [JSON Schema DSL](../kotlinx-schema-json/README.md) — Manual schema construction