Skip to content

Commit d238e0b

Browse files
authored
Allow package exclusions and inclusions in javadocs (#35)
1 parent 8e0170f commit d238e0b

File tree

9 files changed

+416
-54
lines changed

9 files changed

+416
-54
lines changed

docs/api.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ load("@rules_jvm_external//:defs.bzl", "maven_install", "artifact")
1818
<pre>
1919
load("@rules_jvm_external//:defs.bzl", "javadoc")
2020

21-
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_url">doc_url</a>, <a href="#javadoc-excluded_workspaces">excluded_workspaces</a>, <a href="#javadoc-javadocopts">javadocopts</a>)
21+
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>,
22+
<a href="#javadoc-excluded_workspaces">excluded_workspaces</a>, <a href="#javadoc-included_packages">included_packages</a>, <a href="#javadoc-javadocopts">javadocopts</a>)
2223
</pre>
2324

2425
Generate a javadoc from all the `deps`
@@ -32,8 +33,11 @@ Generate a javadoc from all the `deps`
3233
| <a id="javadoc-deps"></a>deps | The java libraries to generate javadocs for.<br><br>The source jars of each dep will be used to generate the javadocs. Currently docs for transitive dependencies are not generated. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
3334
| <a id="javadoc-additional_dependencies"></a>additional_dependencies | Mapping of `Label`s to the excluded workspace names. Note that this must match the values passed to the `pom_file` rule so the `pom.xml` correctly lists these dependencies. | <a href="https://bazel.build/rules/lib/dict">Dictionary: Label -> String</a> | optional | `{}` |
3435
| <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 | `[]` |
36+
| <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 | `[]` |
3537
| <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 | `""` |
38+
| <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 | `[]` |
3639
| <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"]` |
40+
| <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 | `[]` |
3741
| <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"]` |
3842

3943

@@ -44,8 +48,9 @@ Generate a javadoc from all the `deps`
4448
<pre>
4549
load("@rules_jvm_external//:defs.bzl", "java_export")
4650

47-
java_export(<a href="#java_export-name">name</a>, <a href="#java_export-maven_coordinates">maven_coordinates</a>, <a href="#java_export-manifest_entries">manifest_entries</a>, <a href="#java_export-deploy_env">deploy_env</a>, <a href="#java_export-excluded_workspaces">excluded_workspaces</a>,
48-
<a href="#java_export-pom_template">pom_template</a>, <a href="#java_export-visibility">visibility</a>, <a href="#java_export-tags">tags</a>, <a href="#java_export-testonly">testonly</a>, <a href="#java_export-classifier_artifacts">classifier_artifacts</a>, <a href="#java_export-kwargs">kwargs</a>)
51+
java_export(<a href="#java_export-name">name</a>, <a href="#java_export-maven_coordinates">maven_coordinates</a>, <a href="#java_export-manifest_entries">manifest_entries</a>, <a href="#java_export-deploy_env">deploy_env</a>, <a href="#java_export-excluded_workspaces">excluded_workspaces</a>, <a href="#java_export-exclusions">exclusions</a>,
52+
<a href="#java_export-pom_template">pom_template</a>, <a href="#java_export-visibility">visibility</a>, <a href="#java_export-tags">tags</a>, <a href="#java_export-testonly">testonly</a>, <a href="#java_export-classifier_artifacts">classifier_artifacts</a>, <a href="#java_export-publish_maven_metadata">publish_maven_metadata</a>,
53+
<a href="#java_export-kwargs">kwargs</a>)
4954
</pre>
5055

