@@ -21,8 +21,8 @@ package org.apache.grails.gradle.tasks.bom
2121
2222import java.util.regex.Pattern
2323
24- import io.spring.gradle.dependencymanagement. org.apache.maven.model.Model
25- import io.spring.gradle.dependencymanagement. org.apache.maven.model.io.xpp3.MavenXpp3Reader
24+ import org.apache.maven.model.Model
25+ import org.apache.maven.model.io.xpp3.MavenXpp3Reader
2626import org.gradle.api.DefaultTask
2727import org.gradle.api.GradleException
2828import org.gradle.api.NamedDomainObjectProvider
@@ -257,17 +257,19 @@ abstract class ExtractDependenciesTask extends DefaultTask {
257257 }
258258
259259 Properties populatePlatformDependencies (CoordinateVersionHolder bomCoordinates , List<CoordinateHolder > exclusionRules , Map<CoordinateHolder , ExtractedDependencyConstraint > constraints , boolean error = true , int level = 0 ) {
260- Dependency bomDependency = dependencyHandler. create(" ${ bomCoordinates.coordinates} @pom" )
261- Configuration dependencyConfiguration = configurationContainer. detachedConfiguration(bomDependency)
260+ def bomDependency = dependencyHandler. create(" ${ bomCoordinates.coordinates} @pom" )
261+ def dependencyConfiguration = configurationContainer. detachedConfiguration(bomDependency). tap {
262+ transitive = false
263+ }
262264 File bomPomFile = dependencyConfiguration. singleFile
263265
264- MavenXpp3Reader reader = new MavenXpp3Reader ()
265- Model model = reader. read(new FileReader (bomPomFile))
266+ // Parse the BOM POM with Maven's own model library so resolution mirrors upstream Maven.
267+ Model model = bomPomFile. withInputStream { InputStream input -> new MavenXpp3Reader (). read(input) }
268+ def versionProperties = new Properties ()
266269
267- Properties versionProperties = new Properties ()
270+ // Parent POM populated first so its properties can be overridden by the child
268271 if (model. parent) {
269- // Need to populate the parent bom if it's present first
270- CoordinateVersionHolder parentBom = new CoordinateVersionHolder (
272+ def parentBom = new CoordinateVersionHolder (
271273 groupId : model. parent. groupId,
272274 artifactId : model. parent. artifactId,
273275 version : model. parent. version
@@ -276,69 +278,74 @@ abstract class ExtractDependenciesTask extends DefaultTask {
276278 versionProperties. put(entry. key, entry. value)
277279 }
278280 }
281+
279282 model. properties. entrySet(). each { Map.Entry <Object , Object > entry ->
280283 versionProperties. put(entry. key, entry. value)
281284 }
282285 versionProperties. put(' project.groupId' , bomCoordinates. groupId)
283286 versionProperties. put(' project.version' , bomCoordinates. version)
284287
285- if (model. dependencyManagement && model. dependencyManagement. dependencies) {
286- for (io.spring.gradle.dependencymanagement.org.apache.maven.model.Dependency depItem : model. dependencyManagement. dependencies) {
287- CoordinateHolder baseCoordinates = new CoordinateHolder (
288- groupId : depItem. groupId,
289- artifactId : depItem. artifactId
290- )
291-
292- CoordinateHolder resolvedCoordinates = new CoordinateHolder (
293- groupId : resolveMavenProperty(baseCoordinates. coordinatesWithoutVersion, depItem. groupId, versionProperties),
294- artifactId : resolveMavenProperty(baseCoordinates. coordinatesWithoutVersion, depItem. artifactId, versionProperties)
295- )
296-
297- if (! constraints. containsKey(resolvedCoordinates)) {
298- boolean isExcluded = exclusionRules. any { CoordinateHolder excludedCoordinate ->
299- if (excludedCoordinate. groupId && excludedCoordinate. artifactId) {
300- return resolvedCoordinates == excludedCoordinate
301- }
302-
303- if (excludedCoordinate. groupId && ! excludedCoordinate. artifactId) {
304- return depItem. groupId == excludedCoordinate. groupId
305- }
306-
307- if (! excludedCoordinate. groupId && excludedCoordinate. artifactId) {
308- return depItem. artifactId == excludedCoordinate. artifactId
309- }
310-
311- false
312- }
313-
314- if (! isExcluded) {
315- String resolvedVersion = resolveMavenProperty(resolvedCoordinates. coordinatesWithoutVersion, depItem. version, versionProperties)
316- String propertyName = depItem. version. contains(' $' ) ? depItem. version : null
317- ExtractedDependencyConstraint constraint = new ExtractedDependencyConstraint (
318- groupId : resolvedCoordinates. groupId, artifactId : resolvedCoordinates. artifactId,
319- version : resolvedVersion, versionPropertyReference : propertyName, source : bomCoordinates. artifactId
320- )
321- if (depItem. scope == ' import' ) {
322- constraints. put(resolvedCoordinates, constraint)
323-
324- CoordinateVersionHolder resolvedBomCoordinates = new CoordinateVersionHolder (
325- groupId : resolvedCoordinates. groupId,
326- artifactId : resolvedCoordinates. artifactId,
327- version : resolvedVersion
328- )
329- populatePlatformDependencies(resolvedBomCoordinates, exclusionRules, constraints, error, level + 1 )
330- } else {
331- constraints. put(resolvedCoordinates, constraint)
332- }
333- }
334- }
335- }
336- } else {
288+ def managedDependencies = model. dependencyManagement?. dependencies ?: []
289+ if (managedDependencies. isEmpty()) {
337290 if (error) {
338291 // only the boms we directly include need to error since we expect a dependency management;
339292 // parent boms are sometimes use to share properties so we need to not error on these cases
340293 throw new GradleException (" BOM ${ bomCoordinates.coordinates} has no dependencyManagement section." )
341294 }
295+ return versionProperties
296+ }
297+
298+ for (def depItem : managedDependencies) {
299+ def baseCoordinates = new CoordinateHolder (
300+ groupId : depItem. groupId,
301+ artifactId : depItem. artifactId
302+ )
303+
304+ def resolvedCoordinates = new CoordinateHolder (
305+ groupId : resolveMavenProperty(baseCoordinates. coordinatesWithoutVersion, depItem. groupId, versionProperties),
306+ artifactId : resolveMavenProperty(baseCoordinates. coordinatesWithoutVersion, depItem. artifactId, versionProperties)
307+ )
308+
309+ if (constraints. containsKey(resolvedCoordinates)) {
310+ continue
311+ }
312+
313+ boolean isExcluded = exclusionRules. any { CoordinateHolder excludedCoordinate ->
314+ if (excludedCoordinate. groupId && excludedCoordinate. artifactId) {
315+ return resolvedCoordinates == excludedCoordinate
316+ }
317+
318+ if (excludedCoordinate. groupId && ! excludedCoordinate. artifactId) {
319+ return depItem. groupId == excludedCoordinate. groupId
320+ }
321+
322+ if (! excludedCoordinate. groupId && excludedCoordinate. artifactId) {
323+ return depItem. artifactId == excludedCoordinate. artifactId
324+ }
325+
326+ false
327+ }
328+
329+ if (isExcluded) {
330+ continue
331+ }
332+
333+ def resolvedVersion = resolveMavenProperty(resolvedCoordinates. coordinatesWithoutVersion, depItem. version, versionProperties)
334+ def propertyName = depItem. version?. contains(' $' ) ? depItem. version : null
335+ def constraint = new ExtractedDependencyConstraint (
336+ groupId : resolvedCoordinates. groupId, artifactId : resolvedCoordinates. artifactId,
337+ version : resolvedVersion, versionPropertyReference : propertyName, source : bomCoordinates. artifactId
338+ )
339+ constraints. put(resolvedCoordinates, constraint)
340+
341+ if (depItem. scope == ' import' ) {
342+ def resolvedBomCoordinates = new CoordinateVersionHolder (
343+ groupId : resolvedCoordinates. groupId,
344+ artifactId : resolvedCoordinates. artifactId,
345+ version : resolvedVersion
346+ )
347+ populatePlatformDependencies(resolvedBomCoordinates, exclusionRules, constraints, error, level + 1 )
348+ }
342349 }
343350
344351 versionProperties
0 commit comments