Skip to content

Commit

Permalink
Expose list of all artifacts, including transitive
Browse files Browse the repository at this point in the history
And add support for :type and :classifier in defs.bzl:artifact() by internally
using strip_packaging_and_classifier_and_version. To keep support for groupId:artifactId
update strip_packaging_and_classifier_and_version implementation for that case.

To be used like
    load("@maven//:defs.bzl", "maven_artifacts")

and for example
    load("@rules_jvm_external//:defs.bzl", "artifact")
    load("@rules_jvm_external//:specs.bzl", "parse")

    all_maven_targets = [artifact(a) for a in maven_artifacts if not parse.parse_maven_coordinate(a).get("classifier", "compile") in ["sources", "native"]]

    java_export(
        name = "maven-transitive-deps-lib",
        maven_coordinates = "com.example.deps:%s:%s" % (name, "0.1"),
        visibility = ["//visibility:public"],
        runtime_deps = all_maven_targets,
    )

which makes "maven-transitive-deps-lib-pom" target available that
can be useful for other tools like security scanners and/or dependabot
  • Loading branch information
dmivankov committed Jan 24, 2022
1 parent 1e5efcb commit 0ce871c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
6 changes: 5 additions & 1 deletion coursier.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ load("//third_party/bazel_json/lib:json_parser.bzl", _json_parse = "json_parse")
load("//private/rules:jetifier.bzl", "jetify_artifact_dependencies", "jetify_maven_coord")
load("//:specs.bzl", "maven", "parse", "utils")
load("//private:artifact_utilities.bzl", "deduplicate_and_sort_artifacts")
load("//private:coursier_utilities.bzl", "SUPPORTED_PACKAGING_TYPES", "escape")
load("//private:coursier_utilities.bzl", "SUPPORTED_PACKAGING_TYPES", "escape", "strip_packaging_and_classifier")
load("//private:dependency_tree_parser.bzl", "JETIFY_INCLUDE_LIST_JETIFY_ALL", "parser")
load("//private:java_utilities.bzl", "parse_java_version")
load("//private:proxy.bzl", "get_java_proxy_args")
Expand Down Expand Up @@ -471,11 +471,13 @@ def _pinned_coursier_fetch_impl(repository_ctx):
"load(\"@bazel_tools//tools/build_defs/repo:utils.bzl\", \"maybe\")",
"def pinned_maven_install():",
]
maven_artifacts = []
netrc_entries = {}

for artifact in dep_tree["dependencies"]:
if artifact.get("url") != None:
http_file_repository_name = escape(artifact["coord"])
maven_artifacts.extend([artifact["coord"]])
http_files.extend([
" http_file(",
" name = \"%s\"," % http_file_repository_name,
Expand All @@ -499,6 +501,8 @@ def _pinned_coursier_fetch_impl(repository_ctx):

http_files.extend(_get_jq_http_files())

http_files.extend(["maven_artifacts = [\n%s\n]" % (",\n".join([" \"%s\"" % artifact for artifact in maven_artifacts]))])

repository_ctx.file("defs.bzl", "\n".join(http_files), executable = False)
repository_ctx.file(
"netrc",
Expand Down
4 changes: 4 additions & 0 deletions private/coursier_utilities.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def strip_packaging_and_classifier(coord):
return ":".join(coordinates)

def strip_packaging_and_classifier_and_version(coord):
coordinates = coord.split(":")
# Support for simplified versionless groupId:artifactId coordinate format
if len(coordinates) == 2:
return ":".join(coordinates)
return ":".join(strip_packaging_and_classifier(coord).split(":")[:-1])

# TODO: Should these methods be combined with _parse_maven_coordinate_string in specs.
Expand Down
14 changes: 6 additions & 8 deletions private/rules/artifact.bzl
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
load("//:specs.bzl", "parse")
load("//private:constants.bzl", "DEFAULT_REPOSITORY_NAME")
load("//private:coursier_utilities.bzl", "strip_packaging_and_classifier_and_version")

def artifact(a, repository_name = DEFAULT_REPOSITORY_NAME):
artifact_obj = _parse_artifact_str(a) if type(a) == "string" else a
return "@%s//:%s" % (repository_name, _escape(artifact_obj["group"] + ":" + artifact_obj["artifact"]))
artifact_str = _make_artifact_str(a) if type(a) != "string" else a
return "@%s//:%s" % (repository_name, _escape(strip_packaging_and_classifier_and_version(artifact_str)))

def maven_artifact(a):
return artifact(a, repository_name = DEFAULT_REPOSITORY_NAME)

def _escape(string):
return string.replace(".", "_").replace("-", "_").replace(":", "_")

def _parse_artifact_str(artifact_str):
pieces = artifact_str.split(":")
if len(pieces) == 2:
return {"group": pieces[0], "artifact": pieces[1]}
else:
return parse.parse_maven_coordinate(artifact_str)
def _make_artifact_str(artifact_obj):
# TODO: add support for optional type, classifier and version parts in artifact_obj case
return artifact_obj["group"] + ":" + artifact_obj["artifact"]

0 comments on commit 0ce871c

Please sign in to comment.