Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for pom generation #1208

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
15 changes: 12 additions & 3 deletions private/rules/maven_bom.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _maven_bom_impl(ctx):
bom = generate_pom(
ctx,
coordinates = coordinates,
versioned_dep_coordinates = [f[MavenBomFragmentInfo].coordinates for f in ctx.attr.fragments],
versioned_dep_coordinates = [unpack_coordinates(f[MavenBomFragmentInfo].coordinates) for f in ctx.attr.fragments],
pom_template = ctx.file.pom_template,
out_name = "%s.xml" % ctx.label.name,
)
Expand Down Expand Up @@ -77,13 +77,22 @@ def _maven_dependencies_bom_impl(ctx):
# included in the main BOM
first_order_deps = [f[MavenBomFragmentInfo].coordinates for f in ctx.attr.fragments]
all_deps = depset(transitive = [f.maven_info.maven_deps for f in fragments]).to_list()
combined_deps = [a for a in all_deps if a not in first_order_deps]
combined_deps = [unpack_coordinates(a) for a in all_deps if a not in first_order_deps]

unpacked = unpack_coordinates(ctx.attr.bom_coordinates)
unpacked = struct(
groupId = unpacked.groupId,
artifactId = unpacked.artifactId,
type = "pom",
scope = "import",
classifier = unpacked.classifier,
version = unpacked.version,
)

dependencies_bom = generate_pom(
ctx,
coordinates = ctx.attr.maven_coordinates,
versioned_dep_coordinates = combined_deps + ["%s:%s:pom:import:%s" % (unpacked.groupId, unpacked.artifactId, unpacked.version)],
versioned_dep_coordinates = combined_deps + [unpacked],
pom_template = ctx.file.pom_template,
out_name = "%s.xml" % ctx.label.name,
indent = 12,
Expand Down
37 changes: 27 additions & 10 deletions private/rules/maven_utils.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
def unpack_coordinates(coords):
"""Takes a maven coordinate and unpacks it into a struct with fields
`groupId`, `artifactId`, `version`, `type`, `scope`
`groupId`, `artifactId`, `version`, `type`, `classifier`
vinnybod marked this conversation as resolved.
Show resolved Hide resolved
where type and scope are optional.

Assumes following maven coordinate syntax:
groupId:artifactId[:type[:scope]]:version
groupId:artifactId[:type[:classifier]]:version
"""
if not coords:
return None
Expand All @@ -18,6 +18,7 @@ def unpack_coordinates(coords):
artifactId = parts[1],
type = None,
scope = None,
classifier = None,
version = None,
)

Expand All @@ -30,7 +31,8 @@ def unpack_coordinates(coords):
groupId = parts.get(0),
artifactId = parts.get(1),
type = parts.get(2),
scope = parts.get(3),
scope = None,
classifier = parts.get(3),
version = version,
)

Expand Down Expand Up @@ -70,6 +72,12 @@ def format_dep(unpacked, indent = 8, include_version = True):
" <scope>%s</scope>\n" % unpacked.scope,
])

if unpacked.classifier:
dependency.extend([
whitespace,
" <classifier>%s</classifier>\n" % unpacked.classifier,
])

