Skip to content

Commit

Permalink
Support for "exports" attr in java_library rule (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
simontoens authored Jun 15, 2024
1 parent 8d4cb6e commit e01e2fd
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 47 deletions.
47 changes: 28 additions & 19 deletions common/pomgenmode.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,32 @@ class PomGenMode:
"""
The pom generation mode, as specified by the `pom_generation_mode` attribute
of the `maven_artifact` rule.
"""
def __init__(self, name, produces_artifact):
"""
The following attributes are available:
name:
The name of this pom_generation_mode
produces_artifact:
Whether this pomgen mode produces a maven artifact
bazel_produced_artifact:
Whether this pomgen mode represents an artifact that is built by
Bazel (ie a jar)
additional_dependency_attrs:
The rule attributes that point to other dependencies to process.
For all pomgen modes, these attributes are "deps" and "runtime_deps".
Additional attributes may be added using this parameter
"""
def __init__(self, name, produces_artifact, additional_dependency_attrs=()):
"""
"""
self.name = name
self.produces_artifact = produces_artifact
self.dependency_attributes =\
("deps", "runtime_deps") + tuple(additional_dependency_attrs)

def bazel_produced_artifact(self, pom_template_content):
"""
Whether this pomgen mode represents an artifact that is built by
Bazel (ie a jar)
Default is True.
"""
raise Exception("Method must be implemented")

def __str__(self):
Expand All @@ -52,27 +58,30 @@ def __str__(self):


# the pom is generated from scratch, using a common skeleton base template
# "interesting" pom content is only the <dependencies> section, which is based
# on BUILD file content
DYNAMIC = PomGenMode("dynamic",
produces_artifact=True,)
DYNAMIC.bazel_produced_artifact = types.MethodType(lambda self, pom_template_content: True, DYNAMIC)
# dynamic pom content is only the <dependencies> section, which is based on
# BUILD file content
DYNAMIC = PomGenMode("dynamic", produces_artifact=True,)
DYNAMIC.bazel_produced_artifact = types.MethodType(
lambda self, pom_template_content: True, DYNAMIC)


# the pom is generated based on a custom template file only
TEMPLATE = PomGenMode("template",
produces_artifact=True,)
TEMPLATE = PomGenMode("template", produces_artifact=True,)
# this is an edge case - for custom pom templates, the packaging tends to be
# "pom" - that's the whole point. but we have a couple of cases with different
# values (such as maven-plugin), in which case bazel is expected to build
# something also
TEMPLATE.bazel_produced_artifact = types.MethodType(lambda self, pom_template_content: pom_template_content.find("<packaging>pom</packaging>") == -1, TEMPLATE)
TEMPLATE.bazel_produced_artifact = types.MethodType(
lambda self, pom_template_content: pom_template_content.find("<packaging>pom</packaging>") == -1, TEMPLATE)


# this bazel package is skipped over at pom generation time
# dependencies from this bazel package are "pushed up" to the closest parent
# that has an artifact producing pom_generation_mode
SKIP = PomGenMode("skip",
produces_artifact=False,)
SKIP.bazel_produced_artifact = types.MethodType(lambda self, pom_template_content: False, SKIP)
SKIP = PomGenMode("skip", produces_artifact=False,
additional_dependency_attrs=("exports",))
SKIP.bazel_produced_artifact = types.MethodType(
lambda self, pom_template_content: False, SKIP)


DEFAULT = DYNAMIC
Expand Down
10 changes: 5 additions & 5 deletions crawl/bazel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import json


def query_java_library_deps_attributes(repository_root_path, target_pattern, verbose=False):
def query_java_library_deps_attributes(repository_root_path, target_pattern,
dep_attributes, verbose=False):
"""
Returns, as a list of strings, the combined values of the 'deps' and
'runtime_deps' attributes on the java_library rule identified by the
specified target_pattern.
Returns, as a list of strings, the combined values of the dep_attributes
given, typically 'deps' and 'runtime_deps', of the (java_library) rule
identified by the specified target_pattern.
Example return value:
Expand All @@ -46,7 +47,6 @@ def query_java_library_deps_attributes(repository_root_path, target_pattern, ver
if target_pattern.endswith("..."):
raise Exception("target_pattern must be more specific")

dep_attributes = ("deps", "runtime_deps")
query_parts = ["labels(%s, %s)" % (attr, target_pattern) for attr in dep_attributes]
query = "bazel query --noimplicit_deps --order_output full '%s'" % " union ".join(query_parts)
if verbose:
Expand Down
8 changes: 6 additions & 2 deletions crawl/pom.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def _handle_description(self, content, description):

class NoopPomGen(AbstractPomGen):
"""
A placeholder pom generator that doesn't generator anything, but still
A placeholder pom generator that doesn't generate anything, but still
follows references.
"""
def __init__(self, workspace, artifact_def, dependency):
Expand Down Expand Up @@ -723,8 +723,12 @@ def _query_dependencies(workspace, artifact_def, dependency):
try:
label = _build_bazel_label(artifact_def.bazel_package,
dependency.bazel_target)

# the rule attributes to query for dependencies
dep_attrs=artifact_def.pom_generation_mode.dependency_attributes
dep_labels = bazel.query_java_library_deps_attributes(
workspace.repo_root_path, label, workspace.verbose)
workspace.repo_root_path, label, dep_attrs,
workspace.verbose)
deps = workspace.parse_dep_labels(dep_labels)
return workspace.normalize_deps(artifact_def, deps)
except Exception as e:
Expand Down
7 changes: 4 additions & 3 deletions examples/skip-artifact-generation/parent/parent1/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ java_library(
name = "parent1",
srcs = glob(["src/main/java/**/*.java"]),
# these deps get pulled into the pom generated for *this* package
# because the reference below has pomgen mode == skip
deps = ["//examples/skip-artifact-generation/passthrough:set1"],
runtime_deps = ["@maven//:org_antlr_stringtemplate"],
)

java_binary(
name = "Main",
srcs = glob(["src/**/*.java"]),
deps = [":parent1"],
name = "main",
main_class = "com.pomgen.example.lib1.Main",
runtime_deps = [":parent1"],
)

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.pomgen.example.lib1;

import com.google.common.base.Preconditions;
import java.lang.reflect.Method;

public class Main {
public static void main(String[] args) throws Exception {
System.out.println("Hello Guava at-runtime-only: " + Class.forName("com.google.common.base.Preconditions"));
Preconditions.checkNotNull(args);
System.out.println("Greetings from lib2: " + Class.forName("com.pomgen.example.lib2.Lib2API").getMethod("getGreeting").invoke(null));
}
}
1 change: 1 addition & 0 deletions examples/skip-artifact-generation/parent/parent2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ java_library(
name = "parent2",
srcs = glob(["src/main/java/**/*.java"]),
# these deps get pulled into the pom generated for *this* package
# because the reference below has pomgen mode == skip
deps = ["//examples/skip-artifact-generation/passthrough:set1"],
runtime_deps = ["@maven//:org_antlr_ST4"],
)
Expand Down
16 changes: 4 additions & 12 deletions examples/skip-artifact-generation/passthrough/BUILD
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
java_library(
name = "set1",
runtime_deps = [
"@maven//:com_google_guava_guava",
":set2",
],
runtime_deps = [":set2",],
exports = ["@maven//:com_google_guava_guava",],
visibility = ["//visibility:public"],
)

java_library(
name = "set2",
runtime_deps = [
"//examples/skip-artifact-generation/lib",
"@maven//:com_google_guava_guava",
":set3",
],
runtime_deps = [":set3", "//examples/skip-artifact-generation/lib",],
)

java_library(
name = "set3",
runtime_deps = [
"@maven//:org_apache_commons_commons_lang3",
],
runtime_deps = ["@maven//:org_apache_commons_commons_lang3",],
)
3 changes: 2 additions & 1 deletion pomgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def main(args):
mvn_install_info,
pom_content,
dependencymd,
override_file_info.label_to_overridden_fq_label)
override_file_info.label_to_overridden_fq_label,
verbose=args.verbose)
packages = argsupport.get_all_packages(repo_root, args.package)
packages = ws.filter_artifact_producing_packages(packages)
if len(packages) == 0:
Expand Down
8 changes: 4 additions & 4 deletions tests/pomtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_dynamic_pom__sanity(self):

org_function = bazel.query_java_library_deps_attributes
try:
bazel.query_java_library_deps_attributes = lambda r, p, v: ("@maven//:com_google_guava_guava", "@maven//:aopalliance_aopalliance", "@maven//:ch_qos_logback_logback_classic", "@maven//:gt2_t2" )
bazel.query_java_library_deps_attributes = lambda r, p, a, v: ("@maven//:com_google_guava_guava", "@maven//:aopalliance_aopalliance", "@maven//:ch_qos_logback_logback_classic", "@maven//:gt2_t2" )
_, _, deps = pomgen.process_dependencies()
deps = list(deps)
# appending a dependency that is built in the shared-repo (should not have an exclusions block)
Expand Down Expand Up @@ -258,7 +258,7 @@ def test_dyamic_pom__no_explicit_transitives(self):
pomgen = pom.DynamicPomGen(ws, artifact_def, dep, TEST_POM_TEMPLATE)
org_function = bazel.query_java_library_deps_attributes
try:
bazel.query_java_library_deps_attributes = lambda r, p, v: ("@maven//:com_google_guava_guava", )
bazel.query_java_library_deps_attributes = lambda r, p, a, v: ("@maven//:com_google_guava_guava", )
_, _, deps = pomgen.process_dependencies()
pomgen.register_dependencies(deps)

Expand Down Expand Up @@ -322,7 +322,7 @@ def test_dynamic_pom__do_not_include_deps(self):

org_function = bazel.query_java_library_deps_attributes
try:
bazel.query_java_library_deps_attributes = lambda r, p, v: 1/0 # fails
bazel.query_java_library_deps_attributes = lambda r, p, a, v: 1/0 # fails
pomgen.process_dependencies()
generated_pom = pomgen.gen(pom.PomContentType.RELEASE)

Expand All @@ -349,7 +349,7 @@ def test_dynamic_pom_genmode__goldfile(self):

org_function = bazel.query_java_library_deps_attributes
try:
bazel.query_java_library_deps_attributes = lambda r, p, v: ("@maven//:com_google_guava_guava", "@maven//:aopalliance_aopalliance", )
bazel.query_java_library_deps_attributes = lambda r, p, a, v: ("@maven//:com_google_guava_guava", "@maven//:aopalliance_aopalliance", )
_, _, deps = pomgen.process_dependencies()
pomgen.register_dependencies(deps)

Expand Down

0 comments on commit e01e2fd

Please sign in to comment.