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

Migrate from bazel-distribution to rules_jvm_external for Maven distribution #39

Merged
merged 11 commits into from
Mar 15, 2024
4 changes: 4 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Use embedded JDK
build --repo_env=JAVA_HOME=../bazel_tools/jdk
sync --experimental_google_legacy_api
build --experimental_google_legacy_api
4 changes: 2 additions & 2 deletions distribution/NexusStagingCli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class NexusStagingCli : CliktCommand() {
}

val username by option(help = "Nexus server username").defaultLazy {
System.getenv("DEPLOY_MAVEN_USERNAME")
System.getenv("MAVEN_USER")
}

val password by option(help = "Nexus server password").defaultLazy {
System.getenv("DEPLOY_MAVEN_PASSWORD")
System.getenv("MAVEN_PASSWORD")
}

val clientTimeout by option(help = "Client timeout in seconds").long()
Expand Down
6 changes: 3 additions & 3 deletions distribution/staged-maven-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ elif [ "$RELEASE_TYPE" == "release" ]; then
fi

# Find all the maven packages in the repo
readonly DEPLOY_LABELS=$(bazel query --output=label 'kind("deploy_maven rule", //...) - attr("tags", "\[.*do-not-publish.*\]", //...)')
readonly DEPLOY_LABELS=$(bazel query --output=label 'kind("maven_publish rule", //...) - attr("tags", "\[.*do-not-publish.*\]", //...)')
for pkg in $DEPLOY_LABELS ; do
if [ -n "${STAGING-}" ]; then
bazel run "$pkg" --define=DEPLOY_MAVEN_RELEASE_REPO="$STAGING" -- "$1" --gpg
bazel run "$pkg" --define=maven_repo="$STAGING" -- "$1"
else
bazel run "$pkg" -- "$1" --gpg
bazel run "$pkg" --define=maven_repo="https://oss.sonatype.org/content/repositories/snapshots/" -- "$1"
fi
done

Expand Down
70 changes: 9 additions & 61 deletions kotlin/distribution.bzl
Original file line number Diff line number Diff line change
@@ -1,67 +1,15 @@
load("@vaticle_bazel_distribution//maven:rules.bzl", "assemble_maven", "deploy_maven", "MavenDeploymentInfo")
load("@rules_jvm_external//:defs.bzl", "maven_export")
load("//internal:scope_name.bzl", "scope_name")

def distribution(
*,
name,

release_repo,
snapshot_repo,
version_file,

# (optional)
project_name = None,
project_description = None,
project_url = None,
scm_url = None,
developers = None,

workspace_refs = None,
):
assemble_name = scope_name(name, "assemble")
assemble_maven(
name = assemble_name,
target = ":%s" % name,
project_name = project_name,
project_description = project_description,
project_url = project_url,
scm_url = scm_url,
developers = developers,
version_file = version_file,
workspace_refs = workspace_refs,
maven_coordinates,
lib_name = None,
**kwargs):
maven_export(
name = scope_name(name, "export"),
lib_name = lib_name if lib_name else name,
maven_coordinates = maven_coordinates,
**kwargs
)

install_maven(
name = scope_name(name, "install"),
target = ":%s" % assemble_name,
)

deploy_maven(
name = scope_name(name, "deploy"),
target = ":%s" % assemble_name,
release = release_repo,
snapshot = snapshot_repo,
)

def _install_maven_impl(ctx):
deployment_info = ctx.attr.target[MavenDeploymentInfo]
file = deployment_info.jar

script = ctx.actions.declare_file("%s-install" % ctx.label.name)
script_content = "mvn install:install-file -Dfile={}".format(file.short_path,)
ctx.actions.write(script, script_content, is_executable = True)

runfiles = ctx.runfiles(files = [file])
return [DefaultInfo(executable = script, runfiles = runfiles)]

