From 006f6ff3353e1dd743ba5fdf849cf681ef880e4f 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_publish.bzl | 11 ++++-- .../jvm/external/maven/MavenPublisher.java | 34 +++++++++++++++++++ 5 files changed, 66 insertions(+), 5 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_publish.bzl b/private/rules/maven_publish.bzl index 8be0e769e..7ccf27aab 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/tools/java/rules/jvm/external/maven/MavenPublisher.java b/private/tools/java/rules/jvm/external/maven/MavenPublisher.java index 432bb64bb..7e65fc424 100644 --- a/private/tools/java/rules/jvm/external/maven/MavenPublisher.java +++ b/private/tools/java/rules/jvm/external/maven/MavenPublisher.java @@ -87,6 +87,8 @@ public static void main(String[] args) throws IOException, InterruptedException, Path binJar = getPathIfSet(args[6]); Path srcJar = getPathIfSet(args[7]); Path docJar = getPathIfSet(args[8]); + String extraClassifiers = args.length == 10? args[9] : null; + String extraArtifacts = args.length == 11 ? args[10] : null; try { List> futures = new ArrayList<>(); @@ -104,6 +106,10 @@ public static void main(String[] args) throws IOException, InterruptedException, futures.add(upload(repo, credentials, coords, "-javadoc.jar", docJar, gpgSign)); } + if(extraClassifiers != null && extraArtifacts != null) { + futures.add(uploadExtraArtifacts(repo, credentials, coords, extraClassifiers, extraArtifacts)); + } + CompletableFuture all = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); all.get(30, MINUTES); } finally { @@ -165,6 +171,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); }