Using javac plugins with PlatformScalaCompile

I’m trying to use a javac plugin with a Play project but am not able to get the compiler to find the plugin. Everything works find when running javac from the command line, so the issue doesn’t seem to be with the plugin or a missing META-INF/services/com.sun.source.util.Plugin file.

In build.gradle I’ve added
tasks.withType(PlatformScalaCompile).all { task -> task.options.compilerArgs += ["-Xplugin:CustomPlugin"] }

During compilation I get
> Task :play-plugin-project:compilePlayBinaryScala plug-in not found: CustomPlugin

Currently the javac plugin is part of another Gradle plugin, and the class can be found on the classpath using classloader.loadClass. I’ve also tried adding -cp with compilerArgs, and setting fork to true and adding -cp to the forkOptions.jvmArgs. I still get the plug-in not found error.

What’s the correct way to set up the PlatformScalaCompile so that javac is able to find my plugin?

Hi @bbarkley,

I think we greatly restrict the classpath that the Scala compiler uses. Is the javac plugin intended for the Scala compiler and javac or just javac if the Scala compiler invokes it? Is this something you’re already doing via sbt?

It’s only to be used by javac if the Scala compiler invokes it, though we’ll probably be applying it to normal Java compilation for non Play projects as well. It’s not something that we’ve done with sbt before.


If you’re compiling your Java sources with a PlatformJavaCompile or JavaCompile task, I think you can add your javac plugin to the annotationProcessor configuration and pass the extra compiler arguments as you’re doing above. javac compiler plugins appear to get their classpath from the annotation processor classpath.

For javac called from the Scala compiler, it seems harder. I don’t see a great way to properly feed everything through (Gradle -> Zinc Scala compiler -> javac).

Is there a less-than-great way? :stuck_out_tongue: Do you have any pointers for where the classpath the handoff is made in the chain between Gradle->Zinc->javac?