Question

I would like to use Gradle for a multi-module Scala project. I can't figure out how to use the genjavadoc-plugin for the Scala compiler. Ideally I want to generate a .jar, -sources.jar, and -javadocs.jar for each of my libraries. The .jar and -sources.jar were simple, but the javadocs are a little harder. In Maven and SBT you can use the genjavadoc-plugin to generate JavaDoc-able code from Scala, and then just run JavaDoc. I have to think that it's equally possible in Gradle, I just don't know enough Gradle / Groovy to do it.

I can make ScalaDocs, but these libraries are consumed by Java developers and they want to attach the JavaDocs to the .jars in Eclipse, which I think is a perfectly reasonable request.

What should be included in build.gradle to support this?

The compiler plugin is here: https://github.com/typesafehub/genjavadoc

Was it helpful?

Solution

Well, I figured this one out on my own. I'm posting it here with the hope that someone else will find it useful. I'm not posting my entire build.gradle file, only the section that configures the scala projects (I have some pure java projects as well). Essentially you add the genjavadoc-plugin to the dependencies, pass it some arguments, and make sure that you add the 'genjavadoc' directory to the javadoc task.

// specific config for scala projects
configure(scalaProjects) {
    apply plugin: 'scala'

    // this little hack zeroes out the java source directories
    // so that the scala plugin can handle them
    sourceSets.main.scala.srcDir "src/main/java"
    sourceSets.main.java.srcDirs = []
    sourceSets.test.scala.srcDir "src/test/java"
    sourceSets.test.java.srcDirs = []


    // define a configuration for scala compiler plugins
    // the transitive=false means that the plugin won't show up
    // as a dependency in the final output
    configurations {
        scalaCompilerPlugins { transitive = false }
    }

    // this plugin will transform .scala files into javadoc'able .java files
    // so that the regular javadoc will run
    dependencies {
        scalaCompilerPlugins  group: 'com.typesafe.genjavadoc', name: 'genjavadoc-plugin_2.10.2', version:'0.5'
        compile group: 'org.scala-lang', name: 'scala-library', version:'2.10.2'
        compile group: 'org.scala-lang', name: 'scala-compiler', version:'2.10.2'
    }

    // this string contains theplugin paths that get passed to the compiler
    def pluginPaths = configurations.scalaCompilerPlugins.files.collect { "\"-Xplugin:${it.path}\"" }

    // this is the genjavadoc arguments - effectively it tells the plugin where to put the generated code
    compileScala.scalaCompileOptions.additionalParameters = pluginPaths + "\"-P:genjavadoc:out=$buildDir/genjavadoc\""

    task javaDocs(type : Javadoc) {
        source = fileTree("src/main/java").include("*.java") + fileTree("$buildDir/genjavadoc")
        options.addStringOption("-quiet")
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top