This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
How to use a Kotlin Compiler Plugin from Gradle Plugin
Jens Klingenberg edited this page Sep 1, 2019
·
3 revisions
- You can find example code here: Gradle Plugin
- Create a usual Kotlin-Jvm project
- Add dependencies:
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile "org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.3.50"
compileOnly "com.google.auto.service:auto-service:1.0-rc6"
kapt "com.google.auto.service:auto-service:1.0-rc6"
}
- Apply the plugins
apply plugin: "java-gradle-plugin"
apply plugin: "kotlin-kapt"
apply plugin: "maven"
Create a class which implements org.gradle.api.Plugin
open class MpAptGradlePlugin : org.gradle.api.Plugin<Project> {
override fun apply(project: Project) {
//You don't need to do something here
}
}
Create a class which implements KotlinGradleSubplugin
@AutoService(KotlinGradleSubplugin::class) // don't forget!
class MpAptGradleSubplugin : KotlinGradleSubplugin<AbstractCompile> {
...
}
override fun apply(...) : List<SubpluginOption>
You can return an emptyList() here
override fun isApplicable(project: Project, task: AbstractCompile) =
project.plugins.hasPlugin(MpAptGradlePlugin::class.java)
Here you decide when your plugin is applicable.
override fun getCompilerPluginId(): String = "MpAptPlugin"
If you want to pass command line arguments to your compiler plugin the pluginid needs to match with the pluginId in your CommandLineProcessor
override fun getPluginArtifact(): SubpluginArtifact = SubpluginArtifact(
groupId = "de.jensklingenberg",
artifactId = "kotlin-compiler-plugin",
version = "0.0.1" // remember to bump this version before any release!
)
Here you need to set the correct values of the compiler plugin that you want to start. You can only start compiler plugins for Kotlin JS/JVM here
override fun getNativeCompilerPluginArtifact() = SubpluginArtifact(
groupId = "de.jensklingenberg",
artifactId = "kotlin-compiler-native-plugin",
version = "0.0.1" // remember to bump this version before any release!
)
the same as above, but here you can only set Kotlin Native Plugins
- Add configuration to your build.gradle
group = "de.jensklingenberg"
archivesBaseName = "mpapt-gradle"
version = "1.0.0"
install {
repositories.mavenInstaller {
pom.artifactId = archivesBaseName
}
}
gradlePlugin {
plugins {
simplePlugin {
id = "de.jensklingenberg.mpapt" // users will do `apply plugin: "de.jensklingenberg.mpapt"`
implementationClass = "de.jensklingenberg.mpapt.MpAptGradlePlugin" // entry-point class
}
}
}
- Use the install task to create the plugin