maven_jar compatibility to rules_closure#java_import_external

This is a preparation change for bumping rules_closure to the HEAD, that
we need to make transpilation from ES6 to ES5 actually work.

Since this commit: [1] rules_closure moved from using maven_jar to home
grown java_import_external rule. However, this introduced subtle
incompatibility issue: [2], because java_import_external mounts external
dependency under the root directory and not under the @foo/jar directory,
like native and our own custom maven_jar rules are doing.

This leads to the incompatibility, of how the external dependency are
consumed, with the consequence, that artifacts are not found by the
rule_closure rules, that were fetched with our own maven_jar rule.

To rectify, extend our maven_jar rule and generate additional BUILD
file in the root folder of the external dependency directory and alias
it to the real one generated in the @foo/jar directory.

For example, this BUILD file is now generated in root directory:

  alias(
      name = "javax_inject",
      actual = "@javax_inject//jar",
  )

This allows us to omit additional fetching of already fetched artifacts
that are needed by rules_closures rules and let them reference to the
downloaded artifacts in gerrit build.

There is one complication though: java_import_external accepts deps
attribute, in which case the artifacts with dependencies are not
compatible with our own maven_jar custom rule, that doesn't support
transitive dependencies. That means that we can only reuse such
artifacts from the rules_closure build that don't have dependencies.

Another problem, why we can't reuse even such artifacts that don't have
dependencies specified, is different naming convention. gerrit uses
simple names, like jsr305, but rules_closure is using canonical artifact
names, e.g.: com_google_code_findbugs_jsr305, so that we get:

* external/jsr305/jar/jsr305-3.0.1.jar # gerrit
* external/com_google_code_findbugs_jsr305/jsr305-2.0.3.jar # closure

Effectively, right now this change let us only reuse 3 common
dependencies:

* aopalliance
* javax_inject
* args4j

Because the same name, we must solve the collision problem here. Another
considered alternatives to this change would be:

1. Ask rules_closure project to migrate to consuming their dependencies
from @foo/jar directory, so that it is compatible with native and custom
maven_jar rules
2. Consume common dependencies in gerrit with rule_closure's own
java_import_external rule

[1] a47cc063d3
[2] https://github.com/bazelbuild/rules_closure/issues/200

Change-Id: I31e6b863e43adaa1f983a8a9da1675f02cb803f8
(cherry picked from commit b509953008)
This commit is contained in:
David Ostrovsky 2017-05-06 06:35:48 +02:00 committed by David Pursehouse
parent 3c0899307e
commit 1cbeda4978
1 changed files with 20 additions and 7 deletions

View File

@ -64,12 +64,13 @@ def _format_deps(attr, deps):
formatted_deps += " ],"
return formatted_deps
def _generate_build_file(ctx, binjar, srcjar):
def _generate_build_files(ctx, binjar, srcjar):
header = "# DO NOT EDIT: automatically generated BUILD file for maven_jar rule %s" % ctx.name
srcjar_attr = ""
if srcjar:
srcjar_attr = 'srcjar = "%s",' % srcjar
contents = """
# DO NOT EDIT: automatically generated BUILD file for maven_jar rule {rule_name}
{header}
package(default_visibility = ['//visibility:public'])
java_import(
name = 'jar',
@ -86,10 +87,10 @@ java_import(
{exports}
)
\n""".format(srcjar_attr = srcjar_attr,
rule_name = ctx.name,
binjar = binjar,
deps = _format_deps("deps", ctx.attr.deps),
exports = _format_deps("exports", ctx.attr.exports))
header = header,
binjar = binjar,
deps = _format_deps("deps", ctx.attr.deps),
exports = _format_deps("exports", ctx.attr.exports))
if srcjar:
contents += """
java_import(
@ -99,6 +100,18 @@ java_import(
""".format(srcjar = srcjar)
ctx.file('%s/BUILD' % ctx.path("jar"), contents, False)
# Compatibility layer for java_import_external from rules_closure
contents = """
{header}
package(default_visibility = ['//visibility:public'])
alias(
name = "{rule_name}",
actual = "@{rule_name}//jar",
)
\n""".format(rule_name = ctx.name, header = header)
ctx.file("BUILD", contents, False)
def _maven_jar_impl(ctx):
"""rule to download a Maven archive."""
coordinates = _create_coordinates(ctx.attr.artifact)
@ -142,7 +155,7 @@ def _maven_jar_impl(ctx):
if out.return_code:
fail("failed %s: %s" % (args, out.stderr))
_generate_build_file(ctx, binjar, srcjar)
_generate_build_files(ctx, binjar, srcjar)
maven_jar = repository_rule(
attrs = {