Skip to content

Commit

Permalink
extra artifacts is implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
knarukulla committed Dec 14, 2022
1 parent e15c3a2 commit 3b2b950
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
4 changes: 3 additions & 1 deletion examples/java-export/BUILD
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -11,3 +12,12 @@ java_library(
artifact("com.google.guava:guava"),
],
)

pkg_tar(
name = "tar",
srcs = glob(["*"]),
extension = "tar.gz2",
visibility = [
"//:__pkg__",
],
)
10 changes: 9 additions & 1 deletion private/rules/java_export.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -91,6 +92,7 @@ def java_export(
testonly,
lib_name,
javadocopts,
extra_artifacts,
)

def maven_export(
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions private/rules/maven_publish.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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",
Expand Down
37 changes: 37 additions & 0 deletions private/tools/java/rules/jvm/external/maven/MavenPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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[9];
String extraArtifacts = args[10];

try {
List<CompletableFuture<Void>> futures = new ArrayList<>();
Expand All @@ -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) {
uploadextraArtifacts(repo, credentials, coords, extraClassifiers, extraArtifacts);
}

CompletableFuture<Void> all = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
all.get(30, MINUTES);
} finally {
Expand Down Expand Up @@ -165,6 +171,37 @@ private static CompletableFuture<Void> upload(
return CompletableFuture.allOf(uploads.toArray(new CompletableFuture<?>[0]));
}

private static CompletableFuture<Void> uploadextraArtifacts(
String repo,
Credentials credentials,
Coordinates coords,
String extraClassifiers,
String extraArtifacts
){
List<CompletableFuture<?>> uploads = new ArrayList<>();
String[] extraClassifiersArr = extraClassifiers.split(",");
String[] extraArtifactsArr = extraArtifacts.split(",");
System.out.println("Working Directory = " + System.getProperty("user.dir"));
System.out.println(Arrays.toString(extraClassifiersArr));
System.out.println(Arrays.toString(extraArtifactsArr));
for(int index=0; index<extraClassifiersArr.length; ++index) {
Path artifact = getPathIfSet(extraArtifactsArr[index]);
String fileName = artifact.toString();
String ext = fileName.substring(fileName.lastIndexOf("."));
String base = String.format(
"%s/%s/%s/%s/%s-%s",
repo.replaceAll("/$", ""),
coords.groupId.replace('.', '/'),
coords.artifactId,
coords.version,
coords.artifactId,
coords.version);

uploads.add(upload(String.format("%s-%s%s", base, extraClassifiersArr[index], ext), credentials, artifact));
}
return CompletableFuture.allOf(uploads.toArray(new CompletableFuture<?>[0]));
}

private static String toSha1(byte[] toHash) {
return toHexS("%040x", "SHA-1", toHash);
}
Expand Down

0 comments on commit 3b2b950

Please sign in to comment.