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 Jan 4, 2023
1 parent a33194a commit 5d26d0f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 12 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.gz",
visibility = [
"//:__pkg__",
],
)
12 changes: 10 additions & 2 deletions 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 All @@ -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
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
4 changes: 1 addition & 3 deletions private/rules/maven_bom_fragment.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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
3 changes: 1 addition & 2 deletions private/rules/pom_file.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 36 additions & 2 deletions private/tools/java/rules/jvm/external/maven/MavenPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompletableFuture<Void>> futures = new ArrayList<>();
Expand All @@ -106,8 +108,12 @@ public static void main(String[] args)
futures.add(upload(repo, credentials, coords, "-javadoc.jar", docJar, gpgSign));
}

CompletableFuture<Void> 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<Void> all = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
all.get(30, MINUTES);
} finally {
EXECUTOR.shutdown();
Expand Down Expand Up @@ -170,6 +176,34 @@ 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(",");
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 5d26d0f

Please sign in to comment.