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.

One thought on “Creating a javadoc rule for Bazel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: