Creating a javadoc rule for Bazel

A couple of users have asked about how to generate javadoc with Bazel. There’s no built-in way, but I figured it might be useful to whip together a new rule to do so.

Here it is.

If you’d like to use this rule, download it to your workspace, load it in your build file, and give it a list of sources:

load("/javadoc", "javadoc")
 
javadoc(
    name = "app-doc",
    srcs = glob(["**/*.java"]),
)

Then build:

$ bazel build :app-doc
INFO: Found 1 target...
Target //:app-doc up-to-date:
  bazel-bin/app-doc.zip
INFO: Elapsed time: 0.141s, Critical Path: 0.00s

Now, if you look at bazel-bin/app-doc.zip, you can see that it contains the HTML tree generated by javadoc:

$ unzip -l bazel-bin/app-doc.zip
Archive:  bazel-bin/app-doc.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     6990  2015-09-28 15:06   app-doc/SomeClass.html
      568  2015-09-28 15:06   app-doc/allclasses-frame.html
      548  2015-09-28 15:06   app-doc/allclasses-noframe.html
...

You can then unzip it wherever you want to view your docs.

Note that this is an extremely limited implementation: I just dashed this off in 20 minutes. It doesn’t support any javadoc options and probably doesn’t handle dependencies correctly. Let me know if you have any issues with it and I can implement improvements as needed.

  • billgraham

    One thing to be aware of is that if this is used from a target that has subpackages, the glob pattern won’t pick up classes in the subpackages. See limitations and caveats here: http://bazel.io/docs/be/functions.html#glob

kristina chodorow's blog