Apply Java annotation processors to Scala class files


(Jeremy Marshall) #1

Apologies for cross posting but I got no love on StackOverflow

What I want to do is apply Java annotation processing onto built scala classes. The librbary I’m using SezPoz runs processing on ‘source’ annotations. So in Scala I can compile and @source annotations make it into the class file and can be post processed but I can’t find a way to do this with gradle. I was thinking maybe it needs more that config. I have a test repo which demonstrates what I want.

Ultimately I want to be able to build jenkins Plugins with Scala and SezPos is how the plugins are exposed

Github link

This is what I have so far which doesn’t work…
group 'mytest’
version ‘1.0-SNAPSHOT’

apply plugin: 'java'
apply plugin: 'scala'

sourceSets.main.scala.srcDir "src/main/java"
sourceSets.main.java.srcDirs = []

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

jar {
    from {
        (configurations.runtime).collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    manifest {
        attributes 'Main-Class': 'mypackage.Main'
    }
}

ext {
    versions = [
            scala     : '2.11.8',
            scalatest : '2.2.6'
    ]
}

task annotate (type: JavaCompile) {
    source = sourceSets.main.output.classesDir
    include 'mypackage.ScalaUseMyAnnotations$Inner'
    classpath = sourceSets.main.compileClasspath
    destinationDir = sourceSets.main.output.classesDir
    outputs.upToDateWhen { false }

    println("[annotate] ${sourceSets.main.output.classesDir}")
}

annotate.options.compilerArgs = ['-proc:only',  '-processor',  'net.java.sezpoz.impl.Indexer62',  '-verbose']

task run(type: JavaExec, dependsOn: compileScala) {
    main = 'mypackage.Main'
    classpath sourceSets.main.runtimeClasspath
    classpath configurations.runtime
}

compileScala.doLast {
    tasks.annotate
}

dependencies {
    compile group: 'net.java.sezpoz', name: 'sezpoz', version: '1.11'
    runtime group: 'org.scala-lang', name: 'scala-library', version: "$versions.scala"
    compile group: 'org.scala-lang', name: 'scala-library', version: "$versions.scala"
}

(Jeremy Marshall) #2

This is what I ended up with

group 'mytest'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'scala'

sourceSets.main.scala.srcDir "src/main/java"
sourceSets.main.java.srcDirs = []

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

jar {
    from {
        (configurations.runtime).collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    manifest {
        attributes 'Main-Class': 'mypackage.Main'
    }
}

ext {
    versions = [
            scala     : '2.11.8',
            scalatest : '2.2.6'
    ]
}

task annotate (type: JavaCompile) {
    source = sourceSets.main.output.classesDir
    include 'mypackage.ScalaUseMyAnnotations$Inner'
    classpath = sourceSets.main.compileClasspath
    destinationDir = sourceSets.main.output.classesDir
    outputs.upToDateWhen { false }

    println("[annotate] ${sourceSets.main.output.classesDir}")
}

annotate.options.compilerArgs = ['-proc:only',  '-processor',  'net.java.sezpoz.impl.Indexer6',  '-verbose']

task run(type: JavaExec, dependsOn: compileScala) {
    main = 'mypackage.Main'
    classpath sourceSets.main.runtimeClasspath
    classpath configurations.runtime
}

compileScala.doLast {
    tasks.annotate
}

dependencies {
    compile group: 'net.java.sezpoz', name: 'sezpoz', version: '1.11'
    runtime group: 'org.scala-lang', name: 'scala-library', version: "$versions.scala"
    compile group: 'org.scala-lang', name: 'scala-library', version: "$versions.scala"
}

which only does one file in https://github.com/JeremyMarshall/scala-sezpoz
The problem is that to reprocess class files needs the .class extension stripped

A better version is here https://github.com/jenkinsci/hello-world-scala-plugin/blob/master/build.gradle but that is all wrapped up with creating a jenkins plugin in scala.