Skip to content

Commit

Permalink
[bazel] Allow classifiers in maven coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Mar 12, 2020
1 parent 620d0fb commit 70e74bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
11 changes: 7 additions & 4 deletions java/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,22 @@ def combine_jars(ctx, singlejar, inputs, output):

def explode_coordinates(coords):
"""Takes a maven coordinate and explodes it into a tuple of
(groupId, artifactId, version, type)
(groupId, artifactId, version, type, classifier)
"""
if not coords:
return None

parts = coords.split(":")
if len(parts) == 3:
return (parts[0], parts[1], parts[2], "jar")
return (parts[0], parts[1], parts[2], "jar", "jar")
if len(parts) == 4:
# Assume a buildr coordinate: groupId:artifactId:type:version
return (parts[0], parts[1], parts[3], parts[2])
return (parts[0], parts[1], parts[3], parts[2], "jar")
if len(parts) == 5:
# Assume groupId:artifactId:type:classifier:version
return (parts[0], parts[1], parts[4], parts[2], parts[3])

fail("Unparsed: %s" % coords)
fail("Unparsed: %s -> %s" % (coords, parts))

def read_coordinates(tags):
coordinates = []
Expand Down
13 changes: 12 additions & 1 deletion java/private/pom.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ _TYPED_DEP = """ <dependency>
<type>{3}</type>
</dependency>"""

_CLASSIFIER_DEP = """ <dependency>
<groupId>{0}</groupId>
<artifactId>{1}</artifactId>
<version>{2}</version>
<classifier>{4}</classifier>
</dependency>"""

def _pom_file_impl(ctx):
# Ensure the target has coordinates
if not ctx.attr.target[MavenInfo].coordinates:
Expand All @@ -34,15 +41,19 @@ def _pom_file_impl(ctx):
"{artifactId}": coordinates[1],
"{version}": coordinates[2],
"{type}": coordinates[3],
"{classifier}": coordinates[4],
}

deps = []
for dep in sorted(info.maven_deps.to_list()):
exploded = explode_coordinates(dep)
if (exploded[3] == "jar"):
if (exploded[4] != "jar"):
template = _CLASSIFIER_DEP
elif (exploded[3] == "jar"):
template = _PLAIN_DEP
else:
template = _TYPED_DEP

deps.append(template.format(*exploded))
substitutions.update({"{dependencies}": "\n".join(deps)})

Expand Down

0 comments on commit 70e74bb

Please sign in to comment.