This repository contains a simple Kotlin compiler plugin implementation for the purpose of demonstrating how to write a Kotlin compiler plugin.
gradle-plugin
: Gradle plugin to apply the Kotlin compiler plugin to any projects.kotlin-plugin
: Custom compiler plugin implementation.
Note
demo
is an independent project to test the compiler plugin. It is not a part of the compiler plugin itself.
This compiler plugin is quite simple. It just adds statements to print out current system time at the beginning of function. Annotation classes to be processed can be specified in the Gradle plugin configuration.
annotation class HogeAnnotation
@HogeAnnotation
fun main() {
}
fun main() {
val startTime = System.currentTimeMillis()
println(startTime)
}
If enabled
is set to false
, the compiler plugin will not be applied.
annotations
is a list of annotation classes to be processed by the compiler plugin.
myPlugin {
enabled = true
annotations = ["HogeAnnotation"]
}
configure<MyPluginExtension> {
enabled = true
annotations = listOf("HogeAnnotation")
}
- Run
./gradlew publishToMavenLocal
at the root directory to publishgradle-plugin
andkotlin-plugin
to maven local. - Run
cd demo
to move to the root directory of thedemo
project. - Run
./gradlew run
to see the compiler plugin in action.
By default, you will see the output like this:
> Task :run
1696419167450
Want to know more about Kotlin compiler plugin? These resources might be helpful for you. Note that some of them are a bit outdated.