maven_jar: Add support for exports and deps attributes

Expose deps and exports attribute from java_import to maven_jar rule.
This was provided in Buck's maven_jar rule. The following maven_jar
rules:

  maven_jar(
      name = "foo",
      artifact = "foo:foo:1.0",
      sha1 = "4d7e828a4651e2f590b4a059925991be58e62da6",
      deps = [
          "@bar//jar",
          "@baz//jar",
      ],
      exports = [
          "@qux//jar",
          "@quux//jar",
      ]

expands to this java_import rule:

  java_import(
      name = 'jar',
      jars = ['foo-1.0.jar'],
      srcjar = 'foo-1.0-src.jar',
      deps = [
          '@bar//jar',
          '@baz//jar',
      ],
      exports = [
          '@qux//jar',
          '@quux//jar',
      ],
  )

Change-Id: I58cc5df9820240b24fc1b6cb261966f517c4891c
This commit is contained in:
Paladox none 2017-01-28 15:16:29 +00:00
parent 49df12cb7d
commit 6e5f8e619c

View File

@ -52,6 +52,18 @@ def _create_coordinates(fully_qualified_name):
version = version, version = version,
) )
def _format_deps(attr, deps):
formatted_deps = ""
if deps:
if len(deps) == 1:
formatted_deps += "%s = [\'%s\']," % (attr, deps[0])
else:
formatted_deps += "%s = [\n" % attr
for dep in deps:
formatted_deps += " \'%s\',\n" % dep
formatted_deps += " ],"
return formatted_deps
def _generate_build_file(ctx, binjar, srcjar): def _generate_build_file(ctx, binjar, srcjar):
srcjar_attr = "" srcjar_attr = ""
if srcjar: if srcjar:
@ -61,17 +73,23 @@ def _generate_build_file(ctx, binjar, srcjar):
package(default_visibility = ['//visibility:public']) package(default_visibility = ['//visibility:public'])
java_import( java_import(
name = 'jar', name = 'jar',
{srcjar_attr}
jars = ['{binjar}'], jars = ['{binjar}'],
{srcjar_attr}
{deps}
{exports}
) )
java_import( java_import(
name = 'neverlink', name = 'neverlink',
jars = ['{binjar}'], jars = ['{binjar}'],
neverlink = 1, neverlink = 1,
{deps}
{exports}
) )
\n""".format(srcjar_attr = srcjar_attr, \n""".format(srcjar_attr = srcjar_attr,
rule_name = ctx.name, rule_name = ctx.name,
binjar = binjar) binjar = binjar,
deps = _format_deps("deps", ctx.attr.deps),
exports = _format_deps("exports", ctx.attr.exports))
if srcjar: if srcjar:
contents += """ contents += """
java_import( java_import(
@ -135,6 +153,8 @@ maven_jar = repository_rule(
"repository": attr.string(default = MAVEN_CENTRAL), "repository": attr.string(default = MAVEN_CENTRAL),
"attach_source": attr.bool(default = True), "attach_source": attr.bool(default = True),
"unsign": attr.bool(default = False), "unsign": attr.bool(default = False),
"deps": attr.string_list(),
"exports": attr.string_list(),
"exclude": attr.string_list(), "exclude": attr.string_list(),
}, },
local = True, local = True,