Skip to content

Updated the bom validation to ensure it runs. #1671

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

Merged
merged 3 commits into from
Apr 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 33 additions & 32 deletions bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ dependencies {
val defaultScalaVersion: String = project.findProperty("defaultScalaVersion")!!.toString()
val scalaVersions: List<String>? = project.findProperty("supportedScalaVersions")?.toString()?.split(",")

assert(!scalaVersions.isNullOrEmpty()) {
require(!scalaVersions.isNullOrEmpty()) {
"Scala versions must be provided as a comma-separated list in the 'supportedScalaVersions' project property"
}

scalaVersions?.forEach { version ->
require(version.matches(Regex("\\d\\.\\d{2}"))) { "Scala version '$version' must be in the format X.YY" }
}
/*
* Apply the Java Platform plugin to create the BOM
* Modify the generated POM to include all supported versions of Scala for driver-scala or bson-scala.
Expand All @@ -73,14 +76,14 @@ configureMavenPublication {
val pomXml: Node = asNode()

val dependencyManagementNode = pomXml.getNode("dependencyManagement")
assert(dependencyManagementNode != null) {
require(dependencyManagementNode != null) {
"<dependencyManagement> node not found in the generated BOM POM"
}
val dependenciesNode = dependencyManagementNode.getNode("dependencies")
assert(dependenciesNode != null) { "<dependencies> node not found in the generated BOM POM" }
require(dependenciesNode != null) { "<dependencies> node not found in the generated BOM POM" }

val existingScalaDeps =
dependenciesNode!!
dependenciesNode
.children()
.map { it as Node }
.filter { it.getNode("artifactId")?.text()?.contains("scala") ?: false }
Expand Down Expand Up @@ -110,37 +113,35 @@ configureMavenPublication {
* Validate the BOM file.
*/
tasks.withType<GenerateMavenPom> {
doLast {
pom.withXml {
val pomXml: Node = asNode()
val dependenciesNode = pomXml.getNode("dependencyManagement").getNode("dependencies")
assert(dependenciesNode!!.children().isNotEmpty()) {
"BOM must contain more then one <dependency> element:\n$destination"
}
pom.withXml {
val pomXml: Node = asNode()
val dependenciesNode = pomXml.getNode("dependencyManagement").getNode("dependencies")
require(dependenciesNode!!.children().isNotEmpty()) {
"BOM must contain more then one <dependency> element:\n$destination"
}

dependenciesNode
.children()
.map { it as Node }
.forEach {
val groupId: String = it.getNode("groupId")!!.text()
assert(groupId.startsWith("org.mongodb")) {
"BOM must contain only 'org.mongodb' dependencies, but found '$groupId':\n$destination"
}
dependenciesNode
.children()
.map { it as Node }
.forEach {
val groupId: String = it.getNode("groupId")!!.text()
require(groupId.startsWith("org.mongodb")) {
"BOM must contain only 'org.mongodb' dependencies, but found '$groupId':\n$destination"
}

/*
* The <scope> and <optional> tags should be omitted in BOM dependencies.
* This ensures that consuming projects have the flexibility to decide whether a dependency is optional in their context.
*
* The BOM's role is to provide version information, not to dictate inclusion or exclusion of dependencies.
*/
assert(it.getNode("scope") == null) {
"BOM must not contain <scope> elements in dependency:\n$destination"
}
assert(it.getNode("optional") == null) {
"BOM must not contain <optional> elements in dependency:\n$destination"
}
/*
* The <scope> and <optional> tags should be omitted in BOM dependencies.
* This ensures that consuming projects have the flexibility to decide whether a dependency is optional in their context.
*
* The BOM's role is to provide version information, not to dictate inclusion or exclusion of dependencies.
*/
require(it.getNode("scope") == null) {
"BOM must not contain <scope> elements in dependency:\n$destination"
}
}
require(it.getNode("optional") == null) {
"BOM must not contain <optional> elements in dependency:\n$destination"
}
}
}
}

Expand Down