matrei commented on code in PR #15467: URL: https://github.com/apache/grails-core/pull/15467#discussion_r3214550348
########## grails-gradle/bom-property-overrides/src/main/groovy/org/grails/gradle/plugin/bom/BomManagedVersions.groovy: ########## @@ -0,0 +1,378 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.gradle.plugin.bom + +import groovy.transform.CompileStatic +import org.gradle.api.Project +import org.gradle.api.artifacts.Configuration +import org.gradle.api.artifacts.DependencyResolveDetails +import org.gradle.api.logging.Logger +import org.gradle.api.logging.Logging +import org.w3c.dom.Document +import org.w3c.dom.Element +import org.w3c.dom.NodeList + +import javax.xml.parsers.DocumentBuilderFactory + +/** + * Lightweight replacement for the Spring Dependency Management plugin's + * version property override feature. + * + * <p>Parses BOM POM files to build a mapping of Maven property names + * (e.g., {@code slf4j.version}) to the artifacts they control. At + * dependency resolution time, checks whether the user has overridden + * any of these properties via {@code ext['property.name']} in + * {@code build.gradle} or via {@code gradle.properties}, and applies + * those overrides using Gradle's {@code ResolutionStrategy.eachDependency()}.</p> + * + * <p>Gradle's native {@code platform()} mechanism handles the base + * BOM import and default version management. This class only adds the + * one feature Gradle lacks: property-based version customization + * (see <a href="https://github.com/gradle/gradle/issues/9160">Gradle #9160</a>).</p> + * + * <p>This is the underlying utility used by the + * {@code org.apache.grails.gradle.bom-property-overrides} plugin. It is + * BOM-agnostic and can be used directly with any BOM that follows the + * Maven {@code <properties>} convention for managed versions.</p> + * + * @since 8.0 + */ +@CompileStatic +class BomManagedVersions { + + private static final Logger LOG = Logging.getLogger(BomManagedVersions) + private static final int MAX_PROPERTY_INTERPOLATION_DEPTH = 10 + + private final Map<String, String> versionOverrides = new LinkedHashMap<>() + + /** + * Resolves a BOM, parses its POM chain, and determines which managed + * dependency versions need to be overridden based on project properties. + * + * @param project the Gradle project (used for artifact resolution and property lookup) + * @param bomCoordinates the BOM coordinates in {@code group:artifact:version} format + * @return a BomManagedVersions instance containing any version overrides to apply + */ + static BomManagedVersions resolve(Project project, String bomCoordinates) { + return resolve(project, [bomCoordinates]) + } + + /** + * Resolves multiple BOMs, parses their POM chains, and determines which + * managed dependency versions need to be overridden based on project + * properties. Useful when a project applies several platforms (e.g., a + * Grails BOM plus a Micronaut BOM) and any of them may declare overridable + * properties. + * + * @param project the Gradle project (used for artifact resolution and property lookup) + * @param bomCoordinatesList list of BOM coordinates in {@code group:artifact:version} format + * @return a BomManagedVersions instance containing any version overrides to apply + */ + static BomManagedVersions resolve(Project project, Collection<String> bomCoordinatesList) { + BomManagedVersions instance = new BomManagedVersions() + + Map<String, String> bomProperties = new LinkedHashMap<>() Review Comment: I question some of these statements. `@CompileStatic` should work fine in conjunction with `def` where the type can be inferred. The public contract of the method should not be influenced. This should work equally well: ```groovy static BomManagedVersions resolve(Project project, Collection<String> bomCoordinatesList) { def instance = new BomManagedVersions() def bomProperties = new LinkedHashMap<String, String>() def propertyToArtifacts = new LinkedHashMap<String, List<String>>() def processed = new HashSet<String>() for (def bomCoordinates : bomCoordinatesList) { def parts = bomCoordinates?.split(':') if (parts == null || parts.length != 3) { LOG.warn('Invalid BOM coordinates: {}', bomCoordinates) continue } processBom(project, parts[0], parts[1], parts[2], bomProperties, propertyToArtifacts, processed) } for (def entry : propertyToArtifacts.entrySet()) { def propertyName = entry.key if (project.hasProperty(propertyName)) { def overrideVersion = project.property(propertyName).toString() def defaultVersion = bomProperties.get(propertyName) if (overrideVersion != defaultVersion) { for (def artifactKey : entry.value) { instance.versionOverrides.put(artifactKey, overrideVersion) } LOG.lifecycle( 'BOM version override: {} = {} (BOM default: {})', propertyName, overrideVersion, defaultVersion ?: 'unknown' ) } } } if (!instance.versionOverrides.isEmpty()) { LOG.info( 'BOM property overrides: {} version override(s) will be applied', instance.versionOverrides.size() ) } instance } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
