diff --git a/common/pomgenmode.py b/common/pomgenmode.py index f22774f..6cb4ea2 100644 --- a/common/pomgenmode.py +++ b/common/pomgenmode.py @@ -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): @@ -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 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 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("pom") == -1, TEMPLATE) +TEMPLATE.bazel_produced_artifact = types.MethodType( + lambda self, pom_template_content: pom_template_content.find("pom") == -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 diff --git a/crawl/bazel.py b/crawl/bazel.py index 8e2aaef..dd1f18e 100644 --- a/crawl/bazel.py +++ b/crawl/bazel.py @@ -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: @@ -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: diff --git a/crawl/pom.py b/crawl/pom.py index afde392..d52b62f 100644 --- a/crawl/pom.py +++ b/crawl/pom.py @@ -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): @@ -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: diff --git a/examples/skip-artifact-generation/parent/parent1/BUILD b/examples/skip-artifact-generation/parent/parent1/BUILD index c729b06..544de8f 100644 --- a/examples/skip-artifact-generation/parent/parent1/BUILD +++ b/examples/skip-artifact-generation/parent/parent1/BUILD @@ -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"], ) diff --git a/examples/skip-artifact-generation/parent/parent1/src/main/java/com/pomgen/example/lib1/Main.java b/examples/skip-artifact-generation/parent/parent1/src/main/java/com/pomgen/example/lib1/Main.java index 75ce0c9..5877f7f 100644 --- a/examples/skip-artifact-generation/parent/parent1/src/main/java/com/pomgen/example/lib1/Main.java +++ b/examples/skip-artifact-generation/parent/parent1/src/main/java/com/pomgen/example/lib1/Main.java @@ -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)); } } diff --git a/examples/skip-artifact-generation/parent/parent2/BUILD b/examples/skip-artifact-generation/parent/parent2/BUILD index 67f40a3..b820f67 100644 --- a/examples/skip-artifact-generation/parent/parent2/BUILD +++ b/examples/skip-artifact-generation/parent/parent2/BUILD @@ -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"], ) diff --git a/examples/skip-artifact-generation/passthrough/BUILD b/examples/skip-artifact-generation/passthrough/BUILD index 36da5eb..bd08410 100644 --- a/examples/skip-artifact-generation/passthrough/BUILD +++ b/examples/skip-artifact-generation/passthrough/BUILD @@ -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",], ) diff --git a/pomgen.py b/pomgen.py index b370b41..3411795 100755 --- a/pomgen.py +++ b/pomgen.py @@ -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: diff --git a/tests/pomtest.py b/tests/pomtest.py index 136eae8..4f50624 100644 --- a/tests/pomtest.py +++ b/tests/pomtest.py @@ -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) @@ -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) @@ -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) @@ -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)