Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow package exclusions and inclusions in javadocs #1293

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ load("@rules_jvm_external//:defs.bzl", "maven_install", "artifact")
<pre>
load("@rules_jvm_external//:defs.bzl", "javadoc")

javadoc(<a href="#javadoc-name">name</a>, <a href="#javadoc-deps">deps</a>, <a href="#javadoc-additional_dependencies">additional_dependencies</a>, <a href="#javadoc-doc_deps">doc_deps</a>, <a href="#javadoc-doc_resources">doc_resources</a>, <a href="#javadoc-doc_url">doc_url</a>, <a href="#javadoc-excluded_workspaces">excluded_workspaces</a>,
<a href="#javadoc-javadocopts">javadocopts</a>)
javadoc(<a href="#javadoc-name">name</a>, <a href="#javadoc-deps">deps</a>, <a href="#javadoc-additional_dependencies">additional_dependencies</a>, <a href="#javadoc-doc_deps">doc_deps</a>, <a href="#javadoc-doc_resources">doc_resources</a>, <a href="#javadoc-doc_url">doc_url</a>, <a href="#javadoc-excluded_packages">excluded_packages</a>,
<a href="#javadoc-excluded_workspaces">excluded_workspaces</a>, <a href="#javadoc-included_packages">included_packages</a>, <a href="#javadoc-javadocopts">javadocopts</a>)
</pre>

Generate a javadoc from all the `deps`
Expand All @@ -35,7 +35,9 @@ Generate a javadoc from all the `deps`
| <a id="javadoc-doc_deps"></a>doc_deps | `javadoc` targets referenced by the current target.<br><br>Use this to automatically add appropriate `-linkoffline` javadoc options to resolve references to packages documented by the given javadoc targets that have `url` specified. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="javadoc-doc_resources"></a>doc_resources | Resources to include in the javadoc jar. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="javadoc-doc_url"></a>doc_url | The URL at which this documentation will be hosted.<br><br>This information is only used by javadoc targets depending on this target. | String | optional | `""` |
| <a id="javadoc-excluded_packages"></a>excluded_packages | A list of packages to exclude from the generated javadoc. Wildcards are supported at the end of the package name. For example, `com.example.*` will exclude all the subpackages of `com.example`, while `com.example` will exclude only the files directly in `com.example`. | List of strings | optional | `[]` |
| <a id="javadoc-excluded_workspaces"></a>excluded_workspaces | A list of bazel workspace names to exclude from the generated jar | List of strings | optional | `["com_google_protobuf", "protobuf"]` |
| <a id="javadoc-included_packages"></a>included_packages | A list of packages to include in the generated javadoc. Wildcards are supported at the end of the package name. For example, `com.example.*` will include all the subpackages of `com.example`, while `com.example` will include only the files directly in `com.example`. | List of strings | optional | `[]` |
| <a id="javadoc-javadocopts"></a>javadocopts | javadoc options. Note sources and classpath are derived from the deps. Any additional options can be passed here. If nothing is passed, a default list of options is used: ["-notimestamp", "-use", "-quiet", "-Xdoclint:-missing", "-encoding", "UTF8"] | List of strings | optional | `["-notimestamp", "-use", "-quiet", "-Xdoclint:-missing", "-encoding", "UTF8"]` |


