Adam Mackler
Adam Mackler

Reputation: 2080

How to use GenJavaDoc plugin for sbt?

I have a very basic sbt project, all scala code except for one .java file, for which I need to generate a standard javadoc webpage.

I'm trying to follow the instructions on the plugin's README. I've copied all the code following "Integration into an SBT build is quite simple," which defines variablesJavaDoc and javadocSettings, into my build.sbt file, and also added the following:

resolvers += "Typesafe Releases" at "https://repo.typesafe.com/typesafe/releases/"

autoCompilerPlugins := true

The instructions say I must "Add[] javadocSettings to a Project." I don't know what that means. Is that what the code in the README does, or is it something separate I need to do? If so, how?

In any event, the README continues: "the JavaDoc can be generated using genjavadoc:doc." Yet, in sbt I see:

> genjavadoc:doc
[error] No such setting/task
[error] genjavadoc:doc
[error]               ^
>

What am I missing?

Upvotes: 2

Views: 389

Answers (1)

lpiepiora
lpiepiora

Reputation: 13749

You need a configuration as it is described in the README.md of the plugin. Your task invocation is not working, because there is no genjavadoc normally in sbt.

You have to add it as documented in the plugin documentation. In your build.sbt file add following content recommended by the plugin (leaving what you have already):

lazy val JavaDoc = config("genjavadoc") extend Compile

lazy val javadocSettings = inConfig(JavaDoc)(Defaults.configSettings) ++ Seq(
  libraryDependencies += compilerPlugin("com.typesafe.genjavadoc" %% "genjavadoc-plugin" % "0.7" cross CrossVersion.full),
  scalacOptions <+= target map (t => "-P:genjavadoc:out=" + (t / "java")),
  packageDoc in Compile <<= packageDoc in JavaDoc,
  sources in JavaDoc <<= (target, compile in Compile, sources in Compile) map ((t, c, s) =>
      (t / "java" ** "*.java").get ++ s.filter(_.getName.endsWith(".java"))),
  javacOptions in JavaDoc := Seq(),
  artifactName in packageDoc in JavaDoc :=
    ((sv, mod, art) => "" + mod.name + "_" + sv.binary + "-" + mod.revision + "-javadoc.jar")
)

However to make it work you need to add the config and the settings to the project. The easiest way is to add in your build.sbt one extra line:

lazy val root = project.in(file(".")).configs(JavaDoc).settings(javadocSettings: _*)

Now you should be able to generate javadoc using genjavadoc:doc.

Given the above, the complete build.sbt could look like this:

resolvers += "Typesafe Releases" at "https://repo.typesafe.com/typesafe/releases/"

lazy val root = project.in(file(".")).configs(JavaDoc).settings(javadocSettings: _*)

lazy val JavaDoc = config("genjavadoc") extend Compile

lazy val javadocSettings = inConfig(JavaDoc)(Defaults.configSettings) ++ Seq(
  libraryDependencies += compilerPlugin("com.typesafe.genjavadoc" %% "genjavadoc-plugin" % "0.7" cross CrossVersion.full),
  scalacOptions <+= target map (t => "-P:genjavadoc:out=" + (t / "java")),
  packageDoc in Compile <<= packageDoc in JavaDoc,
  sources in JavaDoc <<= (target, compile in Compile, sources in Compile) map ((t, c, s) =>
      (t / "java" ** "*.java").get ++ s.filter(_.getName.endsWith(".java"))),
  javacOptions in JavaDoc := Seq(),
  artifactName in packageDoc in JavaDoc :=
    ((sv, mod, art) => "" + mod.name + "_" + sv.binary + "-" + mod.revision + "-javadoc.jar")
)

PS. Small bonus, if you're using an sbt newer than 0.13.2, you can define javadocSettings without using <<= and <+= and other similar operators like this:

lazy val javadocSettings = inConfig(JavaDoc)(Defaults.configSettings) ++ Seq(
  addCompilerPlugin("com.typesafe.genjavadoc" %% "genjavadoc-plugin" % "0.7" cross CrossVersion.full),
  scalacOptions += s"-P:genjavadoc:out=${target.value}/java",
  packageDoc in Compile := (packageDoc in JavaDoc).value,
  sources in JavaDoc := 
    (target.value / "java" ** "*.java").get ++ (sources in Compile).value.filter(_.getName.endsWith(".java")),
  javacOptions in JavaDoc := Seq(),
  artifactName in packageDoc in JavaDoc :=
    ((sv, mod, art) => "" + mod.name + "_" + sv.binary + "-" + mod.revision + "-javadoc.jar")
)  

Upvotes: 2

Related Questions