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

Setting version of java_export() jar via command line flag #618

Open
caseyduquettesc opened this issue Nov 16, 2021 · 1 comment
Open

Setting version of java_export() jar via command line flag #618

caseyduquettesc opened this issue Nov 16, 2021 · 1 comment

Comments

@caseyduquettesc
Copy link
Contributor

Is there a way to set the version number of the published jar on the command line? If I use an environment variable in the maven_coordinates, it almost works, but it's not substituted in the pom template. I can create my own pom template and do the string replacement via --define= but then I need to set the version in two places. Once by exporting the variable for its use in maven_coordinates and again in a --define=maven_version=0.0.0. Is there a better way to set the version once on the command line and get it applied to the maven_coordinates and the pom file? I've seen a lot of projects using a jenkins build number as part of the published jar version.

java_export(
    name = "export",
    maven_coordinates = "group:pkg:$MAVEN_VERSION",
)
@hisener
Copy link
Contributor

hisener commented Jun 8, 2022

I have a similar use case, where we use the commit sha as the version. I raised the same question on Bazel Slack, and I have a working solution with @shs96c's help. Just posting it as a reference for future readers.

I ended up creating a repository rule and expose some constants. Mostly influenced by https://stackoverflow.com/a/65401963/3528626.

def _maven_configuration_impl(repository_ctx):
    _BUILD_FILE = """
# DO NOT EDIT: automatically generated for _maven_configuration rule
filegroup(
    name = 'files',
    srcs = glob(['**']),
    visibility = ['//visibility:public']
)
"""
    repository_ctx.file("BUILD", _BUILD_FILE, False)

    branch = repository_ctx.os.environ.get("CI_COMMIT_BRANCH")
    repo = "..."

    version = repository_ctx.os.environ.get("CI_COMMIT_SHORT_SHA", "LOCAL")
    if branch != "main":
        version = version + "-SNAPSHOT"

    _MAVEN_CONSTANTS_FILE = """
# DO NOT EDIT: automatically generated for _maven_configuration rule
MAVEN_REPO = \"{repo}\"
MAVEN_VERSION = \"{version}\"
"""

    repository_ctx.file(
        "constants.bzl",
        _MAVEN_CONSTANTS_FILE.format(repo = repo, version = version),
        False
    )

_maven_configuration = repository_rule(
    implementation = _maven_configuration_impl,
    environ = [
        "CI_COMMIT_BRANCH",
        "CI_COMMIT_SHORT_SHA",
    ],
)

def maven_configuration():
    _maven_configuration(name = "maven_configuration")

Loaded the rule in WORKSPACE.

load("//rules/java:maven_configuration.bzl", "maven_configuration")
maven_configuration()

And used the VERSION constant in the BUILD file of the library:

load("@maven_configuration//:constants.bzl", "MAVEN_VERSION")
…
maven_coordinates = "com.datadog.lib:lib:%s" % MAVEN_VERSION,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants