Hi all, I have created a gradle plugin with Kotli...
# compiler
j
Hi all, I have created a gradle plugin with KotlinCompilerPluginSupportPlugin. On top, have a compiler plugin, which extends kotlin-compiler-embeddable I have set kotlin.native.useEmbeddableCompilerJar=true, and I can now hook in to my ComponentRegistrar from compileKotlinLinuxX64 However, When I try and run the same for JVM. The ComponentRegistrar is never called. Is there a step I am missing when I apply the plugin to start the compilerPlugin for JVM too?
y
You need separate plugins for Native vs the other targets. This blog post should explain it better than me. You can also have a look at this plugin that I've been working on, which has one implementation written for all backends through copying the code into a new gradle module (the blog post explains it better)
j
Yeh I have tried that way too, my original code did that. But I still never appear to get the callback when i compile JVM
The componentregistrar is just never called
only for JVM
even if I write it with one using embeddable and one not
is there an additional step in the jvm gradle I need to change?
y
Lemme take a look at how I did it because I haven't messed around with compiler plugins for a couple months now
Are you providing the plugin artifact in the SupportPlugin for JVM and Native? as in you need both of these:
Copy code
override fun getPluginArtifact(): SubpluginArtifact = SubpluginArtifact(
    groupId = BuildConfig.KOTLIN_PLUGIN_GROUP,
    artifactId = BuildConfig.KOTLIN_PLUGIN_NAME,
    version = BuildConfig.KOTLIN_PLUGIN_VERSION
  )

  override fun getPluginArtifactForNative(): SubpluginArtifact = SubpluginArtifact(
    groupId = BuildConfig.KOTLIN_PLUGIN_GROUP,
    artifactId = BuildConfig.KOTLIN_PLUGIN_NAME + "-native",
    version = BuildConfig.KOTLIN_PLUGIN_VERSION
  )
j
Yep, and if I breakpoint it, it is going in to apply for the jvm compilation
it just never breaks in to the ComponentRegistrar (only for JVM)
Copy code
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
Plugin:
Copy code
plugins {
    kotlin("jvm")
    kotlin("kapt")
    `maven-publish`
}

apply(from = "../maven-publishing-setup.kts")

dependencies {
    compileOnly("org.jetbrains.kotlin:kotlin-compiler-embeddable")
    compileOnly("org.jetbrains.kotlin:kotlin-annotation-processing-embeddable")

    kapt("com.google.auto.service:auto-service:1.0")
    api("com.squareup:kotlinpoet:1.9.0")
    api("com.google.code.gson:gson:2.9.0")
    
    compileOnly("com.google.auto.service:auto-service-annotations:1.0")

    ...
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
    kotlinOptions.freeCompilerArgs += "-Xjvm-default=compatibility"
}

publishing {
    publications {
        create<MavenPublication>("default") {
            from(components["java"])
            artifact(tasks.kotlinSourcesJar)
        }
    }
}
Gradle plugin
Copy code
plugins {
    kotlin("jvm")
    id("java-gradle-plugin")
    id("com.github.gmazzo.buildconfig")
    id("com.gradle.plugin-publish")
    `maven-publish`
  }
  
  apply(from = "../maven-publishing-setup.kts")
  
  dependencies {
    implementation(kotlin("gradle-plugin-api"))
    implementation("com.squareup:kotlinpoet:1.9.0")
  }
  
  try {
    buildConfig {
      val pluginProject = project(":plugin")
      packageName(pluginProject.group.toString())
      buildConfigField("String", "KOTLIN_PLUGIN_ID", "\"${pluginProject.group}.${pluginProject.name}\"")
      buildConfigField("String", "KOTLIN_PLUGIN_GROUP", "\"${pluginProject.group}\"")
      buildConfigField("String", "KOTLIN_PLUGIN_NAME", "\"${pluginProject.name}\"")
      buildConfigField("String", "KOTLIN_PLUGIN_VERSION", "\"${pluginProject.version}\"")
  
      val annotationProject = project(":general-code")
      buildConfigField("String", "ANNOTATION_LIBRARY_GROUP", "\"${annotationProject.group}\"")
      buildConfigField("String", "ANNOTATION_LIBRARY_NAME", "\"${annotationProject.name}\"")
      buildConfigField("String", "ANNOTATION_LIBRARY_VERSION", "\"${annotationProject.version}\"")
    }
  
    gradlePlugin {
      plugins {
        create("plugin-name") {
          id = "plugin.package"
          displayName = "blah"
          description = "blah"
          implementationClass = "EntryPointClass"
        }
      }
    }
  } catch (e: Exception) {
  
  }
  
  tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
    kotlinOptions.freeCompilerArgs += "-Xjvm-default=compatibility"
  }
  
  sourceSets.main.get()
    .apply { println(this) }
    .compileClasspath.apply { println(this) }.forEach {
      println(it)
    }
  tasks {
    jar {
      from(
        sourceSets.main.get()
          .apply { println(this) }
          .compileClasspath.apply { println(this) }.filter { dependency ->
            listOf(
              "gradle-plugin-commons",
            ).any {
              dependency.absolutePath.contains(it)
            }
          }.map(::zipTree)
      )
    }
  }
It feels like for jvmMain, it is just not getting added to the classpath
Ok so i just tried manually adding it to freeCompilerArgs, and it says "Multiple values are not allowed for plugin option pluginblahx=y" So it is getting added to the compile command, just not getting through to the entry point
Ok, in the end, stupid mistake on my part 🙂 for jvm compiler: 1. println does not output anything & 2. breakpoint doesnt work on compiler plugin All sorted
132 Views