Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra maven artifacts support for java_export rule #805

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ load("//private/rules:java_export.bzl", _java_export = "java_export")
load("//private/rules:javadoc.bzl", _javadoc = "javadoc")
load("//private/rules:maven_bom.bzl", _maven_bom = "maven_bom")
load("//private/rules:maven_install.bzl", _maven_install = "maven_install")
load("//private/rules:maven_publish.bzl", _MavenPublishInfo = "MavenPublishInfo")
load("//private/rules:maven_publish.bzl", _MavenPublishInfo = "MavenPublishInfo", _maven_publish="maven_publish")
load("//private/rules:pom_file.bzl", _pom_file = "pom_file")

DEFAULT_REPOSITORY_NAME = _DEFAULT_REPOSITORY_NAME
Expand All @@ -33,3 +33,4 @@ maven_install = _maven_install
pom_file = _pom_file
read_coordinates = _read_coordinates
MavenPublishInfo = _MavenPublishInfo
maven_publish = _maven_publish
20 changes: 19 additions & 1 deletion examples/java-export/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_jvm_external//:defs.bzl", "java_export", "maven_bom")
load("@rules_jvm_external//:defs.bzl", "java_export", "maven_bom", "maven_publish")

# To export the file, run:
#
Expand All @@ -13,8 +13,26 @@ java_export(
runtime_deps = [
"//src/main/java/com/github/bazelbuild/rulesjvmexternal/example/export",
],
extra_artifacts = {
"//src/main/java/com/github/bazelbuild/rulesjvmexternal/example/export:tar": "release"
}
)

# To export generic artifact like war or tar, run:
#
# `bazel run //:artifact-publish --version 1.0.0 --define "maven_repo=file://$(pwd)/repository"`
#
# GPG signing will be enabled if you also define `gpg_sign=true`, `maven_user`, and
# `maven_password`


maven_publish(
name = "artifact-publish",
artifact = "//src/main/java/com/github/bazelbuild/rulesjvmexternal/example/export:tar",
coordinates = "com.example:artifact-example:$(version)",
)


maven_bom(
name = "bom",
dependencies_maven_coordinates = "com.example:bazel-example-dependencies:0.0.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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 +13,11 @@ java_library(
artifact("com.google.guava:guava"),
],
)

pkg_tar(
name = "tar",
srcs = glob(["*"]),
visibility = [
"//:__pkg__",
],
)
8 changes: 6 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 Down Expand Up @@ -178,11 +181,12 @@ def maven_export(
coordinates = maven_coordinates,
pom = "%s-pom" % name,
javadocs = docs_jar,
artifact_jar = ":%s-maven-artifact" % name,
artifact = ":%s-maven-artifact" % name,
source_jar = ":%s-maven-source" % name,
visibility = visibility,
tags = tags,
testonly = testonly,
extra_artifacts = extra_artifacts
)

# We may want to aggregate several `java_export` targets into a single Maven BOM POM
Expand Down
35 changes: 24 additions & 11 deletions private/rules/maven_publish.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ MavenPublishInfo = provider(
"coordinates": "Maven coordinates for the project, which may be None",
"pom": "Pom.xml file for metdata",
"javadocs": "Javadoc jar file for documentation files",
"artifact_jar": "Jar with the code and metadata for execution",
"artifact": "Jar with the code and metadata or war/tar artifacts",
"source_jar": "Jar with the source code for review",
"extra_artifacts": "Extra artifacts to be published"
},
)

