Skip to content

Commit

Permalink
[bazel] Generate pom.xml files for maven rules
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Aug 3, 2019
1 parent 979ec6b commit 000b82d
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 25 deletions.
8 changes: 4 additions & 4 deletions java/client/src/org/openqa/selenium/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
load("//java:version.bzl", "SE_VERSION")
load("//java:maven.bzl", "java_export")

filegroup(
name = "template-pom",
srcs = ["pom.xml"],
visibility = ["//visibility:public"],
)

java_library(
java_export(
name = "core",
srcs = glob([
"*.java",
Expand All @@ -16,9 +17,8 @@ java_library(
"logging/**/*.java",
"mobile/*.java",
]),
tags = [
"maven_coordinates=org.seleniumhq.selenium:selenium-api:%s" % SE_VERSION,
],
maven_coordinates = "org.seleniumhq.selenium:selenium-api:%s" % SE_VERSION,
pom_template = ":template-pom",
visibility = ["//visibility:public"],
deps = [
# Nothing from third party
Expand Down
8 changes: 4 additions & 4 deletions java/client/src/org/openqa/selenium/firefox/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
load("//java:maven.bzl", "java_export")
load("//java:version.bzl", "SE_VERSION")
load("//:copy_file.bzl", "copy_file")

java_library(
java_export(
name = "firefox",
srcs = glob(["*.java"]),
resources = [":prefs"],
tags = [
"maven_coordinates=org.seleniumhq.selenium:selenium-firefox-driver:%s" % SE_VERSION,
],
maven_coordinates = "org.seleniumhq.selenium:selenium-firefox-driver:%s" % SE_VERSION,
pom_template = "//java/client/src/org/openqa/selenium:template-pom",
visibility = ["//visibility:public"],
deps = [
"//java/client/src/org/openqa/selenium/json",
Expand Down
8 changes: 4 additions & 4 deletions java/client/src/org/openqa/selenium/firefox/xpi/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
load("//java:maven.bzl", "java_export")
load("//java:version.bzl", "SE_VERSION")
load("//:copy_file.bzl", "copy_file")

java_library(
java_export(
name = "xpi",
srcs = glob(["*.java"]),
resources = [
":amd64",
":i386",
":webdriver_xpi",
],
tags = [
"maven_coordinates=org.seleniumhq.selenium:selenium-firefox-xpi-driver:%s" % SE_VERSION,
],
maven_coordinates = "org.seleniumhq.selenium:selenium-firefox-xpi-driver:%s" % SE_VERSION,
pom_template = "//java/client/src/org/openqa/selenium:template-pom",
visibility = ["//visibility:public"],
deps = [
"//java/client/src/org/openqa/selenium:core",
Expand Down
24 changes: 16 additions & 8 deletions java/client/src/org/openqa/selenium/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>empty</artifactId>
<version>unknown</version>

<groupId>{groupId}</groupId>
<artifactId>{artifactId}</artifactId>
<version>{version}</version>
<description>Selenium automates browsers. That's it! What you do with that power is entirely up to you.</description>
<url>http://www.seleniumhq.org/</url>

<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<scm>
<url>https://github.com/SeleniumHQ/selenium/</url>
<connection>scm:git:https://github.com/SeleniumHQ/selenium.git</connection>
<developerConnection>scm:git:git@github.com:SeleniumHQ/selenium.git</developerConnection>
</scm>

<developers>
<developer>
<id>simon.m.stewart</id>
Expand Down Expand Up @@ -86,9 +95,8 @@
</roles>
</developer>
</developers>
<scm>
<connection>scm:git:git@github.com:SeleniumHQ/selenium.git</connection>
<developerConnection>scm:git:git@github.com:SeleniumHQ/selenium.git</developerConnection>
<url>https://github.com/SeleniumHQ/selenium/</url>
</scm>

<dependencies>
{dependencies}
</dependencies>
</project>
8 changes: 4 additions & 4 deletions java/client/src/org/openqa/selenium/remote/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//java:version.bzl", "SE_VERSION")
load("//java:maven.bzl", "java_export")

java_library(
name = "api",
Expand Down Expand Up @@ -43,7 +44,7 @@ java_library(
srcs = ["HttpSessionId.java"],
)

java_library(
java_export(
name = "remote",
srcs = [
"AddRotatable.java",
Expand All @@ -57,9 +58,8 @@ java_library(
"html5/AddWebStorage.java",
"mobile/AddNetworkConnection.java",
],
tags = [
"maven_coordinates=org.seleniumhq.selenium:selenium-remote-driver:%s" % SE_VERSION,
],
maven_coordinates = "org.seleniumhq.selenium:selenium-remote-driver:%s" % SE_VERSION,
pom_template = "//java/client/src/org/openqa/selenium:template-pom",
visibility = ["//visibility:public"],
exports = [
":api",
Expand Down
137 changes: 136 additions & 1 deletion java/maven.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,22 @@ def _has_maven_deps_impl(target, ctx):
all_infos = [dep[MavenInfo] for dep in all_deps if MavenInfo in dep]

maven_deps = depset([], transitive = [info.transitive_maven_deps for info in all_infos])
transitive_maven_deps = depset(coordinates, transitive = [info.transitive_maven_deps for info in all_infos])

# Because of the way that maven works, if a rule has maven coordinates,
# it's enough to set set the transitive deps to just be the rule for
# anything that depends upon it. Otherwise, gather them up, and carry on
# as if nothing really mattered.

if len(coordinates) > 0:
transitive_maven_deps = depset(coordinates)
else:
transitive_maven_deps = depset(coordinates, transitive = [info.transitive_maven_deps for info in all_infos])
artifact_jars = depset(java_info.runtime_output_jars, transitive = [info.artifact_jars for info in all_infos if not info.coordinates])
source_jars = depset(java_info.source_jars, transitive = [info.source_jars for info in all_infos if not info.coordinates])

infos = []
coordinate = coordinates[0] if len(coordinates) > 0 else None

info = MavenInfo(
coordinates = coordinate,
maven_deps = maven_deps,
Expand Down Expand Up @@ -119,3 +129,128 @@ maven_jars = rule(
allow_files = True),
},
)

_PLAIN_DEP = """ <dependency>
<groupId>{0}</groupId>
<artifactId>{1}</artifactId>
<version>{2}</version>
</dependency>"""

_TYPED_DEP = """ <dependency>
<groupId>{0}</groupId>
<artifactId>{1}</artifactId>
<version>{2}</version>
<type>{3}</type>
</dependency>"""


def explode_coordinates(coords):
"""Takes a maven coordinate and explodes it into a tuple of
(groupId, artifactId, version, type)
"""
parts = coords.split(":")
if len(parts) == 3:
return (parts[0], parts[1], parts[2], "jar")
if len(parts) == 4:
# Assume a buildr coordinate: groupId:artifactId:type:version
return (parts[0], parts[1], parts[3], parts[2])

fail("Unparsed: %s" % coords)


def _pom_file_impl(ctx):
# Ensure the target has coordinates
if not ctx.attr.target[MavenInfo].coordinates:
fail("pom_file target must have maven coordinates.")

info = ctx.attr.target[MavenInfo]

# Separate out the various parts of the

coordinates = explode_coordinates(info.coordinates)
substitutions = {
"{groupId}": coordinates[0],
"{artifactId}": coordinates[1],
"{version}": coordinates[2],
"{type}": coordinates[3],
}

deps = []
for dep in sorted(info.maven_deps.to_list()):
exploded = explode_coordinates(dep)
if (exploded[3] == "jar"):
template = _PLAIN_DEP
else:
template = _TYPED_DEP
deps.append(template.format(*exploded))
substitutions.update({"{dependencies}": "\n".join(deps)})

out = ctx.actions.declare_file("%s.xml" % ctx.attr.name)
ctx.actions.expand_template(
template = ctx.file.template,
output = out,
substitutions = substitutions,
)

return [
DefaultInfo(files = depset([out]))
]

pom_file = rule(
_pom_file_impl,
attrs = {
"target": attr.label(
aspects = [_has_maven_deps],
providers = [JavaInfo, MavenInfo]),
"template": attr.label(
allow_single_file = True,
),
"_zip": attr.label(
executable = True,
cfg = "host",
default = "@bazel_tools//tools/zip:zipper",
allow_files = True ),
},
)

def java_export(
name,
maven_coordinates=None,
pom_template=None,
tags=[],
srcs=None,
deps=None,
exports=None,
resources=None,
runtime_deps=None,
neverlink=False,
visibility=None):

actual_tags = tags
if maven_coordinates:
actual_tags = tags + ["%s%s" % (_PREFIX, maven_coordinates)]
if not pom_template:
fail("java_export requires pom_template to be set if coordinates given: %s" % maven_coordinates)

native.java_library(
name = name,
srcs = srcs,
resources = resources,
deps = deps,
exports = exports,
runtime_deps = runtime_deps,
tags = actual_tags,
visibility = visibility)

pom_file(
name = "%s-pom" % name,
target = ":%s" % name,
template = pom_template,
visibility = visibility,
)

maven_jars(
name = "%s-maven" % name,
targets = [":%s" % name],
visibility = visibility,
)

0 comments on commit 000b82d

Please sign in to comment.