install_maven = rule(
implementation = _install_maven_impl,
attrs = {
"target": attr.label(
mandatory = True,
providers = [MavenDeploymentInfo],
doc = "assemble_maven target to install",
),
},
executable = True,
)
60 changes: 20 additions & 40 deletions kotlin/kt_jvm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,10 @@ def kt_jvm(

# Distribution config
group = None,
release_repo = None,
snapshot_repo = None,
version_file = None,

# (optional)
project_name = None,
project_description = None,
project_url = None,
scm_url = None,
developers = None,
workspace_refs = None,
version = None,
deploy_env = None,
excluded_workspaces = None,
pom_template = None,

# Package level config
module_name = None,
Expand All @@ -44,8 +37,7 @@ def kt_jvm(
test_resource_strip_prefix = None,
test_associates = None,
test_deps = None,
test_runtime_deps = None,
):
test_runtime_deps = None):
"""Generic Kotlin JVM macro for conditionally configuring build & test targets, linting, and publishing.

# Building + Testing
Expand All @@ -63,20 +55,11 @@ def kt_jvm(

# Publishing

Distribution requires a group to use for Maven coordinates, as well as the Maven
snapshot and release repositories to publish to. Additionally, a version file is
required to read the version from for publishing.
Distribution requires a group and version to use for Maven coordinates.

If any of these properties are provided, publishing targets will be attempted to be
If either of these properties are provided, publishing targets will be attempted to be
created, but will error out if any of the other required properties are missing.

The following can be provided for additional information to publish the artifact
with:
- project_name
- project_description
- project_url
- scm_url

Three targets are created for publishing:
- {name}-assemble: Package artifact and generate POM
- {name}-deploy: Executable target for actually deploying to a Maven repo
Expand All @@ -90,9 +73,9 @@ def kt_jvm(

# Distribution project config
group: (optional) group identifier for publishing
release_repo: (optional) Maven release repository
snapshot_repo: (optional) Maven snapshot repository
version_file: (optional) file containing version string
version: (optional) version to publish under
deploy_env: (optional) collection of targets to exclude from transitive closure
excluded_workspaces: (optional) dict of workspace names to replace, or remove, from transitive closure

# Distribution target config
project_name: (optional) project name for POM
Expand Down Expand Up @@ -136,15 +119,17 @@ def kt_jvm(
if test_resources == None:
test_resources = native.glob(["src/test/resources/**/*"])

should_publish = group or version_file or snapshot_repo or release_repo
required_info_to_publish = group and version_file and snapshot_repo and release_repo
should_publish = group or version
required_info_to_publish = group and version

if should_publish and not required_info_to_publish:
fail("publishing info not fully provided. to enable publishing, ensure group, version_file, snapshot_repo, and release_repo are provided: %s, %s, %s, %s" % (group, version_file, snapshot_repo, release_repo))
fail("publishing info not fully provided. to enable publishing, ensure group and version are provided: %s, %s" % (group, version))

maven_coordinates = "%s:%s:%s" % (group, name, version if version else "{pom_version}") if should_publish else None

kt_jvm_library_and_test(
name = name,
tags = ["maven_coordinates=%s:%s:{pom_version}" % (group, name)] if should_publish else None,
tags = ["maven_coordinates=%s" % (maven_coordinates)] if maven_coordinates else None,
module_name = module_name,
main_opts = main_opts,
main_srcs = main_srcs,
Expand Down Expand Up @@ -176,13 +161,8 @@ def kt_jvm(
if should_publish:
distribution(
name = name,
release_repo = release_repo,
snapshot_repo = snapshot_repo,
version_file = version_file,
project_name = project_name,
project_description = project_description,
project_url = project_url,
scm_url = scm_url,
developers = developers,
workspace_refs = workspace_refs,
maven_coordinates = maven_coordinates,
deploy_env = deploy_env,
excluded_workspaces = excluded_workspaces,
pom_template = pom_template,
)
16 changes: 15 additions & 1 deletion maven/workspace.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("//internal:maybe.bzl", "maybe")

def maven(tag = "3.3", sha = "d85951a92c0908c80bd8551002d66cb23c3434409c814179c0ff026b53544dab"):
def maven(tag = "5.3", sha = "d31e369b854322ca5098ea12c69d7175ded971435e55c18dd9dd5f29cc5249ac"):
maybe(
http_archive,
name = "rules_jvm_external",
strip_prefix = "rules_jvm_external-%s" % tag,
sha256 = sha,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % tag,
)

# Load maven rules from git
def maven_repository(
remote = "https://github.com/sugarmanz/rules_jvm_external.git",
Copy link
Contributor

Choose a reason for hiding this comment

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

plan is to try to get your PR in rules_jvm_external merged, yea?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ideally, but IMO it's not really acceptable as-is. It requires an --experimental_google_legacy_api flag, which would require every consumer of the rules to enable that flag. I'm not sure what the path forward is, but will likely try to get some feedback from the team over there.

Copy link
Member Author

Choose a reason for hiding this comment

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

Opened a PR for feedback:
bazel-contrib/rules_jvm_external#1049

branch = "maven-export-aar",
**kwargs):
maybe(
git_repository,
name = "rules_jvm_external",
remote = remote,
branch = branch,
**kwargs
)
6 changes: 3 additions & 3 deletions workspace.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("//cocoapods:workspace.bzl", "cocoapods")
load("//distribution:workspace.bzl", "distribution")
load("//junit5:workspace.bzl", "junit5")
load("//kotlin:workspace.bzl", "kotlin")
load("//maven:workspace.bzl", "maven")
load("//maven:workspace.bzl", "maven_repository")
load("//javascript:workspace.bzl", "javascript")

def deps(
Expand All @@ -18,6 +18,6 @@ def deps(
distribution()
junit5()
kotlin()
maven()
maven_repository()
javascript()
cocoapods()
cocoapods()