SimpleKsp is a Kotlin Symbol Processing (KSP) library that enables compile-time code generation using KTE (Part of JTE) templates. It provides a simple annotation, @GenerateCode, to generate Kotlin source files based on template strings.
- KSP-based: Fast, incremental code generation during compilation.
- KTE template support: Write templates in KTE for flexible, type-safe code generation.
- File-level annotation: Use
@file:GenerateCodeto trigger generation from any Kotlin file.
Gradle Kotlin DSL:
plugins {
id("com.google.devtools.ksp") version "<ksp version>"
}
dependencies {
compileOnly("io.github.stream29:kteksp-annotation:<version>")
ksp("io.github.stream29:kteksp-ksp:<version>")
}As KSP does not officially support Maven, we don't plan to support Maven.
If you still want to use this library with maven, please look at community KSP support of maven.
Use the @file:GenerateCode annotation at the top of a Kotlin file to specify generation parameters:
@file:GenerateCode(
packageName = "io.github.stream29.kteksp",
fileName = "Generated$0",
template =
"""
@file:GenerateCode(
packageName = \"io.github.stream29.kteksp\",
fileName = \"Generated$1\",
template = \"\"
)
package io.github.stream29.kteksp
"""
)
package io.github.stream29.kteksppackageName: The package for the generated file. If empty, uses the current package.fileName: The name of the generated file (without.kt). Supports template variables.template: The KTE template string for the generated file content.
Run the build. The KSP processor will generate the specified files in the build/generated/ksp/ directory.
@Target(AnnotationTarget.FILE)
public annotation class GenerateCode(
val packageName: String = "",
val fileName: String = "",
@Language("kte")
val template: String,
)- packageName: Target package for the generated file.
- fileName: Name of the generated file (without
.kt). - template: KTE template string for the file content.
See Generate.kt for a real usage example.