Skip to content

Commit

Permalink
Bazelbuild master conflicts (#1)
Browse files Browse the repository at this point in the history
* 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 <ppremont@confluent.io>

* 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 <111819456+lyao-77@users.noreply.github.com>
Co-authored-by: Patrick Prémont <ppremont@confluent.io>
  • Loading branch information
3 people authored Sep 9, 2024
1 parent 9f2291c commit d21c82b
Show file tree
Hide file tree
Showing 17 changed files with 311 additions and 22 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @confluentinc/builds-automations-and-repositories
1 change: 0 additions & 1 deletion private/rules/has_maven_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ def _has_maven_deps_impl(target, ctx):
return _EMPTY_INFO

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

gathered = _gathered(
all_infos = [],
Expand Down
18 changes: 18 additions & 0 deletions private/rules/java_export.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//:specs.bzl", "parse", _json = "json")
load(":javadoc.bzl", "javadoc")
load(":maven_bom_fragment.bzl", "maven_bom_fragment")
load(":maven_project_jar.bzl", "DEFAULT_EXCLUDED_WORKSPACES", "maven_project_jar")
Expand All @@ -10,6 +11,7 @@ def java_export(
manifest_entries = {},
deploy_env = [],
excluded_workspaces = {name: None for name in DEFAULT_EXCLUDED_WORKSPACES},
exclusions = {},
pom_template = None,
visibility = None,
tags = [],
Expand Down Expand Up @@ -70,6 +72,9 @@ def java_export(
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.
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.
classifier_artifacts: A dict of classifier -> artifact of additional artifacts to publish to Maven.
doc_deps: Other `javadoc` targets that are referenced by the generated `javadoc` target
(if not using `tags = ["no-javadoc"]`)
Expand Down Expand Up @@ -105,6 +110,7 @@ def java_export(
manifest_entries = manifest_entries,
deploy_env = deploy_env,
excluded_workspaces = excluded_workspaces,
exclusions = exclusions,
pom_template = pom_template,
visibility = visibility,
tags = tags,
Expand All @@ -124,6 +130,7 @@ def maven_export(
manifest_entries = {},
deploy_env = [],
excluded_workspaces = {},
exclusions = {},
pom_template = None,
visibility = None,
tags = [],
Expand Down Expand Up @@ -188,6 +195,9 @@ def maven_export(
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.
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.
doc_deps: Other `javadoc` targets that are referenced by the generated `javadoc` target
(if not using `tags = ["no-javadoc"]`)
doc_url: The URL at which the generated `javadoc` will be hosted (if not using
Expand Down Expand Up @@ -271,6 +281,13 @@ def maven_export(
)
classifier_artifacts.setdefault("javadoc", docs_jar)

exclusions_dict_json_strings = {
target: _json.write_exclusion_spec_list(
parse.parse_exclusion_spec_list(targetExclusions),
)
for target, targetExclusions in exclusions.items()
}

pom_file(
name = "%s-pom" % name,
target = ":%s" % lib_name,
Expand All @@ -280,6 +297,7 @@ def maven_export(
tags = tags,
testonly = testonly,
toolchains = toolchains,
exclusions = exclusions_dict_json_strings,
)

maven_publish(
Expand Down
15 changes: 12 additions & 3 deletions private/rules/maven_bom.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _maven_bom_impl(ctx):
bom = generate_pom(
ctx,
coordinates = coordinates,
versioned_dep_coordinates = [f[MavenBomFragmentInfo].coordinates for f in ctx.attr.fragments],
versioned_dep_coordinates = [unpack_coordinates(f[MavenBomFragmentInfo].coordinates) for f in ctx.attr.fragments],
pom_template = ctx.file.pom_template,
out_name = "%s.xml" % ctx.label.name,
)
Expand Down Expand Up @@ -77,13 +77,22 @@ def _maven_dependencies_bom_impl(ctx):
# included in the main BOM
first_order_deps = [f[MavenBomFragmentInfo].coordinates for f in ctx.attr.fragments]
all_deps = depset(transitive = [f.maven_info.maven_deps for f in fragments]).to_list()
combined_deps = [a for a in all_deps if a not in first_order_deps]
combined_deps = [unpack_coordinates(a) for a in all_deps if a not in first_order_deps]

unpacked = unpack_coordinates(ctx.attr.bom_coordinates)
unpacked = struct(
groupId = unpacked.groupId,
artifactId = unpacked.artifactId,
type = "pom",
scope = "import",
classifier = unpacked.classifier,
version = unpacked.version,
)

dependencies_bom = generate_pom(
ctx,
coordinates = ctx.attr.maven_coordinates,
versioned_dep_coordinates = combined_deps + ["%s:%s:pom:import:%s" % (unpacked.groupId, unpacked.artifactId, unpacked.version)],
versioned_dep_coordinates = combined_deps + [unpacked],
pom_template = ctx.file.pom_template,
out_name = "%s.xml" % ctx.label.name,
indent = 12,
Expand Down
69 changes: 56 additions & 13 deletions private/rules/maven_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ load("//private/lib:bzlmod.bzl", "get_module_name_of_owner_of_repo")

def unpack_coordinates(coords):
"""Takes a maven coordinate and unpacks it into a struct with fields
`groupId`, `artifactId`, `version`, `type`, `scope`
`groupId`, `artifactId`, `version`, `type`, `classifier`
where type and scope are optional.
Assumes following maven coordinate syntax:
groupId:artifactId[:type[:scope]]:version
groupId:artifactId[:type[:classifier]]:version
"""
if not coords:
return None
Expand All @@ -20,6 +20,7 @@ def unpack_coordinates(coords):
artifactId = parts[1],
type = None,
scope = None,
classifier = None,
version = None,
)

Expand All @@ -32,7 +33,8 @@ def unpack_coordinates(coords):
groupId = parts.get(0),
artifactId = parts.get(1),
type = parts.get(2),
scope = parts.get(3),
scope = None,
classifier = parts.get(3),
version = version,
)

Expand All @@ -42,7 +44,7 @@ def _whitespace(indent):
whitespace = whitespace + " "
return whitespace

def format_dep(unpacked, indent = 8, include_version = True):
def format_dep(unpacked, indent = 8, include_version = True, exclusions = {}):
whitespace = _whitespace(indent)

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

if unpacked.classifier:
dependency.extend([
whitespace,
" <classifier>%s</classifier>\n" % unpacked.classifier,
])

if exclusions:
dependency.extend([
whitespace,
" <exclusions>\n",
])
for exclusion in exclusions:
dependency.extend([
whitespace,
" <exclusion>\n",
whitespace,
" <groupId>%s</groupId>\n" % exclusion["group"],
whitespace,
" <artifactId>%s</artifactId>\n" % exclusion["artifact"],
whitespace,
" </exclusion>\n",
])
dependency.extend([
whitespace,
" </exclusions>\n",
])

dependency.extend([
whitespace,
"</dependency>",
Expand All @@ -88,16 +117,22 @@ def generate_pom(
versioned_dep_coordinates = [],
unversioned_dep_coordinates = [],
runtime_deps = [],
indent = 8):
indent = 8,
exclusions = {}):
unpacked_coordinates = unpack_coordinates(coordinates)
substitutions = {
"{groupId}": unpacked_coordinates.groupId,
"{artifactId}": unpacked_coordinates.artifactId,
"{version}": unpacked_coordinates.version,
"{type}": unpacked_coordinates.type or "jar",
"{classifier}": unpacked_coordinates.classifier or "",
"{scope}": unpacked_coordinates.scope or "compile",
}

for key in exclusions:
if key not in versioned_dep_coordinates and key not in unversioned_dep_coordinates:
fail("Key %s in exclusions does not occur in versioned_dep_coordinates or unversioned_dep_coordinates" % key)

if parent:
# We only want the groupId, artifactID, and version
unpacked_parent = unpack_coordinates(parent)
Expand All @@ -114,18 +149,18 @@ def generate_pom(
substitutions.update({"{parent}": "".join(parts)})

deps = []
for dep in sorted(versioned_dep_coordinates) + sorted(unversioned_dep_coordinates):
for dep in _sort_unpacked(versioned_dep_coordinates) + _sort_unpacked(unversioned_dep_coordinates):
include_version = dep in versioned_dep_coordinates
unpacked = unpack_coordinates(dep)
new_scope = "runtime" if dep in runtime_deps else unpacked.scope
new_scope = "runtime" if dep in runtime_deps else dep.scope
unpacked = struct(
groupId = unpacked.groupId,
artifactId = unpacked.artifactId,
type = unpacked.type,
groupId = dep.groupId,
artifactId = dep.artifactId,
type = dep.type,
scope = new_scope,
version = unpacked.version,
classifier = dep.classifier,
version = dep.version,
)
deps.append(format_dep(unpacked, indent = indent, include_version = include_version))
deps.append(format_dep(unpacked, indent = indent, exclusions = exclusions.get(dep, {}), include_version = include_version))

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

Expand Down Expand Up @@ -158,3 +193,11 @@ def determine_additional_dependencies(jar_files, additional_dependencies):
to_return.append(dep)

return to_return

def _sort_unpacked(unpacked_dep):
"""Sorts a list of unpacked dependencies by groupId, artifactId, and version."""

def _sort_key(dep):
return (dep.groupId, dep.artifactId, dep.version)

return sorted(unpacked_dep, key = _sort_key)
31 changes: 26 additions & 5 deletions private/rules/pom_file.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_java//java:defs.bzl", "JavaInfo")
load(":has_maven_deps.bzl", "MavenInfo", "calculate_artifact_jars", "has_maven_deps")
load(":maven_utils.bzl", "determine_additional_dependencies", "generate_pom")
load(":maven_utils.bzl", "determine_additional_dependencies", "generate_pom", "unpack_coordinates")

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

def get_exclusion_coordinates(target):
if not info.label_to_javainfo.get(target.label):
fail("exclusions key %s not found in dependencies %s" % (target, info.label_to_javainfo.keys()))
else:
coords = ctx.expand_make_variables("exclusions", target[MavenInfo].coordinates, ctx.var)
return unpack_coordinates(coords)

exclusions = {
get_exclusion_coordinates(target): json.decode(targetExclusions)
for target, targetExclusions in ctx.attr.exclusions.items()
}

all_maven_deps = info.maven_deps.to_list()
runtime_maven_deps = info.maven_runtime_deps.to_list()

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

expanded_maven_deps = [
ctx.expand_make_variables("additional_deps", coords, ctx.var)
unpack_coordinates(ctx.expand_make_variables("additional_deps", coords, ctx.var))
for coords in all_maven_deps
]
expanded_runtime_deps = [
ctx.expand_make_variables("maven_runtime_deps", coords, ctx.var)
unpack_coordinates(ctx.expand_make_variables("maven_runtime_deps", coords, ctx.var))
for coords in runtime_maven_deps
]

Expand All @@ -34,10 +46,11 @@ def _pom_file_impl(ctx):
out = generate_pom(
ctx,
coordinates = coordinates,
versioned_dep_coordinates = sorted(expanded_maven_deps),
versioned_dep_coordinates = expanded_maven_deps,
runtime_deps = expanded_runtime_deps,
pom_template = ctx.file.pom_template,
out_name = "%s.xml" % ctx.label.name,
exclusions = exclusions,
)

return [
Expand All @@ -62,7 +75,8 @@ The following substitutions are performed on the template file:
{type}: Replaced by the maven coordinates type, if present (defaults to "jar")
{scope}: Replaced by the maven coordinates type, if present (defaults to "compile")
{dependencies}: Replaced by a list of maven dependencies directly relied upon
by java_library targets within the artifact.
by java_library targets within the artifact. Dependencies have exclusions
for any transitive dependencies that are occur in deploy_env.
""",
attrs = {
"pom_template": attr.label(
Expand Down Expand Up @@ -90,5 +104,12 @@ The following substitutions are performed on the template file:
has_maven_deps,
],
),
"exclusions": attr.label_keyed_string_dict(
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.",
allow_empty = True,
aspects = [
has_maven_deps,
],
),
},
)
7 changes: 7 additions & 0 deletions specs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ def _exclusion_spec_to_json(exclusion_spec):
"""
return "{ \"group\": \"" + exclusion_spec["group"] + "\", \"artifact\": \"" + exclusion_spec["artifact"] + "\" }"

def _exclusion_spec_list_to_json(exclusion_spec):
"""
Given a list of artifact exclusion spec, returns its json serialization.
"""
return "[" + ", ".join([_exclusion_spec_to_json(spec) for spec in exclusion_spec]) + "]"

def _override_license_types_spec_to_json(override_license_types_spec):
"""
Given an override license types spec, returns the json serialization of the object.
Expand Down Expand Up @@ -248,6 +254,7 @@ json = struct(
write_repository_credentials_spec = _repository_credentials_spec_to_json,
write_repository_spec = _repository_spec_to_json,
write_exclusion_spec = _exclusion_spec_to_json,
write_exclusion_spec_list = _exclusion_spec_list_to_json,
write_override_license_types_spec = _override_license_types_spec_to_json,
write_artifact_spec = _artifact_spec_to_json,
)
Expand Down
16 changes: 16 additions & 0 deletions tests/com/github/bazelbuild/rules_jvm_external/javadoc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,19 @@ java_test(
artifact("com.google.guava:guava"),
],
)

java_test(
name = "ResourceTest",
srcs = ["ResourceTest.java"],
test_class = "com.github.bazelbuild.rules_jvm_external.javadoc.ResourceTest",
deps = [
"//private/tools/java/com/github/bazelbuild/rules_jvm_external",
"//private/tools/java/com/github/bazelbuild/rules_jvm_external/javadoc",
"//tests/com/github/bazelbuild/rules_jvm_external:zip_utils",
artifact(
"junit:junit",
repository_name = "regression_testing_coursier",
),
artifact("com.google.guava:guava"),
],
)
4 changes: 4 additions & 0 deletions tests/integration/maven_bom/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ java_export(
name = "one-dep",
srcs = ["OneDep.java"],
maven_coordinates = "com.example:one-dep:1.0.0",
visibility = [
":__pkg__",
"//tests/integration/pom_file:__pkg__",
],
deps = [
artifact("com.google.guava:guava"),
],
Expand Down
Loading

0 comments on commit d21c82b

Please sign in to comment.