Mark `runtime_deps` as `runtime` scope in generated pom files (#1113)
diff --git a/private/rules/has_maven_deps.bzl b/private/rules/has_maven_deps.bzl index df75b67..93ade76 100644 --- a/private/rules/has_maven_deps.bzl +++ b/private/rules/has_maven_deps.bzl
@@ -5,6 +5,7 @@ # Fields to do with maven coordinates "coordinates": "Maven coordinates for the project, which may be None", "maven_deps": "Depset of first-order maven dependencies", + "maven_runtime_deps": "Depset of first-order maven runtime dependencies", "as_maven_dep": "Depset of this project if used as a maven dependency", # Fields used for generating artifacts @@ -93,6 +94,7 @@ _gathered = provider( fields = [ "all_infos", + "runtime_infos", "label_to_javainfo", "artifact_infos", "transitive_exports", @@ -100,10 +102,14 @@ ], ) -def _extract_from(gathered, maven_info, dep, include_transitive_exports): +def _extract_from(gathered, maven_info, dep, include_transitive_exports, is_runtime_dep): java_info = dep[JavaInfo] if dep and JavaInfo in dep else None gathered.all_infos.append(maven_info) + + if is_runtime_dep: + gathered.runtime_infos.append(maven_info) + gathered.label_to_javainfo.update(maven_info.label_to_javainfo) if java_info: if maven_info.coordinates: @@ -127,6 +133,7 @@ gathered = _gathered( all_infos = [], + runtime_infos = [], artifact_infos = [target[JavaInfo]], transitive_exports = [], dep_infos = [], @@ -137,13 +144,13 @@ for dep in getattr(ctx.rule.attr, attr, []): if MavenHintInfo in dep: for info in dep[MavenHintInfo].maven_infos.to_list(): - _extract_from(gathered, info, None, attr == "exports") + _extract_from(gathered, info, None, attr == "exports", attr == "runtime_deps") if not MavenInfo in dep: continue info = dep[MavenInfo] - _extract_from(gathered, info, dep, attr == "exports") + _extract_from(gathered, info, dep, attr == "exports", attr == "runtime_deps") all_infos = gathered.all_infos artifact_infos = gathered.artifact_infos @@ -151,6 +158,7 @@ dep_infos = gathered.dep_infos label_to_javainfo = gathered.label_to_javainfo maven_deps = depset(transitive = [i.as_maven_dep for i in all_infos]) + maven_runtime_deps = depset(transitive = [i.as_maven_dep for i in gathered.runtime_infos]) transitive_exports_from_exports = depset() if hasattr(ctx.rule.attr, "exports"): @@ -163,6 +171,7 @@ info = MavenInfo( coordinates = coordinates, maven_deps = maven_deps, + maven_runtime_deps = maven_runtime_deps, as_maven_dep = depset([coordinates]) if coordinates else maven_deps, artifact_infos = depset(direct = artifact_infos), dep_infos = depset(direct = dep_infos, transitive = [i.dep_infos for i in all_infos]),
diff --git a/private/rules/maven_utils.bzl b/private/rules/maven_utils.bzl index 0202101..77fd560 100644 --- a/private/rules/maven_utils.bzl +++ b/private/rules/maven_utils.bzl
@@ -85,6 +85,7 @@ parent = None, versioned_dep_coordinates = [], unversioned_dep_coordinates = [], + runtime_deps = [], indent = 8): unpacked_coordinates = unpack_coordinates(coordinates) substitutions = { @@ -111,12 +112,18 @@ substitutions.update({"{parent}": "".join(parts)}) deps = [] - for dep in sorted(versioned_dep_coordinates): + for dep in sorted(versioned_dep_coordinates) + sorted(unversioned_dep_coordinates): + include_version = dep in versioned_dep_coordinates unpacked = unpack_coordinates(dep) - deps.append(format_dep(unpacked, indent = indent)) - for dep in sorted(unversioned_dep_coordinates): - unpacked = unpack_coordinates(dep) - deps.append(format_dep(unpacked, indent = indent, include_version = False)) + new_scope = "runtime" if dep in runtime_deps else unpacked.scope + unpacked = struct( + groupId = unpacked.groupId, + artifactId = unpacked.artifactId, + type = unpacked.type, + scope = new_scope, + version = unpacked.version, + ) + deps.append(format_dep(unpacked, indent = indent, include_version = include_version)) substitutions.update({"{dependencies}": "\n".join(deps)})
diff --git a/private/rules/pom_file.bzl b/private/rules/pom_file.bzl index 2a3825d..9a1177d 100644 --- a/private/rules/pom_file.bzl +++ b/private/rules/pom_file.bzl
@@ -13,13 +13,20 @@ additional_deps = determine_additional_dependencies(artifact_jars, ctx.attr.additional_dependencies) all_maven_deps = info.maven_deps.to_list() + runtime_maven_deps = info.maven_runtime_deps.to_list() + for dep in additional_deps: for coords in dep[MavenInfo].as_maven_dep.to_list(): all_maven_deps.append(coords) + expanded_maven_deps = [ 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) + for coords in runtime_maven_deps + ] # Expand maven coordinates for any variables to be replaced. coordinates = ctx.expand_make_variables("coordinates", info.coordinates, ctx.var) @@ -28,6 +35,7 @@ ctx, coordinates = coordinates, versioned_dep_coordinates = sorted(expanded_maven_deps), + runtime_deps = expanded_runtime_deps, pom_template = ctx.file.pom_template, out_name = "%s.xml" % ctx.label.name, )