5156
Extends `java_library` to allow maven artifacts to be uploaded.
@@ -101,11 +106,13 @@ Generated rules:
101106
| <a id="java_export-manifest_entries"></a>manifest_entries | A dict of `String: String` containing additional manifest entry attributes and values. | `{}` |
102107
| <a id="java_export-deploy_env"></a>deploy_env | A list of labels of Java targets to exclude from the generated jar. [`java_binary`](https://bazel.build/reference/be/java#java_binary) targets are *not* supported. | `[]` |
103108
| <a id="java_export-excluded_workspaces"></a>excluded_workspaces | A dict of strings representing the workspace names of artifacts that should not be included in the maven jar to a `Label` pointing to the dependency that workspace should be replaced by, or `None` if the exclusion shouldn't be replaced with an extra dependency. | `{"com_google_protobuf": None, "protobuf": None}` |
109+
| <a id="java_export-exclusions"></a>exclusions | Mapping of target labels to a list of exclusions to be added to the POM file. Each label must correspond to a direct maven dependency of this target. Each exclusion is represented as a `group:artifact` string. | `{}` |
104110
| <a id="java_export-pom_template"></a>pom_template | The template to be used for the pom.xml file. | `None` |
105111
| <a id="java_export-visibility"></a>visibility | The visibility of the target | `None` |
106112
| <a id="java_export-tags"></a>tags | <p align="center"> - </p> | `[]` |
107113
| <a id="java_export-testonly"></a>testonly | <p align="center"> - </p> | `None` |
108114
| <a id="java_export-classifier_artifacts"></a>classifier_artifacts | A dict of classifier -> artifact of additional artifacts to publish to Maven. | `{}` |
115+
| <a id="java_export-publish_maven_metadata"></a>publish_maven_metadata | Whether to publish a maven-metadata.xml and so may contain any valid parameter for that rule. | `True` |
109116
| <a id="java_export-kwargs"></a>kwargs | <p align="center"> - </p> | none |
110117

111118

private/rules/java_export.bzl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def java_export(
9494
doc_deps = kwargs.pop("doc_deps", [])
9595
doc_url = kwargs.pop("doc_url", "")
9696
doc_resources = kwargs.pop("doc_resources", [])
97+
doc_excluded_packages = kwargs.pop("doc_excluded_packages", [])
98+
doc_included_packages = kwargs.pop("doc_included_packages", [])
9799
toolchains = kwargs.pop("toolchains", [])
98100

99101
# Construct the java_library we'll export from here.
@@ -123,6 +125,8 @@ def java_export(
123125
doc_deps = doc_deps,
124126
doc_url = doc_url,
125127
doc_resources = doc_resources,
128+
doc_excluded_packages = doc_excluded_packages,
129+
doc_included_packages = doc_included_packages,
126130
toolchains = toolchains,
127131
)
128132

@@ -144,6 +148,8 @@ def maven_export(
144148
doc_deps = [],
145149
doc_url = "",
146150
doc_resources = [],
151+
doc_excluded_packages = [],
152+
doc_included_packages = [],
147153
publish_maven_metadata = True,
148154
toolchains = None):
149155
"""
@@ -207,6 +213,12 @@ def maven_export(
207213
doc_url: The URL at which the generated `javadoc` will be hosted (if not using
208214
`tags = ["no-javadoc"]`).
209215
doc_resources: Resources to be included in the javadoc jar.
216+
doc_excluded_packages: A list of packages to exclude from the generated javadoc. Wildcards are supported at the
217+
end of the package name. For example, `com.example.*` will exclude all the subpackages of `com.example`, while
218+
`com.example` will exclude only the files directly in `com.example`
219+
doc_included_packages: A list of packages to include in the generated javadoc. Wildcards are supported at the
220+
end of the package name. For example, `com.example.*` will include all the subpackages of `com.example`, while
221+
`com.example` will include only the files directly in `com.example`
210222
visibility: The visibility of the target
211223
publish_maven_metadata: Whether to publish a maven-metadata.xml
212224
kwargs: These are passed to [`java_library`](https://bazel.build/reference/be/java#java_library),
@@ -278,6 +290,8 @@ def maven_export(
278290
doc_deps = doc_deps,
279291
doc_url = doc_url,
280292
doc_resources = doc_resources,
293+
excluded_packages = doc_excluded_packages,
294+
included_packages = doc_included_packages,
281295
excluded_workspaces = excluded_workspaces.keys(),
282296
additional_dependencies = additional_dependencies,
283297
visibility = visibility,

private/rules/javadoc.bzl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,28 @@ def generate_javadoc(
2525
javadocopts,
2626
doc_deps,
2727
doc_resources,
28+
excluded_packages,
2829
output,
2930
element_list):
3031
inputs = []
3132
transitive_inputs = []
3233
args = ctx.actions.args()
34+
3335
args.add("--out", output)
3436
args.add("--element-list", element_list)
37+
3538
args.add_all(source_jars, before_each = "--in")
3639
inputs.extend(source_jars)
40+
3741
args.add_all(ctx.files.doc_resources, before_each = "--resources")
3842
inputs.extend(ctx.files.doc_resources)
43+
3944
args.add_all(classpath, before_each = "--cp")
4045
transitive_inputs.append(classpath)
4146

47+
args.add_all(excluded_packages, before_each = "--exclude-packages")
48+
args.add_all(ctx.attr.included_packages, before_each = "--include-packages")
49+
4250
for dep in doc_deps:
4351
dep_info = dep[_JavadocInfo]
4452
args.add("-linkoffline")
@@ -90,6 +98,7 @@ def _javadoc_impl(ctx):
9098
ctx.attr.javadocopts,
9199
ctx.attr.doc_deps,
92100
ctx.attr.doc_resources,
101+
ctx.attr.excluded_packages,
93102
jar_file,
94103
element_list,
95104
)
@@ -153,6 +162,20 @@ javadoc = rule(
153162
allow_files = True,
154163
default = [],
155164
),
165+
"excluded_packages": attr.string_list(
166+
doc = """A list of packages to exclude from the generated javadoc. Wildcards are supported at the end of
167+
the package name. For example, `com.example.*` will exclude all the subpackages of `com.example`, while
168+
`com.example` will exclude only the files directly in `com.example`.""",
169+
allow_empty = True,
170+
default = [],
171+
),
172+
"included_packages": attr.string_list(
173+
doc = """A list of packages to include in the generated javadoc. Wildcards are supported at the end of
174+
the package name. For example, `com.example.*` will include all the subpackages of `com.example`, while
175+
`com.example` will include only the files directly in `com.example`.""",
176+
allow_empty = True,
177+
default = [],
178+
),
156179
"excluded_workspaces": attr.string_list(
157180
doc = "A list of bazel workspace names to exclude from the generated jar",
158181
allow_empty = True,

private/rules/kt_jvm_export.bzl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
22
load(":java_export.bzl", "maven_export")
33
load(":maven_project_jar.bzl", "DEFAULT_EXCLUDED_WORKSPACES")
44

5-
KOTLIN_STDLIB = "@rules_kotlin//kotlin/compiler:kotlin-stdlib"
5+
KOTLIN_STDLIB = "rules_kotlin"
66

77
def kt_jvm_export(
88
name,
@@ -70,11 +70,8 @@ def kt_jvm_export(
7070

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

73-
# ensure that the kotlin-stdlib is included in deploy_env
74-
if KOTLIN_STDLIB not in deploy_env:
75-
updated_deploy_env = deploy_env + [KOTLIN_STDLIB]
76-
else:
77-
updated_deploy_env = deploy_env
73+
updated_excluded_workspaces = {name: None for name in excluded_workspaces}
74+
updated_excluded_workspaces.update({KOTLIN_STDLIB: None})
7875

7976
# Construct the kt_jvm_library we'll export from here.
8077
kt_jvm_library(
@@ -88,8 +85,8 @@ def kt_jvm_export(
8885
name = name,
8986
maven_coordinates = maven_coordinates,
9087
lib_name = lib_name,
91-
deploy_env = updated_deploy_env,
92-
excluded_workspaces = excluded_workspaces,
88+
deploy_env = deploy_env,
89+
excluded_workspaces = updated_excluded_workspaces,
9390
pom_template = pom_template,
9491
visibility = visibility,
9592
tags = tags,
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,46 @@
11
java_library(
2-
name = "create_jar_lib",
3-
srcs = ["CreateJar.java"],
2+
name = "javadoc_lib",
3+
srcs = ["JavadocJarMaker.java"],
4+
visibility = [
5+
"//tests/com/github/bazelbuild/rules_jvm_external/javadoc:__pkg__",
6+
],
47
deps = [
8+
":create_jar_lib",
59
"//private/tools/java/com/github/bazelbuild/rules_jvm_external",
610
"//private/tools/java/com/github/bazelbuild/rules_jvm_external/zip",
711
],
812
)
913

1014
java_binary(
1115
name = "javadoc",
12-
srcs = ["JavadocJarMaker.java"],
1316
main_class = "com.github.bazelbuild.rules_jvm_external.javadoc.JavadocJarMaker",
1417
visibility = [
1518
"//visibility:public",
1619
],
20+
runtime_deps = [
21+
":javadoc_lib",
22+
],
23+
)
24+
25+
java_library(
26+
name = "create_jar_lib",
27+
srcs = ["CreateJar.java"],
28+
visibility = [
29+
"//tests/com/github/bazelbuild/rules_jvm_external/javadoc:__pkg__",
30+
],
1731
deps = [
18-
":create_jar_lib",
1932
"//private/tools/java/com/github/bazelbuild/rules_jvm_external",
2033
"//private/tools/java/com/github/bazelbuild/rules_jvm_external/zip",
2134
],
2235
)
2336

2437
java_binary(
2538
name = "create_jar",
26-
srcs = ["CreateJar.java"],
2739
main_class = "com.github.bazelbuild.rules_jvm_external.javadoc.CreateJar",
2840
visibility = [
2941
"//visibility:public",
3042
],
31-
deps = [
32-
"//private/tools/java/com/github/bazelbuild/rules_jvm_external",
33-
"//private/tools/java/com/github/bazelbuild/rules_jvm_external/zip",
43+
runtime_deps = [
44+
":create_jar_lib",
3445
],
3546
)

private/tools/java/com/github/bazelbuild/rules_jvm_external/javadoc/CreateJar.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.bazelbuild.rules_jvm_external.javadoc;
22

33
import com.github.bazelbuild.rules_jvm_external.zip.StableZipEntry;
4-
54
import java.io.IOException;
65
import java.io.InputStream;
76
import java.io.OutputStream;
@@ -21,10 +20,7 @@ public class CreateJar {
2120

2221
public static void main(String[] args) throws IOException {
2322
Path out = Paths.get(args[0]);
24-
Set<Path> inputs = Stream.of(args)
25-
.skip(1)
26-
.map(Paths::get)
27-
.collect(Collectors.toSet());
23+
Set<Path> inputs = Stream.of(args).skip(1).map(Paths::get).collect(Collectors.toSet());
2824

2925
Path tmpDir = Files.createTempDirectory("create-jar-temp");
3026
tmpDir.toFile().deleteOnExit();
@@ -57,8 +53,8 @@ public static void main(String[] args) throws IOException {
5753

5854
public static void createJar(Path out, Path inputDir) throws IOException {
5955
try (OutputStream os = Files.newOutputStream(out);
60-
ZipOutputStream zos = new ZipOutputStream(os);
61-
Stream<Path> walk = Files.walk(inputDir)) {
56+
ZipOutputStream zos = new ZipOutputStream(os);
57+
Stream<Path> walk = Files.walk(inputDir)) {
6258

6359
walk.sorted(Comparator.naturalOrder())
6460
.forEachOrdered(
@@ -88,4 +84,4 @@ public static void createJar(Path out, Path inputDir) throws IOException {
8884
});
8985
}
9086
}
91-
}
87+
}

0 commit comments

Comments
 (0)