Skip to content

Commit d21c82b

Browse files
vinnybodlyao-77patrick-premont
authored
Bazelbuild master conflicts (#1)
* add codeowners (#1) * Add exclusion info to pom.xml (#2) * Make java_export capable of writing exclusions to the POM file it generates. * Reformatted _exclusion_spec_list_to_json_test_impl --------- Co-authored-by: Patrick Prémont <[email protected]> * Add an `implementation_deps` argument to pom generation (#3) * add bazel equivalent of gradle's 'implementation' scope * update get_implementation_coordinates * Update private/rules/maven_utils.bzl * Update private/rules/pom_file.bzl * Add an exception for COPYRIGHT and NOTICE files in the same way LICENSE is already supported * prepend_services argument for java_export (#9) * initial 'prepend services' implementation add tests, remove un-needed lines add comments * initial 'prepend services' implementation add tests, remove un-needed lines add comments * remove System.out.println from MergeJars * match indentation a bit better * match indentation a bit better in MergeJars * trigger ci * Skip non-class files in the exclusions processing (#10) * reimplement license retention on service provider files (#11) * initial 'prepend services' implementation add tests, remove un-needed lines add comments * remove System.out.println from MergeJars * match indentation a bit better * match indentation a bit better in MergeJars * trigger ci * concatenate service files instead of prepending license with extra property * remove unnecessary test * remove 'prepend-services' feature * Remove hardcoded javadocopts * Add support for resources in javadoc jars. Add support for make variable substitution in javadoc arguments. (#14) * Add support for resources in javadoc jars. Add support for make variable substitution in javadoc arguments. * remove unused deps * Add .strip() to assert to account for some difference in windows/mac * DP-14758 - add classifier to generated pom (#17) * add classifier to generated pom * fix typo * Fix pom generation for classifier artifacts * fix bad cherry-pick * support bom generation * add tests * add line missing from patch file * fix incorrect merge conflicts * fix incorrect merge conflicts --------- Co-authored-by: Lan Yao <[email protected]> Co-authored-by: Patrick Prémont <[email protected]>
1 parent 9f2291c commit d21c82b

File tree

17 files changed

+311
-22
lines changed

17 files changed

+311
-22
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @confluentinc/builds-automations-and-repositories

private/rules/has_maven_deps.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ def _has_maven_deps_impl(target, ctx):
129129
return _EMPTY_INFO
130130

131131
coordinates = read_coordinates(ctx.rule.attr.tags)
132-
label_to_javainfo = {target.label: target[JavaInfo]}
133132

134133
gathered = _gathered(
135134
all_infos = [],

private/rules/java_export.bzl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("//:specs.bzl", "parse", _json = "json")
12
load(":javadoc.bzl", "javadoc")
23
load(":maven_bom_fragment.bzl", "maven_bom_fragment")
34
load(":maven_project_jar.bzl", "DEFAULT_EXCLUDED_WORKSPACES", "maven_project_jar")
@@ -10,6 +11,7 @@ def java_export(
1011
manifest_entries = {},
1112
deploy_env = [],
1213
excluded_workspaces = {name: None for name in DEFAULT_EXCLUDED_WORKSPACES},
14+
exclusions = {},
1315
pom_template = None,
1416
visibility = None,
1517
tags = [],
@@ -70,6 +72,9 @@ def java_export(
7072
that should not be included in the maven jar to a `Label` pointing to the dependency
7173
that workspace should be replaced by, or `None` if the exclusion shouldn't be replaced
7274
with an extra dependency.
75+
exclusions: Mapping of target labels to a list of exclusions to be added to the POM file.
76+
Each label must correspond to a direct maven dependency of this target.
77+
Each exclusion is represented as a `group:artifact` string.
7378
classifier_artifacts: A dict of classifier -> artifact of additional artifacts to publish to Maven.
7479
doc_deps: Other `javadoc` targets that are referenced by the generated `javadoc` target
7580
(if not using `tags = ["no-javadoc"]`)
@@ -105,6 +110,7 @@ def java_export(
105110
manifest_entries = manifest_entries,
106111
deploy_env = deploy_env,
107112
excluded_workspaces = excluded_workspaces,
113+
exclusions = exclusions,
108114
pom_template = pom_template,
109115
visibility = visibility,
110116
tags = tags,
@@ -124,6 +130,7 @@ def maven_export(
124130
manifest_entries = {},
125131
deploy_env = [],
126132
excluded_workspaces = {},
133+
exclusions = {},
127134
pom_template = None,
128135
visibility = None,
129136
tags = [],
@@ -188,6 +195,9 @@ def maven_export(
188195
that should not be included in the maven jar to a `Label` pointing to the dependency
189196
that workspace should be replaced by, or `None` if the exclusion shouldn't be replaced
190197
with an extra dependency.
198+
exclusions: Mapping of target labels to a list of exclusions to be added to the POM file.
199+
Each label must correspond to a direct maven dependency of this target.
200+
Each exclusion is represented as a `group:artifact` string.
191201
doc_deps: Other `javadoc` targets that are referenced by the generated `javadoc` target
192202
(if not using `tags = ["no-javadoc"]`)
193203
doc_url: The URL at which the generated `javadoc` will be hosted (if not using
@@ -271,6 +281,13 @@ def maven_export(
271281
)
272282
classifier_artifacts.setdefault("javadoc", docs_jar)
273283

284+
exclusions_dict_json_strings = {
285+
target: _json.write_exclusion_spec_list(
286+
parse.parse_exclusion_spec_list(targetExclusions),
287+
)
288+
for target, targetExclusions in exclusions.items()
289+
}
290+
274291
pom_file(
275292
name = "%s-pom" % name,
276293
target = ":%s" % lib_name,
@@ -280,6 +297,7 @@ def maven_export(
280297
tags = tags,
281298
testonly = testonly,
282299
toolchains = toolchains,
300+
exclusions = exclusions_dict_json_strings,
283301
)
284302

285303
maven_publish(

private/rules/maven_bom.bzl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _maven_bom_impl(ctx):
4141
bom = generate_pom(
4242
ctx,
4343
coordinates = coordinates,
44-
versioned_dep_coordinates = [f[MavenBomFragmentInfo].coordinates for f in ctx.attr.fragments],
44+
versioned_dep_coordinates = [unpack_coordinates(f[MavenBomFragmentInfo].coordinates) for f in ctx.attr.fragments],
4545
pom_template = ctx.file.pom_template,
4646
out_name = "%s.xml" % ctx.label.name,
4747
)
@@ -77,13 +77,22 @@ def _maven_dependencies_bom_impl(ctx):
7777
# included in the main BOM
7878
first_order_deps = [f[MavenBomFragmentInfo].coordinates for f in ctx.attr.fragments]
7979
all_deps = depset(transitive = [f.maven_info.maven_deps for f in fragments]).to_list()
80-
combined_deps = [a for a in all_deps if a not in first_order_deps]
80+
combined_deps = [unpack_coordinates(a) for a in all_deps if a not in first_order_deps]
8181

8282
unpacked = unpack_coordinates(ctx.attr.bom_coordinates)
83+
unpacked = struct(
84+
groupId = unpacked.groupId,
85+
artifactId = unpacked.artifactId,
86+
type = "pom",
87+
scope = "import",
88+
classifier = unpacked.classifier,
89+
version = unpacked.version,
90+
)
91+
8392
dependencies_bom = generate_pom(
8493
ctx,
8594
coordinates = ctx.attr.maven_coordinates,
86-
versioned_dep_coordinates = combined_deps + ["%s:%s:pom:import:%s" % (unpacked.groupId, unpacked.artifactId, unpacked.version)],
95+
versioned_dep_coordinates = combined_deps + [unpacked],
8796
pom_template = ctx.file.pom_template,
8897
out_name = "%s.xml" % ctx.label.name,
8998
indent = 12,

private/rules/maven_utils.bzl

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ load("//private/lib:bzlmod.bzl", "get_module_name_of_owner_of_repo")
22

33
def unpack_coordinates(coords):
44
"""Takes a maven coordinate and unpacks it into a struct with fields
5-
`groupId`, `artifactId`, `version`, `type`, `scope`
5+
`groupId`, `artifactId`, `version`, `type`, `classifier`
66
where type and scope are optional.
77
88
Assumes following maven coordinate syntax:
9-
groupId:artifactId[:type[:scope]]:version
9+
groupId:artifactId[:type[:classifier]]:version
1010
"""
1111
if not coords:
1212
return None
@@ -20,6 +20,7 @@ def unpack_coordinates(coords):
2020
artifactId = parts[1],
2121
type = None,
2222
scope = None,
23+
classifier = None,
2324
version = None,
2425
)
2526

@@ -32,7 +33,8 @@ def unpack_coordinates(coords):
3233
groupId = parts.get(0),
3334
artifactId = parts.get(1),
3435
type = parts.get(2),
35-
scope = parts.get(3),
36+
scope = None,
37+
classifier = parts.get(3),
3638
version = version,
3739
)
3840

@@ -42,7 +44,7 @@ def _whitespace(indent):
4244
whitespace = whitespace + " "
4345
return whitespace
4446

45-
def format_dep(unpacked, indent = 8, include_version = True):
47+
def format_dep(unpacked, indent = 8, include_version = True, exclusions = {}):
4648
whitespace = _whitespace(indent)
4749

4850
dependency = [
@@ -72,6 +74,33 @@ def format_dep(unpacked, indent = 8, include_version = True):
7274
" <scope>%s</scope>\n" % unpacked.scope,
7375
])
7476

77+
if unpacked.classifier:
78+
dependency.extend([
79+
whitespace,
80+
" <classifier>%s</classifier>\n" % unpacked.classifier,
81+
])
82+
83+
if exclusions:
84+
dependency.extend([
85+
whitespace,
86+
" <exclusions>\n",
87+
])
88+
for exclusion in exclusions:
89+
dependency.extend([
90+
whitespace,
91+
" <exclusion>\n",
92+
whitespace,
93+
" <groupId>%s</groupId>\n" % exclusion["group"],
94+
whitespace,
95+
" <artifactId>%s</artifactId>\n" % exclusion["artifact"],
96+
whitespace,
97+
" </exclusion>\n",
98+
])
99+
dependency.extend([
100+
whitespace,
101+
" </exclusions>\n",
102+
])
103+
75104
dependency.extend([
76105
whitespace,
77106
"</dependency>",
@@ -88,16 +117,22 @@ def generate_pom(
88117
versioned_dep_coordinates = [],
89118
unversioned_dep_coordinates = [],
90119
runtime_deps = [],
91-
indent = 8):
120+
indent = 8,
121+
exclusions = {}):
92122
unpacked_coordinates = unpack_coordinates(coordinates)
93123
substitutions = {
94124
"{groupId}": unpacked_coordinates.groupId,
95125
"{artifactId}": unpacked_coordinates.artifactId,
96126
"{version}": unpacked_coordinates.version,
97127
"{type}": unpacked_coordinates.type or "jar",
128+
"{classifier}": unpacked_coordinates.classifier or "",
98129
"{scope}": unpacked_coordinates.scope or "compile",
99130
}
100131

132+
for key in exclusions:
133+
if key not in versioned_dep_coordinates and key not in unversioned_dep_coordinates:
134+
fail("Key %s in exclusions does not occur in versioned_dep_coordinates or unversioned_dep_coordinates" % key)
135+
101136
if parent:
102137
# We only want the groupId, artifactID, and version
103138
unpacked_parent = unpack_coordinates(parent)
@@ -114,18 +149,18 @@ def generate_pom(
114149
substitutions.update({"{parent}": "".join(parts)})
115150

116151
deps = []
117-
for dep in sorted(versioned_dep_coordinates) + sorted(unversioned_dep_coordinates):
152+
for dep in _sort_unpacked(versioned_dep_coordinates) + _sort_unpacked(unversioned_dep_coordinates):
118153
include_version = dep in versioned_dep_coordinates
119-
unpacked = unpack_coordinates(dep)
120-
new_scope = "runtime" if dep in runtime_deps else unpacked.scope
154+
new_scope = "runtime" if dep in runtime_deps else dep.scope
121155
unpacked = struct(
122-
groupId = unpacked.groupId,
123-
artifactId = unpacked.artifactId,
124-
type = unpacked.type,
156+
groupId = dep.groupId,
157+
artifactId = dep.artifactId,
158+
type = dep.type,
125159
scope = new_scope,
126-
version = unpacked.version,
160+
classifier = dep.classifier,
161+
version = dep.version,
127162
)
128-
deps.append(format_dep(unpacked, indent = indent, include_version = include_version))
163+
deps.append(format_dep(unpacked, indent = indent, exclusions = exclusions.get(dep, {}), include_version = include_version))
129164

130165
substitutions.update({"{dependencies}": "\n".join(deps)})
131166

@@ -158,3 +193,11 @@ def determine_additional_dependencies(jar_files, additional_dependencies):
158193
to_return.append(dep)
159194

160195
return to_return
196+
197+
def _sort_unpacked(unpacked_dep):
198+
"""Sorts a list of unpacked dependencies by groupId, artifactId, and version."""
199+
200+
def _sort_key(dep):
201+
return (dep.groupId, dep.artifactId, dep.version)
202+
203+
return sorted(unpacked_dep, key = _sort_key)

private/rules/pom_file.bzl

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@rules_java//java:defs.bzl", "JavaInfo")
22
load(":has_maven_deps.bzl", "MavenInfo", "calculate_artifact_jars", "has_maven_deps")
3-
load(":maven_utils.bzl", "determine_additional_dependencies", "generate_pom")
3+
load(":maven_utils.bzl", "determine_additional_dependencies", "generate_pom", "unpack_coordinates")
44

55
def _pom_file_impl(ctx):
66
# Ensure the target has coordinates
@@ -12,6 +12,18 @@ def _pom_file_impl(ctx):
1212
artifact_jars = calculate_artifact_jars(info)
1313
additional_deps = determine_additional_dependencies(artifact_jars, ctx.attr.additional_dependencies)
1414

15+
def get_exclusion_coordinates(target):
16+
if not info.label_to_javainfo.get(target.label):
17+
fail("exclusions key %s not found in dependencies %s" % (target, info.label_to_javainfo.keys()))
18+
else:
19+
coords = ctx.expand_make_variables("exclusions", target[MavenInfo].coordinates, ctx.var)
20+
return unpack_coordinates(coords)
21+
22+
exclusions = {
23+
get_exclusion_coordinates(target): json.decode(targetExclusions)
24+
for target, targetExclusions in ctx.attr.exclusions.items()
25+
}
26+
1527
all_maven_deps = info.maven_deps.to_list()
1628
runtime_maven_deps = info.maven_runtime_deps.to_list()
1729

@@ -20,11 +32,11 @@ def _pom_file_impl(ctx):
2032
all_maven_deps.append(coords)
2133

2234
expanded_maven_deps = [
23-
ctx.expand_make_variables("additional_deps", coords, ctx.var)
35+
unpack_coordinates(ctx.expand_make_variables("additional_deps", coords, ctx.var))
2436
for coords in all_maven_deps
2537
]
2638
expanded_runtime_deps = [
27-
ctx.expand_make_variables("maven_runtime_deps", coords, ctx.var)
39+
unpack_coordinates(ctx.expand_make_variables("maven_runtime_deps", coords, ctx.var))
2840
for coords in runtime_maven_deps
2941
]
3042

@@ -34,10 +46,11 @@ def _pom_file_impl(ctx):
3446
out = generate_pom(
3547
ctx,
3648
coordinates = coordinates,
37-
versioned_dep_coordinates = sorted(expanded_maven_deps),
49+
versioned_dep_coordinates = expanded_maven_deps,
3850
runtime_deps = expanded_runtime_deps,
3951
pom_template = ctx.file.pom_template,
4052
out_name = "%s.xml" % ctx.label.name,
53+
exclusions = exclusions,
4154
)
4255

4356
return [
@@ -62,7 +75,8 @@ The following substitutions are performed on the template file:
6275
{type}: Replaced by the maven coordinates type, if present (defaults to "jar")
6376
{scope}: Replaced by the maven coordinates type, if present (defaults to "compile")
6477
{dependencies}: Replaced by a list of maven dependencies directly relied upon
65-
by java_library targets within the artifact.
78+
by java_library targets within the artifact. Dependencies have exclusions
79+
for any transitive dependencies that are occur in deploy_env.
6680
""",
6781
attrs = {
6882
"pom_template": attr.label(
@@ -90,5 +104,12 @@ The following substitutions are performed on the template file:
90104
has_maven_deps,
91105
],
92106
),
107+
"exclusions": attr.label_keyed_string_dict(
108+
doc = "Mapping of dependency labels to a list of exclusions (encoded as a json string). Each exclusion is a dict with a group and an artifact.",
109+
allow_empty = True,
110+
aspects = [
111+
has_maven_deps,
112+
],
113+
),
93114
},
94115
)

specs.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ def _exclusion_spec_to_json(exclusion_spec):
212212
"""
213213
return "{ \"group\": \"" + exclusion_spec["group"] + "\", \"artifact\": \"" + exclusion_spec["artifact"] + "\" }"
214214

215+
def _exclusion_spec_list_to_json(exclusion_spec):
216+
"""
217+
Given a list of artifact exclusion spec, returns its json serialization.
218+
"""
219+
return "[" + ", ".join([_exclusion_spec_to_json(spec) for spec in exclusion_spec]) + "]"
220+
215221
def _override_license_types_spec_to_json(override_license_types_spec):
216222
"""
217223
Given an override license types spec, returns the json serialization of the object.
@@ -248,6 +254,7 @@ json = struct(
248254
write_repository_credentials_spec = _repository_credentials_spec_to_json,
249255
write_repository_spec = _repository_spec_to_json,
250256
write_exclusion_spec = _exclusion_spec_to_json,
257+
write_exclusion_spec_list = _exclusion_spec_list_to_json,
251258
write_override_license_types_spec = _override_license_types_spec_to_json,
252259
write_artifact_spec = _artifact_spec_to_json,
253260
)

tests/com/github/bazelbuild/rules_jvm_external/javadoc/BUILD

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,19 @@ java_test(
1515
artifact("com.google.guava:guava"),
1616
],
1717
)
18+
19+
java_test(
20+
name = "ResourceTest",
21+
srcs = ["ResourceTest.java"],
22+
test_class = "com.github.bazelbuild.rules_jvm_external.javadoc.ResourceTest",
23+
deps = [
24+
"//private/tools/java/com/github/bazelbuild/rules_jvm_external",
25+
"//private/tools/java/com/github/bazelbuild/rules_jvm_external/javadoc",
26+
"//tests/com/github/bazelbuild/rules_jvm_external:zip_utils",
27+
artifact(
28+
"junit:junit",
29+
repository_name = "regression_testing_coursier",
30+
),
31+
artifact("com.google.guava:guava"),
32+
],
33+
)

tests/integration/maven_bom/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ java_export(
3737
name = "one-dep",
3838
srcs = ["OneDep.java"],
3939
maven_coordinates = "com.example:one-dep:1.0.0",
40+
visibility = [
41+
":__pkg__",
42+
"//tests/integration/pom_file:__pkg__",
43+
],
4044
deps = [
4145
artifact("com.google.guava:guava"),
4246
],

0 commit comments

Comments
 (0)