2016-12-11 19:00:10 +09:00
|
|
|
GERRIT = "GERRIT:"
|
|
|
|
|
|
|
|
GERRIT_API = "GERRIT_API:"
|
|
|
|
|
|
|
|
MAVEN_CENTRAL = "MAVEN_CENTRAL:"
|
|
|
|
|
|
|
|
MAVEN_LOCAL = "MAVEN_LOCAL:"
|
2016-11-07 23:05:32 +01:00
|
|
|
|
|
|
|
def _maven_release(ctx, parts):
|
2018-07-10 11:51:36 +02:00
|
|
|
"""induce jar and url name from maven coordinates."""
|
|
|
|
if len(parts) not in [3, 4]:
|
|
|
|
fail('%s:\nexpected id="groupId:artifactId:version[:classifier]"' %
|
|
|
|
ctx.attr.artifact)
|
|
|
|
if len(parts) == 4:
|
|
|
|
group, artifact, version, classifier = parts
|
|
|
|
file_version = version + "-" + classifier
|
|
|
|
else:
|
|
|
|
group, artifact, version = parts
|
|
|
|
file_version = version
|
|
|
|
|
|
|
|
jar = artifact.lower() + "-" + file_version
|
|
|
|
url = "/".join([
|
|
|
|
ctx.attr.repository,
|
|
|
|
group.replace(".", "/"),
|
|
|
|
artifact,
|
|
|
|
version,
|
|
|
|
artifact + "-" + file_version,
|
|
|
|
])
|
|
|
|
|
|
|
|
return jar, url
|
2016-11-07 23:05:32 +01:00
|
|
|
|
|
|
|
# Creates a struct containing the different parts of an artifact's FQN
|
|
|
|
def _create_coordinates(fully_qualified_name):
|
2018-07-10 11:51:36 +02:00
|
|
|
parts = fully_qualified_name.split(":")
|
|
|
|
packaging = None
|
|
|
|
classifier = None
|
|
|
|
|
|
|
|
if len(parts) == 3:
|
|
|
|
group_id, artifact_id, version = parts
|
|
|
|
elif len(parts) == 4:
|
|
|
|
group_id, artifact_id, version, packaging = parts
|
|
|
|
elif len(parts) == 5:
|
|
|
|
group_id, artifact_id, version, packaging, classifier = parts
|
|
|
|
else:
|
|
|
|
fail("Invalid fully qualified name for artifact: %s" % fully_qualified_name)
|
|
|
|
|
|
|
|
return struct(
|
|
|
|
fully_qualified_name = fully_qualified_name,
|
|
|
|
group_id = group_id,
|
|
|
|
artifact_id = artifact_id,
|
|
|
|
packaging = packaging,
|
|
|
|
classifier = classifier,
|
|
|
|
version = version,
|
|
|
|
)
|
2016-11-07 23:05:32 +01:00
|
|
|
|
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
2017-01-28 15:16:29 +00:00
|
|
|
def _format_deps(attr, deps):
|
2018-07-10 11:51:36 +02:00
|
|
|
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
|
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
2017-01-28 15:16:29 +00:00
|
|
|
|
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] https://github.com/bazelbuild/rules_closure/commit/a47cc063d39b3958f31b4c5a992741020fd9ee3e
[2] https://github.com/bazelbuild/rules_closure/issues/200
Change-Id: I31e6b863e43adaa1f983a8a9da1675f02cb803f8
(cherry picked from commit b5099530084deb61fcb9e60314579dd0daed79b5)
2017-05-06 06:35:48 +02:00
|
|
|
def _generate_build_files(ctx, binjar, srcjar):
|
2018-07-10 11:51:36 +02:00
|
|
|
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 = """
|
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] https://github.com/bazelbuild/rules_closure/commit/a47cc063d39b3958f31b4c5a992741020fd9ee3e
[2] https://github.com/bazelbuild/rules_closure/issues/200
Change-Id: I31e6b863e43adaa1f983a8a9da1675f02cb803f8
(cherry picked from commit b5099530084deb61fcb9e60314579dd0daed79b5)
2017-05-06 06:35:48 +02:00
|
|
|
{header}
|
2016-11-29 15:06:30 +01:00
|
|
|
package(default_visibility = ['//visibility:public'])
|
2016-11-07 23:05:32 +01:00
|
|
|
java_import(
|
2016-11-29 15:06:30 +01:00
|
|
|
name = 'jar',
|
|
|
|
jars = ['{binjar}'],
|
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
2017-01-28 15:16:29 +00:00
|
|
|
{srcjar_attr}
|
|
|
|
{deps}
|
|
|
|
{exports}
|
2016-11-07 23:05:32 +01:00
|
|
|
)
|
2016-12-10 15:19:05 +01:00
|
|
|
java_import(
|
|
|
|
name = 'neverlink',
|
|
|
|
jars = ['{binjar}'],
|
|
|
|
neverlink = 1,
|
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
2017-01-28 15:16:29 +00:00
|
|
|
{deps}
|
|
|
|
{exports}
|
2016-12-10 15:19:05 +01:00
|
|
|
)
|
2018-07-10 11:51:36 +02:00
|
|
|
\n""".format(
|
|
|
|
srcjar_attr = srcjar_attr,
|
|
|
|
header = header,
|
|
|
|
binjar = binjar,
|
|
|
|
deps = _format_deps("deps", ctx.attr.deps),
|
|
|
|
exports = _format_deps("exports", ctx.attr.exports),
|
|
|
|
)
|
|
|
|
if srcjar:
|
|
|
|
contents += """
|
2016-11-29 15:06:30 +01:00
|
|
|
java_import(
|
|
|
|
name = 'src',
|
|
|
|
jars = ['{srcjar}'],
|
|
|
|
)
|
|
|
|
""".format(srcjar = srcjar)
|
2018-07-10 11:51:36 +02:00
|
|
|
ctx.file("%s/BUILD" % ctx.path("jar"), contents, False)
|
2016-11-07 23:05:32 +01:00
|
|
|
|
2018-07-10 11:51:36 +02:00
|
|
|
# Compatibility layer for java_import_external from rules_closure
|
|
|
|
contents = """
|
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] https://github.com/bazelbuild/rules_closure/commit/a47cc063d39b3958f31b4c5a992741020fd9ee3e
[2] https://github.com/bazelbuild/rules_closure/issues/200
Change-Id: I31e6b863e43adaa1f983a8a9da1675f02cb803f8
(cherry picked from commit b5099530084deb61fcb9e60314579dd0daed79b5)
2017-05-06 06:35:48 +02:00
|
|
|
{header}
|
|
|
|
package(default_visibility = ['//visibility:public'])
|
|
|
|
|
|
|
|
alias(
|
|
|
|
name = "{rule_name}",
|
|
|
|
actual = "@{rule_name}//jar",
|
|
|
|
)
|
|
|
|
\n""".format(rule_name = ctx.name, header = header)
|
2018-07-10 11:51:36 +02:00
|
|
|
ctx.file("BUILD", contents, False)
|
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] https://github.com/bazelbuild/rules_closure/commit/a47cc063d39b3958f31b4c5a992741020fd9ee3e
[2] https://github.com/bazelbuild/rules_closure/issues/200
Change-Id: I31e6b863e43adaa1f983a8a9da1675f02cb803f8
(cherry picked from commit b5099530084deb61fcb9e60314579dd0daed79b5)
2017-05-06 06:35:48 +02:00
|
|
|
|
2016-11-07 23:05:32 +01:00
|
|
|
def _maven_jar_impl(ctx):
|
2018-07-10 11:51:36 +02:00
|
|
|
"""rule to download a Maven archive."""
|
|
|
|
coordinates = _create_coordinates(ctx.attr.artifact)
|
|
|
|
|
|
|
|
name = ctx.name
|
|
|
|
sha1 = ctx.attr.sha1
|
|
|
|
|
|
|
|
parts = ctx.attr.artifact.split(":")
|
|
|
|
|
|
|
|
# TODO(davido): Only releases for now, implement handling snapshots
|
|
|
|
jar, url = _maven_release(ctx, parts)
|
|
|
|
|
|
|
|
binjar = jar + ".jar"
|
|
|
|
binjar_path = ctx.path("/".join(["jar", binjar]))
|
|
|
|
binurl = url + ".jar"
|
|
|
|
|
|
|
|
python = ctx.which("python")
|
|
|
|
script = ctx.path(ctx.attr._download_script)
|
|
|
|
|
|
|
|
args = [python, script, "-o", binjar_path, "-u", binurl]
|
|
|
|
if ctx.attr.sha1:
|
|
|
|
args.extend(["-v", sha1])
|
|
|
|
if ctx.attr.unsign:
|
|
|
|
args.append("--unsign")
|
|
|
|
for x in ctx.attr.exclude:
|
|
|
|
args.extend(["-x", x])
|
|
|
|
|
2016-11-07 23:05:32 +01:00
|
|
|
out = ctx.execute(args)
|
2016-11-29 15:06:30 +01:00
|
|
|
|
2018-07-10 11:51:36 +02:00
|
|
|
if out.return_code:
|
|
|
|
fail("failed %s: %s" % (" ".join(args), out.stderr))
|
|
|
|
|
|
|
|
srcjar = None
|
|
|
|
if ctx.attr.src_sha1 or ctx.attr.attach_source:
|
|
|
|
srcjar = jar + "-src.jar"
|
|
|
|
srcurl = url + "-sources.jar"
|
|
|
|
srcjar_path = ctx.path("jar/" + srcjar)
|
|
|
|
args = [python, script, "-o", srcjar_path, "-u", srcurl]
|
|
|
|
if ctx.attr.src_sha1:
|
|
|
|
args.extend(["-v", ctx.attr.src_sha1])
|
|
|
|
out = ctx.execute(args)
|
|
|
|
if out.return_code:
|
|
|
|
fail("failed %s: %s" % (args, out.stderr))
|
|
|
|
|
|
|
|
_generate_build_files(ctx, binjar, srcjar)
|
2016-11-07 23:05:32 +01:00
|
|
|
|
2016-12-11 19:00:10 +09:00
|
|
|
maven_jar = repository_rule(
|
|
|
|
attrs = {
|
|
|
|
"artifact": attr.string(mandatory = True),
|
2017-01-28 14:45:40 +00:00
|
|
|
"sha1": attr.string(),
|
2016-12-11 19:00:10 +09:00
|
|
|
"src_sha1": attr.string(),
|
|
|
|
"_download_script": attr.label(default = Label("//tools:download_file.py")),
|
|
|
|
"repository": attr.string(default = MAVEN_CENTRAL),
|
|
|
|
"attach_source": attr.bool(default = True),
|
|
|
|
"unsign": attr.bool(default = False),
|
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
2017-01-28 15:16:29 +00:00
|
|
|
"deps": attr.string_list(),
|
|
|
|
"exports": attr.string_list(),
|
2016-12-11 19:00:10 +09:00
|
|
|
"exclude": attr.string_list(),
|
|
|
|
},
|
|
|
|
local = True,
|
|
|
|
implementation = _maven_jar_impl,
|
|
|
|
)
|