Expand Down
21 changes: 21 additions & 0 deletions private/rules/java_export.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def java_export(
(if not using `tags = ["no-javadoc"]`)
doc_url: The URL at which the generated `javadoc` will be hosted (if not using
`tags = ["no-javadoc"]`).
doc_resources: Resources to be included in the javadoc jar.
doc_excluded_packages: A list of packages to exclude from the generated javadoc. Wildcards are supported at the
end of the package name. For example, `com.example.*` will exclude all the subpackages of `com.example`, while
`com.example` will exclude only the files directly in `com.example`
doc_included_packages: A list of packages to include in the generated javadoc. Wildcards are supported at the
end of the package name. For example, `com.example.*` will include all the subpackages of `com.example`, while
`com.example` will include only the files directly in `com.example`
visibility: The visibility of the target
kwargs: These are passed to [`java_library`](https://bazel.build/reference/be/java#java_library),
and so may contain any valid parameter for that rule.
Expand All @@ -88,6 +95,8 @@ def java_export(
doc_deps = kwargs.pop("doc_deps", [])
doc_url = kwargs.pop("doc_url", "")
doc_resources = kwargs.pop("doc_resources", [])
doc_excluded_packages = kwargs.pop("doc_excluded_packages", [])
doc_included_packages = kwargs.pop("doc_included_packages", [])
toolchains = kwargs.pop("toolchains", [])

# Construct the java_library we'll export from here.
Expand Down Expand Up @@ -115,6 +124,8 @@ def java_export(
doc_deps = doc_deps,
doc_url = doc_url,
doc_resources = doc_resources,
doc_excluded_packages = doc_excluded_packages,
doc_included_packages = doc_included_packages,
toolchains = toolchains,
)

Expand All @@ -135,6 +146,8 @@ def maven_export(
doc_deps = [],
doc_url = "",
doc_resources = [],
doc_excluded_packages = [],
doc_included_packages = [],
toolchains = None):
"""
All arguments are the same as java_export with the addition of:
Expand Down Expand Up @@ -194,6 +207,12 @@ def maven_export(
doc_url: The URL at which the generated `javadoc` will be hosted (if not using
`tags = ["no-javadoc"]`).
doc_resources: Resources to be included in the javadoc jar.
doc_excluded_packages: A list of packages to exclude from the generated javadoc. Wildcards are supported at the
end of the package name. For example, `com.example.*` will exclude all the subpackages of `com.example`, while
`com.example` will exclude only the files directly in `com.example`
doc_included_packages: A list of packages to include in the generated javadoc. Wildcards are supported at the
end of the package name. For example, `com.example.*` will include all the subpackages of `com.example`, while
`com.example` will include only the files directly in `com.example`
visibility: The visibility of the target
kwargs: These are passed to [`java_library`](https://bazel.build/reference/be/java#java_library),
and so may contain any valid parameter for that rule.
Expand Down Expand Up @@ -264,6 +283,8 @@ def maven_export(
doc_deps = doc_deps,
doc_url = doc_url,
doc_resources = doc_resources,
excluded_packages = doc_excluded_packages,
included_packages = doc_included_packages,
excluded_workspaces = excluded_workspaces.keys(),
additional_dependencies = additional_dependencies,
visibility = visibility,
Expand Down
23 changes: 23 additions & 0 deletions private/rules/javadoc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,28 @@ def generate_javadoc(
javadocopts,
doc_deps,
doc_resources,
excluded_packages,
output,
element_list):
inputs = []
transitive_inputs = []
args = ctx.actions.args()

args.add("--out", output)
args.add("--element-list", element_list)

args.add_all(source_jars, before_each = "--in")
inputs.extend(source_jars)

args.add_all(ctx.files.doc_resources, before_each = "--resources")
inputs.extend(ctx.files.doc_resources)

args.add_all(classpath, before_each = "--cp")
transitive_inputs.append(classpath)

args.add_all(excluded_packages, before_each = "--exclude-packages")
args.add_all(ctx.attr.included_packages, before_each = "--include-packages")

for dep in doc_deps:
dep_info = dep[_JavadocInfo]
args.add("-linkoffline")
Expand Down Expand Up @@ -90,6 +98,7 @@ def _javadoc_impl(ctx):
ctx.attr.javadocopts,
ctx.attr.doc_deps,
ctx.attr.doc_resources,
ctx.attr.excluded_packages,
jar_file,
element_list,
)
Expand Down Expand Up @@ -153,6 +162,20 @@ javadoc = rule(
allow_files = True,
default = [],
),
"excluded_packages": attr.string_list(
doc = """A list of packages to exclude from the generated javadoc. Wildcards are supported at the end of
the package name. For example, `com.example.*` will exclude all the subpackages of `com.example`, while
`com.example` will exclude only the files directly in `com.example`.""",
allow_empty = True,
default = [],
),
"included_packages": attr.string_list(
doc = """A list of packages to include in the generated javadoc. Wildcards are supported at the end of
the package name. For example, `com.example.*` will include all the subpackages of `com.example`, while
`com.example` will include only the files directly in `com.example`.""",
allow_empty = True,
default = [],
),
"excluded_workspaces": attr.string_list(
doc = "A list of bazel workspace names to exclude from the generated jar",
allow_empty = True,
Expand Down
13 changes: 5 additions & 8 deletions private/rules/kt_jvm_export.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load(":java_export.bzl", "maven_export")
load(":maven_project_jar.bzl", "DEFAULT_EXCLUDED_WORKSPACES")

KOTLIN_STDLIB = Label("@rules_kotlin//kotlin/compiler:kotlin-stdlib")
KOTLIN_STDLIB = "rules_kotlin"

def kt_jvm_export(
name,
Expand Down Expand Up @@ -70,11 +70,8 @@ def kt_jvm_export(

javadocopts = kwargs.pop("javadocopts", None)

# ensure that the kotlin-stdlib is included in deploy_env
if KOTLIN_STDLIB not in deploy_env:
updated_deploy_env = deploy_env + [KOTLIN_STDLIB]
else:
updated_deploy_env = deploy_env
updated_excluded_workspaces = {name: None for name in excluded_workspaces}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deploy_env gets added back into the docs so we were generating docs for kotlin stdlib which seems wrong. Changed to excluded_workspace to keep it out of the docs, which seems like what was actually intended.
This is also how we handle scala_export in confluent's fork of rules_jvm_external.

updated_excluded_workspaces.update({KOTLIN_STDLIB: None})

# Construct the kt_jvm_library we'll export from here.
kt_jvm_library(
Expand All @@ -88,8 +85,8 @@ def kt_jvm_export(
name = name,
maven_coordinates = maven_coordinates,
lib_name = lib_name,
deploy_env = updated_deploy_env,
excluded_workspaces = excluded_workspaces,
deploy_env = deploy_env,
excluded_workspaces = updated_excluded_workspaces,
pom_template = pom_template,
visibility = visibility,
tags = tags,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
load("@rules_java//java:defs.bzl", "java_binary")
load("@rules_java//java:defs.bzl", "java_binary", "java_library")

java_library(
name = "javadoc_lib",
srcs = ["JavadocJarMaker.java"],
visibility = [
"//tests/com/github/bazelbuild/rules_jvm_external/javadoc:__pkg__",
],
deps = [
":create_jar_lib",
"//private/tools/java/com/github/bazelbuild/rules_jvm_external",
"//private/tools/java/com/github/bazelbuild/rules_jvm_external/zip",
],
)

java_binary(
name = "javadoc",
srcs = glob(["*.java"]),
main_class = "com.github.bazelbuild.rules_jvm_external.javadoc.JavadocJarMaker",
visibility = [
"//visibility:public",
],
runtime_deps = [
":javadoc_lib",
],
)

java_library(
name = "create_jar_lib",
srcs = ["CreateJar.java"],
visibility = [
"//tests/com/github/bazelbuild/rules_jvm_external/javadoc:__pkg__",
],
deps = [
"//private/tools/java/com/github/bazelbuild/rules_jvm_external",
"//private/tools/java/com/github/bazelbuild/rules_jvm_external/zip",
],
)

java_binary(
name = "create_jar",
main_class = "com.github.bazelbuild.rules_jvm_external.javadoc.CreateJar",
visibility = [
"//visibility:public",
],
runtime_deps = [
":create_jar_lib",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.github.bazelbuild.rules_jvm_external.javadoc;

import com.github.bazelbuild.rules_jvm_external.zip.StableZipEntry;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Comparator;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CreateJar {

public static void main(String[] args) throws IOException {
Path out = Paths.get(args[0]);
Set<Path> inputs = Stream.of(args).skip(1).map(Paths::get).collect(Collectors.toSet());

Path tmpDir = Files.createTempDirectory("create-jar-temp");
tmpDir.toFile().deleteOnExit();

for (Path input : inputs) {
if (!Files.isDirectory(input)) {
Files.copy(input, tmpDir.resolve(input.getFileName()), StandardCopyOption.REPLACE_EXISTING);
continue;
}

Files.walk(input)
.forEachOrdered(
source -> {
try {
Path target = tmpDir.resolve(input.relativize(source));
if (Files.isDirectory(source)) {
Files.createDirectories(target);
} else {
Files.createDirectories(target.getParent());
Files.copy(source, target);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}

createJar(out, tmpDir);
}

public static void createJar(Path out, Path inputDir) throws IOException {
try (OutputStream os = Files.newOutputStream(out);
ZipOutputStream zos = new ZipOutputStream(os);
Stream<Path> walk = Files.walk(inputDir)) {

walk.sorted(Comparator.naturalOrder())
.forEachOrdered(
path -> {
if (path.equals(inputDir)) {
return;
}

try {
if (Files.isDirectory(path)) {
String name = inputDir.relativize(path) + "/";
ZipEntry entry = new StableZipEntry(name);
zos.putNextEntry(entry);
zos.closeEntry();
} else {
String name = inputDir.relativize(path).toString();
ZipEntry entry = new StableZipEntry(name);
zos.putNextEntry(entry);
try (InputStream is = Files.newInputStream(path)) {
com.github.bazelbuild.rules_jvm_external.ByteStreams.copy(is, zos);
}
zos.closeEntry();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
}
}
Loading