jamesfredley commented on code in PR #15467:
URL: https://github.com/apache/grails-core/pull/15467#discussion_r3342068215


##########
grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/bom/BomManagedVersions.groovy:
##########
@@ -0,0 +1,503 @@
+/*
+ *  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 java.util.function.Function
+
+import groovy.transform.CompileStatic
+import org.gradle.api.Project
+import org.gradle.api.artifacts.ConfigurationContainer
+import org.gradle.api.artifacts.DependencyConstraint
+import org.gradle.api.artifacts.MutableVersionConstraint
+import org.gradle.api.artifacts.dsl.DependencyHandler
+import org.gradle.api.logging.Logger
+import org.gradle.api.logging.Logging
+import org.w3c.dom.Document
+import org.w3c.dom.Element
+
+import javax.xml.parsers.DocumentBuilderFactory
+
+/**
+ * Lightweight replacement for the Spring Dependency Management plugin's
+ * version property override feature.
+ *
+ * <p>Parses BOM POM files to determine the version every managed artifact
+ * resolves to, both with the BOM's default {@code <properties>} values and
+ * with the project's overrides applied (via {@code ext['property.name']} in
+ * {@code build.gradle} or via {@code gradle.properties}). Any artifact whose
+ * effective version differs from the BOM default becomes a version 
override.</p>
+ *
+ * <p>Overrides are applied as <strong>strict</strong> dependency constraints
+ * (see {@link #applyTo(DependencyHandler, String)}). A strict constraint wins
+ * over the {@code require} constraints contributed by Gradle's native
+ * {@code platform()} mechanism, so an override is honored even when it
+ * <em>downgrades</em> a managed version - a plain
+ * {@code ResolutionStrategy.eachDependency()} / {@code useVersion()} hook
+ * would lose to the platform's higher version during conflict resolution.</p>
+ *
+ * <p>Because the effective version is computed by re-resolving imported
+ * ({@code <scope>import</scope>}) BOMs with the project's property overrides
+ * applied, overriding a property that selects an imported BOM's version
+ * (for example {@code spring-boot.version}) re-imports that BOM and pulls in
+ * its updated managed-dependency set.</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
+ * (registered in {@code grails-gradle-plugins}). 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
+
+    /** A property resolver that never overrides anything (BOM defaults only). 
*/
+    private static final Function<String, String> NO_OVERRIDES = { String name 
-> null } as Function<String, String>
+
+    private final Map<String, String> versionOverrides = new LinkedHashMap<>()
+
+    /**
+     * Resolves a single BOM via captured Gradle services rather than a
+     * {@link Project} reference. Preferred for config-cache discipline:
+     * callers capture services once (typically inside a single
+     * {@code afterEvaluate} block) and never leak a {@link Project}
+     * reference into the override map that lives on past configuration time.
+     *
+     * @param configurations the project's configuration container, captured 
at apply/afterEvaluate time
+     * @param dependencies the project's dependency handler, captured at 
apply/afterEvaluate time
+     * @param propertyLookup function returning the project's property value 
as a String, or {@code null} if unset
+     * @param bomCoordinates the BOM coordinates in {@code 
group:artifact:version} format
+     * @return a BomManagedVersions instance containing any version overrides 
to apply
+     */
+    static BomManagedVersions resolve(ConfigurationContainer configurations,
+                                      DependencyHandler dependencies,
+                                      Function<String, String> propertyLookup,
+                                      String bomCoordinates) {
+        return resolve(configurations, dependencies, propertyLookup, 
[bomCoordinates])
+    }
+
+    /**
+     * Resolves multiple BOMs via captured Gradle services. The result is a
+     * plain data carrier (a {@code Map<String, String>} of version overrides
+     * inside {@link BomManagedVersions}) that holds no {@link Project}
+     * reference, so it can be safely captured by per-configuration
+     * constraint declarations and survive configuration-cache serialization.
+     *
+     * <p>The override set is computed as the difference between two 
resolutions
+     * of the BOM tree: one using the BOM's default property values, and one
+     * using the project's property overrides. Any managed artifact whose
+     * effective version differs from its default version is recorded as an
+     * override.</p>
+     *
+     * @param configurations the project's configuration container, captured 
at apply/afterEvaluate time
+     * @param dependencies the project's dependency handler, captured at 
apply/afterEvaluate time
+     * @param propertyLookup function returning the project's property value 
as a String, or {@code null} if unset
+     * @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(ConfigurationContainer configurations,
+                                      DependencyHandler dependencies,
+                                      Function<String, String> propertyLookup,
+                                      Collection<String> bomCoordinatesList) {
+        def instance = new BomManagedVersions()
+
+        Map<String, String> defaultVersions = computeManagedVersions(

Review Comment:
   @{comment=3339264251; thread=PRRT_kwDOAAfRJ86GVyNl; body=Done in c2ac4a6948 
- both `defaultVersions` and `effectiveVersions` now use `def`.}.body



-- 
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]

Reply via email to