antannotationskotlinkotlinc

Can I invoke the Kotlin noargs plugin from the command line or from Ant?


Kotlin has a noarg compiler plugin which can be used to generate synthetic no-argument constructors for specially-annotated classes. The documentation page clearly explains how to use it with Maven and Gradle, but it is not clear whether it can be used, or how to use it, either from Ant or using kotlinc on the command line.

So far I've been unable to make it work. Here's my NoArgs.kt source file:

package test

annotation class NoArgs

@NoArgs data class Test(val x: String, val y: String)

And here are some command lines I've tried, which have no effect at all:

$ kotlinc -verbose "-Xplugin:$KOTLIN_HOME/lib/noarg-compiler-plugin.jar -P plugin:org.jetbrains.kotlin.noarg:annotation=test.NoArgs NoArgs.kt

and:

$ kotlinc -verbose "-Xplugin=$KOTLIN_HOME/lib/kotlin-annotation-processing.jar;$KOTLIN_HOME/lib/noarg-compiler-plugin.jar;$JRE_HOME/lib/tools.jar" -P plugin:org.jetbrains.kotlin.noarg:annotation=test.NoArgs NoArgs.kt

When I run $ javap test.Test there is no evidence of a generated no-args constructor. There's some side-evidence that the plugin isn't running at all, as well, in the fact that if I put garbage instead of the word annotation for the -P option, there's no error message or change in behaviour.

If you can tell me how to get it to run using kotlinc or from Ant, I'd really appreciate it!


Solution

  • Please check the path to the noarg-compiler-plugin.jar file. This works for me (Kotlin 1.1.3-2 from Homebrew):

    kotlinc-jvm -Xplugin=$KOTLIN_HOME/libexec/lib/noarg-compiler-plugin.jar -P plugin:org.jetbrains.kotlin.noarg:annotation=test.NoArgs NoArgs.kt

    Compiled from "NoArgs.kt"
    public final class test.Test {
      public final java.lang.String getX();
      public final java.lang.String getY();
      public test.Test(java.lang.String, java.lang.String);
      public final java.lang.String component1();
      public final java.lang.String component2();
      public final test.Test copy(java.lang.String, java.lang.String);
      public static test.Test copy$default(test.Test, java.lang.String, java.lang.String, int, java.lang.Object);
      public java.lang.String toString();
      public int hashCode();
      public boolean equals(java.lang.Object);
      public test.Test();
    }