dependency.extend([
whitespace,
"</dependency>",
Expand All @@ -93,6 +101,7 @@ def generate_pom(
"{artifactId}": unpacked_coordinates.artifactId,
"{version}": unpacked_coordinates.version,
"{type}": unpacked_coordinates.type or "jar",
"{classifier}": unpacked_coordinates.classifier or "",
"{scope}": unpacked_coordinates.scope or "compile",
}

Expand All @@ -112,16 +121,16 @@ def generate_pom(
substitutions.update({"{parent}": "".join(parts)})

deps = []
for dep in sorted(versioned_dep_coordinates) + sorted(unversioned_dep_coordinates):
for dep in _sort_unpacked(versioned_dep_coordinates) + _sort_unpacked(unversioned_dep_coordinates):
include_version = dep in versioned_dep_coordinates
unpacked = unpack_coordinates(dep)
new_scope = "runtime" if dep in runtime_deps else unpacked.scope
new_scope = "runtime" if dep in runtime_deps else dep.scope
unpacked = struct(
groupId = unpacked.groupId,
artifactId = unpacked.artifactId,
type = unpacked.type,
groupId = dep.groupId,
artifactId = dep.artifactId,
type = dep.type,
scope = new_scope,
version = unpacked.version,
classifier = dep.classifier,
version = dep.version,
)
deps.append(format_dep(unpacked, indent = indent, include_version = include_version))

Expand Down Expand Up @@ -156,3 +165,11 @@ def determine_additional_dependencies(jar_files, additional_dependencies):
to_return.append(dep)

return to_return

def _sort_unpacked(unpacked_dep):
"""Sorts a list of unpacked dependencies by groupId, artifactId, and version."""

def _sort_key(dep):
return (dep.groupId, dep.artifactId, dep.version)

return sorted(unpacked_dep, key = _sort_key)
8 changes: 4 additions & 4 deletions private/rules/pom_file.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_java//java:defs.bzl", "JavaInfo")
load(":has_maven_deps.bzl", "MavenInfo", "calculate_artifact_jars", "has_maven_deps")
load(":maven_utils.bzl", "determine_additional_dependencies", "generate_pom")
load(":maven_utils.bzl", "determine_additional_dependencies", "generate_pom", "unpack_coordinates")
vinnybod marked this conversation as resolved.
Show resolved Hide resolved

def _pom_file_impl(ctx):
# Ensure the target has coordinates
Expand All @@ -20,11 +20,11 @@ def _pom_file_impl(ctx):
all_maven_deps.append(coords)

expanded_maven_deps = [
ctx.expand_make_variables("additional_deps", coords, ctx.var)
unpack_coordinates(ctx.expand_make_variables("additional_deps", coords, ctx.var))
for coords in all_maven_deps
]
expanded_runtime_deps = [
ctx.expand_make_variables("maven_runtime_deps", coords, ctx.var)
unpack_coordinates(ctx.expand_make_variables("maven_runtime_deps", coords, ctx.var))
for coords in runtime_maven_deps
]

Expand All @@ -34,7 +34,7 @@ def _pom_file_impl(ctx):
out = generate_pom(
ctx,
coordinates = coordinates,
versioned_dep_coordinates = sorted(expanded_maven_deps),
versioned_dep_coordinates = expanded_maven_deps,
runtime_deps = expanded_runtime_deps,
pom_template = ctx.file.pom_template,
out_name = "%s.xml" % ctx.label.name,
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/maven_bom/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ java_export(
name = "one-dep",
srcs = ["OneDep.java"],
maven_coordinates = "com.example:one-dep:1.0.0",
visibility = [
":__pkg__",
"//tests/integration/pom_file:__pkg__",
],
deps = [
artifact("com.google.guava:guava"),
],
Expand Down
67 changes: 67 additions & 0 deletions tests/integration/pom_file/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("//:defs.bzl", "artifact", "java_export", "maven_bom")

java_export(
name = "pom-example",
srcs = ["PomExample.java"],
maven_coordinates = "com.example:app:1.0.0",
deps = [
"//tests/integration/maven_bom:one-dep",
],
)

diff_test(
name = "validate-pom",
file1 = ":pom-example-pom",
file2 = "pom.golden.xml",
)

java_export(
name = "pom-example-with-runtime-dep",
srcs = ["PomExample.java"],
maven_coordinates = "com.example:app:1.0.0",
runtime_deps = [
"//tests/integration/maven_bom:one-dep",
],
deps = [
"//tests/integration/maven_bom:one-dep",
artifact("com.google.guava:guava"),
],
)

diff_test(
name = "validate-pom-with-runtime-dep",
file1 = ":pom-example-with-runtime-dep-pom",
file2 = "pom-with-runtime-dep.golden.xml",
)

java_export(
name = "pom-example-with-classifier-dep",
srcs = ["PomExample.java"],
maven_coordinates = "com.example:app:1.0.0",
runtime_deps = [
":classifier-artifact-one",
],
deps = [
":classifier-artifact-one",
":classifier-artifact-two",
],
)

java_export(
name = "classifier-artifact-one",
srcs = ["ClassifierArtifactOne.java"],
maven_coordinates = "com.example:lib:jar:linux-x86:1.0.0",
)

java_export(
name = "classifier-artifact-two",
srcs = ["ClassifierArtifactTwo.java"],
maven_coordinates = "com.example:lib:jar:linux-arm64:1.0.0",
)

diff_test(
name = "validate-pom-example-with-classifier-dep",
file1 = ":pom-example-with-classifier-dep-pom",
file2 = "pom-with-classifier-dep.golden.xml",
)
5 changes: 5 additions & 0 deletions tests/integration/pom_file/ClassifierArtifactOne.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tests.integration.pom_file;

public class ClassifierArtifactOne {
// This space left blank intentionally
}
5 changes: 5 additions & 0 deletions tests/integration/pom_file/ClassifierArtifactTwo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tests.integration.pom_file;

public class ClassifierArtifactTwo {
// This space left blank intentionally
}
9 changes: 9 additions & 0 deletions tests/integration/pom_file/PomExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tests.integration.pom_file;

public class PomExample {

public static void main(String[] args) {
System.out.println("Hello World!");
}

}
25 changes: 25 additions & 0 deletions tests/integration/pom_file/pom-with-classifier-dep.golden.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>app</artifactId>
<version>1.0.0</version>

<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>lib</artifactId>
<version>1.0.0</version>
<scope>runtime</scope>
<classifier>linux-x86</classifier>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>lib</artifactId>
<version>1.0.0</version>
<classifier>linux-arm64</classifier>
</dependency>
</dependencies>
</project>
23 changes: 23 additions & 0 deletions tests/integration/pom_file/pom-with-runtime-dep.golden.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>app</artifactId>
<version>1.0.0</version>

<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>one-dep</artifactId>
<version>1.0.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
</dependencies>
</project>
17 changes: 17 additions & 0 deletions tests/integration/pom_file/pom.golden.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>app</artifactId>
<version>1.0.0</version>

<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>one-dep</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>