From 5d26d0fe9c20f59d0a4bd26cdf7db4e19c2325db Mon Sep 17 00:00:00 2001 From: Krish Narukulla Date: Tue, 13 Dec 2022 21:42:50 -0600 Subject: [PATCH] extra artifacts is implemented --- examples/java-export/BUILD | 4 +- .../rulesjvmexternal/example/export/BUILD | 10 +++++ private/rules/java_export.bzl | 12 +++++- private/rules/maven_bom_fragment.bzl | 4 +- private/rules/maven_publish.bzl | 11 +++++- private/rules/pom_file.bzl | 3 +- .../jvm/external/maven/MavenPublisher.java | 38 ++++++++++++++++++- 7 files changed, 70 insertions(+), 12 deletions(-) diff --git a/examples/java-export/BUILD b/examples/java-export/BUILD index 74810c933..64bddd307 100644 --- a/examples/java-export/BUILD +++ b/examples/java-export/BUILD @@ -1,5 +1,4 @@ load("@rules_jvm_external//:defs.bzl", "java_export", "maven_bom") - # To export the file, run: # # `bazel run //:example-export.publish --define "maven_repo=file://$(pwd)/repository"` @@ -9,6 +8,9 @@ load("@rules_jvm_external//:defs.bzl", "java_export", "maven_bom") java_export( name = "example-export", + extra_artifacts = { + "release": "//src/main/java/com/github/bazelbuild/rulesjvmexternal/example/export:tar", + }, maven_coordinates = "com.example:bazel-example:0.0.1", runtime_deps = [ "//src/main/java/com/github/bazelbuild/rulesjvmexternal/example/export", diff --git a/examples/java-export/src/main/java/com/github/bazelbuild/rulesjvmexternal/example/export/BUILD b/examples/java-export/src/main/java/com/github/bazelbuild/rulesjvmexternal/example/export/BUILD index d8717ddd2..62db4414f 100644 --- a/examples/java-export/src/main/java/com/github/bazelbuild/rulesjvmexternal/example/export/BUILD +++ b/examples/java-export/src/main/java/com/github/bazelbuild/rulesjvmexternal/example/export/BUILD @@ -1,4 +1,5 @@ load("@rules_jvm_external//:defs.bzl", "artifact") +load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar") java_library( name = "export", @@ -11,3 +12,12 @@ java_library( artifact("com.google.guava:guava"), ], ) + +pkg_tar( + name = "tar", + srcs = glob(["*"]), + extension = "tar.gz", + visibility = [ + "//:__pkg__", + ], +) diff --git a/private/rules/java_export.bzl b/private/rules/java_export.bzl index 06b8acc90..b495678d6 100644 --- a/private/rules/java_export.bzl +++ b/private/rules/java_export.bzl @@ -12,6 +12,7 @@ def java_export( visibility = None, tags = [], testonly = None, + extra_artifacts = {}, **kwargs): """Extends `java_library` to allow maven artifacts to be uploaded. @@ -91,6 +92,7 @@ def java_export( testonly, lib_name, javadocopts, + extra_artifacts, ) def maven_export( @@ -103,7 +105,8 @@ def maven_export( tags, testonly, lib_name, - javadocopts): + javadocopts, + extra_artifacts = {}): """Helper rule to reuse this code for both java_export and kt_jvm_export. After a library has already been created (either a kt_jvm_library or @@ -113,7 +116,7 @@ def maven_export( All arguments are the same as java_export with the addition of: lib_name: Name of the library that has been built. javadocopts: The options to be used for javadocs. - + extra_artifacts: Dictionary: classifier -> artifact """ # Merge the jars to create the maven project jar @@ -162,6 +165,9 @@ def maven_export( testonly = testonly, ) + extra_artifacts_targets = extra_artifacts.values() + extra_classifiers = extra_artifacts.keys() + pom_file( name = "%s-pom" % name, target = ":%s" % lib_name, @@ -172,6 +178,8 @@ def maven_export( ) maven_publish( + extra_artifacts = extra_artifacts_targets, + extra_classifiers = extra_classifiers, name = "%s.publish" % name, coordinates = maven_coordinates, pom = "%s-pom" % name, diff --git a/private/rules/maven_bom_fragment.bzl b/private/rules/maven_bom_fragment.bzl index 97e980cae..9c3c2d263 100644 --- a/private/rules/maven_bom_fragment.bzl +++ b/private/rules/maven_bom_fragment.bzl @@ -12,11 +12,9 @@ MavenBomFragmentInfo = provider( ) def _maven_bom_fragment_impl(ctx): - # Expand maven coordinates for any variables to be replaced. - coordinates = ctx.attr.maven_coordinates.format(**ctx.var) return [ MavenBomFragmentInfo( - coordinates = coordinates, + coordinates = ctx.attr.maven_coordinates, artifact = ctx.file.artifact, srcs = ctx.file.src_artifact, javadocs = ctx.file.javadoc_artifact, diff --git a/private/rules/maven_publish.bzl b/private/rules/maven_publish.bzl index 21a3af14b..cfc0139b9 100644 --- a/private/rules/maven_publish.bzl +++ b/private/rules/maven_publish.bzl @@ -5,13 +5,15 @@ MavenPublishInfo = provider( "javadocs": "Javadoc jar file for documentation files", "artifact_jar": "Jar with the code and metadata for execution", "source_jar": "Jar with the source code for review", + "extra_classifiers": "Extra classifiers for Artifacts", + "extra_artifacts": "Extra artifacts such as tars, wars", }, ) _TEMPLATE = """#!/usr/bin/env bash echo "Uploading {coordinates} to {maven_repo}" -{uploader} {maven_repo} {gpg_sign} {user} {password} {coordinates} {pom} {artifact_jar} {source_jar} {javadoc} +{uploader} {maven_repo} {gpg_sign} {user} {password} {coordinates} {pom} {artifact_jar} {source_jar} {javadoc} {extra_classifiers} {extra_artifacts} """ def _maven_publish_impl(ctx): @@ -27,6 +29,7 @@ def _maven_publish_impl(ctx): artifacts_short_path = ctx.file.artifact_jar.short_path if ctx.file.artifact_jar else "''" source_short_path = ctx.file.source_jar.short_path if ctx.file.source_jar else "''" javadocs_short_path = ctx.file.javadocs.short_path if ctx.file.javadocs else "''" + extra_artifacts = [file.short_path for file in ctx.files.extra_artifacts] ctx.actions.write( output = executable, @@ -42,10 +45,12 @@ def _maven_publish_impl(ctx): artifact_jar = artifacts_short_path, source_jar = source_short_path, javadoc = javadocs_short_path, + extra_classifiers = ",".join(ctx.attr.extra_classifiers), + extra_artifacts = ",".join(extra_artifacts), ), ) - files = [ctx.file.pom] + files = ctx.files.extra_artifacts + [ctx.file.pom] if ctx.file.artifact_jar: files.append(ctx.file.artifact_jar) if ctx.file.source_jar: @@ -104,6 +109,8 @@ When signing with GPG, the current default key is used. "source_jar": attr.label( allow_single_file = True, ), + "extra_classifiers": attr.string_list(), + "extra_artifacts": attr.label_list(), "_uploader": attr.label( executable = True, cfg = "exec", diff --git a/private/rules/pom_file.bzl b/private/rules/pom_file.bzl index 1cea8486a..1ca74f2ce 100644 --- a/private/rules/pom_file.bzl +++ b/private/rules/pom_file.bzl @@ -8,10 +8,9 @@ def _pom_file_impl(ctx): info = ctx.attr.target[MavenInfo] - coordinates = info.coordinates.format(**ctx.var) out = generate_pom( ctx, - coordinates = coordinates, + coordinates = info.coordinates, versioned_dep_coordinates = sorted(info.maven_deps.to_list()), pom_template = ctx.file.pom_template, out_name = "%s.xml" % ctx.label.name, diff --git a/private/tools/java/rules/jvm/external/maven/MavenPublisher.java b/private/tools/java/rules/jvm/external/maven/MavenPublisher.java index 3412a7980..891c6d0c9 100644 --- a/private/tools/java/rules/jvm/external/maven/MavenPublisher.java +++ b/private/tools/java/rules/jvm/external/maven/MavenPublisher.java @@ -89,6 +89,8 @@ public static void main(String[] args) Path binJar = getPathIfSet(args[6]); Path srcJar = getPathIfSet(args[7]); Path docJar = getPathIfSet(args[8]); + String extraClassifiers = args.length > 9 ? args[9] : null; + String extraArtifacts = args.length > 10 ? args[10] : null; try { List> futures = new ArrayList<>(); @@ -106,8 +108,12 @@ public static void main(String[] args) futures.add(upload(repo, credentials, coords, "-javadoc.jar", docJar, gpgSign)); } - CompletableFuture all = - CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); + if(extraClassifiers != null && extraArtifacts != null) { + LOG.info(String.format("extraClassifiers: %s, extraArtifacts: %s", extraClassifiers, extraArtifacts)); + futures.add(uploadExtraArtifacts(repo, credentials, coords, extraClassifiers, extraArtifacts)); + } + + CompletableFuture all = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); all.get(30, MINUTES); } finally { EXECUTOR.shutdown(); @@ -170,6 +176,34 @@ private static CompletableFuture upload( return CompletableFuture.allOf(uploads.toArray(new CompletableFuture[0])); } + private static CompletableFuture uploadExtraArtifacts( + String repo, + Credentials credentials, + Coordinates coords, + String extraClassifiers, + String extraArtifacts + ){ + List> uploads = new ArrayList<>(); + String[] extraClassifiersArr = extraClassifiers.split(","); + String[] extraArtifactsArr = extraArtifacts.split(","); + for(int index=0; index[0])); + } + private static String toSha1(byte[] toHash) { return toHexS("%040x", "SHA-1", toHash); }