_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} {source_jar} {javadoc} {extra_artifacts}
"""

def _maven_publish_impl(ctx):
Expand All @@ -24,10 +25,18 @@ def _maven_publish_impl(ctx):

# Expand maven coordinates for any variables to be replaced.
coordinates = ctx.expand_make_variables("coordinates", ctx.attr.coordinates, {})
artifacts_short_path = ctx.file.artifact_jar.short_path if ctx.file.artifact_jar else "''"
pom_short_path = ctx.file.pom.short_path if ctx.file.pom else "''"
artifacts_short_path = ctx.file.artifact.short_path if ctx.file.artifact 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_artifact_files = []
extra_artifacts_classfiers = []
for target,classifier in ctx.attr.extra_artifacts.items():
file = target.files.to_list()[0]
extra_artifact_files.append(file)
extra_artifacts_classfiers.append("{}={}".format(classifier, file.short_path))

ctx.actions.write(
output = executable,
is_executable = True,
Expand All @@ -38,16 +47,19 @@ def _maven_publish_impl(ctx):
maven_repo = maven_repo,
password = password,
user = user,
pom = ctx.file.pom.short_path,
artifact_jar = artifacts_short_path,
pom = pom_short_path,
artifact = artifacts_short_path,
source_jar = source_short_path,
javadoc = javadocs_short_path,
extra_artifacts = ",".join(extra_artifacts_classfiers)
),
)

files = [ctx.file.pom]
if ctx.file.artifact_jar:
files.append(ctx.file.artifact_jar)
files = extra_artifact_files
if ctx.file.pom:
files.append(ctx.file.pom)
if ctx.file.artifact:
files.append(ctx.file.artifact)
if ctx.file.source_jar:
files.append(ctx.file.source_jar)
if ctx.file.javadocs:
Expand All @@ -64,10 +76,11 @@ def _maven_publish_impl(ctx):
),
MavenPublishInfo(
coordinates = coordinates,
artifact_jar = ctx.file.artifact_jar,
artifact = ctx.file.artifact,
javadocs = ctx.file.javadocs,
source_jar = ctx.file.source_jar,
pom = ctx.file.pom,
extra_artifacts = extra_artifact_files
),
]

Expand All @@ -92,18 +105,18 @@ When signing with GPG, the current default key is used.
mandatory = True,
),
"pom": attr.label(
mandatory = True,
allow_single_file = True,
),
"javadocs": attr.label(
allow_single_file = True,
),
"artifact_jar": attr.label(
"artifact": attr.label(
allow_single_file = True,
),
"source_jar": attr.label(
allow_single_file = True,
),
"extra_artifacts": attr.label_keyed_string_dict(),
"_uploader": attr.label(
executable = True,
cfg = "exec",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ java_binary(
visibility = ["//visibility:public"],
deps = [
"//private/tools/java/com/github/bazelbuild/rules_jvm_external",
artifact(
"com.google.guava:guava",
repository_name = "rules_jvm_external_deps",
),
artifact(
"com.google.auth:google-auth-library-oauth2-http",
repository_name = "rules_jvm_external_deps",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;

import com.google.common.base.Splitter;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;

Expand Down Expand Up @@ -85,17 +87,20 @@ public static void main(String[] args)
Coordinates coords = new Coordinates(parts.get(0), parts.get(1), parts.get(2));

// Calculate md5 and sha1 for each of the inputs
Path pom = Paths.get(args[5]);
Path pom = getPathIfSet(args[5]);
Path binJar = getPathIfSet(args[6]);
Path srcJar = getPathIfSet(args[7]);
Path docJar = getPathIfSet(args[8]);

try {
List<CompletableFuture<Void>> futures = new ArrayList<>();
futures.add(upload(repo, credentials, coords, ".pom", pom, gpgSign));
if(pom != null) {
futures.add(upload(repo, credentials, coords, ".pom", pom, gpgSign));
}

if (binJar != null) {
futures.add(upload(repo, credentials, coords, ".jar", binJar, gpgSign));
String ext = com.google.common.io.Files.getFileExtension(binJar.getFileName().toString());
futures.add(upload(repo, credentials, coords, "." + ext, binJar, gpgSign));
}

if (srcJar != null) {
Expand All @@ -106,6 +111,17 @@ public static void main(String[] args)
futures.add(upload(repo, credentials, coords, "-javadoc.jar", docJar, gpgSign));
}

if(args.length > 9) {
String ext = com.google.common.io.Files.getFileExtension(args[9]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this value need to be calculated per tuple, rather than just on the final one? eg war=foo.jar,tar=foo.tar would result in ext being set to tar for both the war and tar classifiers.

List<String> extraArtifactTuples = Splitter.onPattern(",").splitToList(args[9]);
for(String artifactTuple: extraArtifactTuples) {
String[] splits = artifactTuple.split("=");
String classifier = splits[0];
Path artifact = getPathIfSet(splits[1]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Path should exist. If it doesn't, we've got real problems.... Paths.get is fine here.

futures.add(upload(repo, credentials, coords, String.format("-%s.%s", classifier,ext), artifact, gpgSign));
}
}

CompletableFuture<Void> all =
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
all.get(30, MINUTES);
Expand Down
1 change: 1 addition & 0 deletions repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def rules_jvm_external_deps(repositories = _DEFAULT_REPOSITORIES):
maven_install(
name = "rules_jvm_external_deps",
artifacts = [
"com.google.guava:guava:31.1-jre",
"com.google.auth:google-auth-library-credentials:0.22.0",
"com.google.auth:google-auth-library-oauth2-http:0.22.0",
"com.google.cloud:google-cloud-core:1.93.10",
Expand Down
2 changes: 1 addition & 1 deletion rules_jvm_external_deps_install.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
"__INPUT_ARTIFACTS_HASH": 1692773303,
"__INPUT_ARTIFACTS_HASH": -946950608,
"__RESOLVED_ARTIFACTS_HASH": -965264991,
"artifacts": {
"com.fasterxml.jackson.core:jackson-core": {
Expand Down
Loading