Skip to content

Commit 52910f4

Browse files
authored
Updated the bom validation to ensure it runs. (#1671)
* Updated the bom validation to ensure it runs. JAVA-5822 Co-authored-by: Viacheslav Babanin <[email protected]>
1 parent 9ad2e3e commit 52910f4

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

bom/build.gradle.kts

+33-32
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ dependencies {
5353
val defaultScalaVersion: String = project.findProperty("defaultScalaVersion")!!.toString()
5454
val scalaVersions: List<String>? = project.findProperty("supportedScalaVersions")?.toString()?.split(",")
5555

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

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

7578
val dependencyManagementNode = pomXml.getNode("dependencyManagement")
76-
assert(dependencyManagementNode != null) {
79+
require(dependencyManagementNode != null) {
7780
"<dependencyManagement> node not found in the generated BOM POM"
7881
}
7982
val dependenciesNode = dependencyManagementNode.getNode("dependencies")
80-
assert(dependenciesNode != null) { "<dependencies> node not found in the generated BOM POM" }
83+
require(dependenciesNode != null) { "<dependencies> node not found in the generated BOM POM" }
8184

8285
val existingScalaDeps =
83-
dependenciesNode!!
86+
dependenciesNode
8487
.children()
8588
.map { it as Node }
8689
.filter { it.getNode("artifactId")?.text()?.contains("scala") ?: false }
@@ -110,37 +113,35 @@ configureMavenPublication {
110113
* Validate the BOM file.
111114
*/
112115
tasks.withType<GenerateMavenPom> {
113-
doLast {
114-
pom.withXml {
115-
val pomXml: Node = asNode()
116-
val dependenciesNode = pomXml.getNode("dependencyManagement").getNode("dependencies")
117-
assert(dependenciesNode!!.children().isNotEmpty()) {
118-
"BOM must contain more then one <dependency> element:\n$destination"
119-
}
116+
pom.withXml {
117+
val pomXml: Node = asNode()
118+
val dependenciesNode = pomXml.getNode("dependencyManagement").getNode("dependencies")
119+
require(dependenciesNode!!.children().isNotEmpty()) {
120+
"BOM must contain more then one <dependency> element:\n$destination"
121+
}
120122

121-
dependenciesNode
122-
.children()
123-
.map { it as Node }
124-
.forEach {
125-
val groupId: String = it.getNode("groupId")!!.text()
126-
assert(groupId.startsWith("org.mongodb")) {
127-
"BOM must contain only 'org.mongodb' dependencies, but found '$groupId':\n$destination"
128-
}
123+
dependenciesNode
124+
.children()
125+
.map { it as Node }
126+
.forEach {
127+
val groupId: String = it.getNode("groupId")!!.text()
128+
require(groupId.startsWith("org.mongodb")) {
129+
"BOM must contain only 'org.mongodb' dependencies, but found '$groupId':\n$destination"
130+
}
129131

130-
/*
131-
* The <scope> and <optional> tags should be omitted in BOM dependencies.
132-
* This ensures that consuming projects have the flexibility to decide whether a dependency is optional in their context.
133-
*
134-
* The BOM's role is to provide version information, not to dictate inclusion or exclusion of dependencies.
135-
*/
136-
assert(it.getNode("scope") == null) {
137-
"BOM must not contain <scope> elements in dependency:\n$destination"
138-
}
139-
assert(it.getNode("optional") == null) {
140-
"BOM must not contain <optional> elements in dependency:\n$destination"
141-
}
132+
/*
133+
* The <scope> and <optional> tags should be omitted in BOM dependencies.
134+
* This ensures that consuming projects have the flexibility to decide whether a dependency is optional in their context.
135+
*
136+
* The BOM's role is to provide version information, not to dictate inclusion or exclusion of dependencies.
137+
*/
138+
require(it.getNode("scope") == null) {
139+
"BOM must not contain <scope> elements in dependency:\n$destination"
142140
}
143-
}
141+
require(it.getNode("optional") == null) {
142+
"BOM must not contain <optional> elements in dependency:\n$destination"
143+
}
144+
}
144145
}
145146
}
146147

0 commit comments

Comments
 (0)