001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.util.graph.manager;
020
021import java.util.ArrayList;
022import java.util.Collection;
023
024import org.eclipse.aether.collection.DependencyManager;
025import org.eclipse.aether.graph.Exclusion;
026import org.eclipse.aether.scope.ScopeManager;
027import org.eclipse.aether.scope.SystemDependencyScope;
028
029/**
030 * A dependency manager that provides proper transitive dependency management for modern Maven usage.
031 *
032 * <h2>Overview</h2>
033 * <p>
034 * This manager implements proper "transitive dependency management" that works harmoniously
035 * with Maven's ModelBuilder. It produces more precise results regarding versions by respecting
036 * transitive management rules while allowing higher-level management to override lower-level rules.
037 * </p>
038 *
039 * <h2>Key Characteristics</h2>
040 * <ul>
041 * <li><strong>Transitive Management:</strong> {@code deriveUntil=Integer.MAX_VALUE}, {@code applyFrom=2}</li>
042 * <li><strong>ModelBuilder Friendly:</strong> Works in conjunction with, not against, ModelBuilder</li>
043 * <li><strong>Inheritance Aware:</strong> Special handling for scope and optional properties</li>
044 * <li><strong>Precise Versioning:</strong> Obeys transitive management unless managed at higher levels</li>
045 * </ul>
046 *
047 * <h2>Inheritance Handling</h2>
048 * <p>
049 * This manager provides special care for "scope" and "optional" properties that are subject
050 * to inheritance in the dependency graph during later graph transformation steps. These
051 * properties are only derived from the root to prevent interference with inheritance logic.
052 * </p>
053 *
054 * <h2>When to Use</h2>
055 * <p>
056 * This is the <strong>recommended manager for modern Maven projects</strong> that need proper
057 * transitive dependency management while maintaining compatibility with Maven's ModelBuilder.
058 * </p>
059 *
060 * <h2>Comparison with Other Managers</h2>
061 * <ul>
062 * <li>{@link ClassicDependencyManager}: Maven 2.x compatibility, limited transitive support</li>
063 * <li>{@link DefaultDependencyManager}: Aggressive but interferes with ModelBuilder</li>
064 * <li><strong>This manager:</strong> Modern, transitive, ModelBuilder-compatible (recommended)</li>
065 * </ul>
066 *
067 * @author Christian Schulte
068 * @since 1.4.0
069 * @see ClassicDependencyManager
070 * @see DefaultDependencyManager
071 */
072public final class TransitiveDependencyManager extends AbstractDependencyManager {
073    /**
074     * Creates a new dependency manager without any management information.
075     */
076    public TransitiveDependencyManager() {
077        this(null);
078    }
079
080    /**
081     * Creates a new transitive dependency manager with ModelBuilder-compatible behavior.
082     * <p>
083     * This constructor initializes the manager with settings optimized for modern Maven usage:
084     * <ul>
085     * <li>deriveUntil = Integer.MAX_VALUE (collect management rules at all levels)</li>
086     * <li>applyFrom = 2 (apply management starting from depth 2, respecting ModelBuilder)</li>
087     * <li>Special inheritance handling for scope and optional properties</li>
088     * </ul>
089     *
090     * @param scopeManager application-specific scope manager for handling system dependencies,
091     *                     may be null to use legacy system dependency scope handling
092     */
093    public TransitiveDependencyManager(ScopeManager scopeManager) {
094        super(Integer.MAX_VALUE, 2, scopeManager);
095    }
096
097    @SuppressWarnings("checkstyle:ParameterNumber")
098    private TransitiveDependencyManager(
099            ArrayList<AbstractDependencyManager> path,
100            int depth,
101            int deriveUntil,
102            int applyFrom,
103            MMap<Key, String> managedVersions,
104            MMap<Key, String> managedScopes,
105            MMap<Key, Boolean> managedOptionals,
106            MMap<Key, String> managedLocalPaths,
107            MMap<Key, Holder<Collection<Exclusion>>> managedExclusions,
108            SystemDependencyScope systemDependencyScope) {
109        super(
110                path,
111                depth,
112                deriveUntil,
113                applyFrom,
114                managedVersions,
115                managedScopes,
116                managedOptionals,
117                managedLocalPaths,
118                managedExclusions,
119                systemDependencyScope);
120    }
121
122    @Override
123    protected DependencyManager newInstance(
124            MMap<Key, String> managedVersions,
125            MMap<Key, String> managedScopes,
126            MMap<Key, Boolean> managedOptionals,
127            MMap<Key, String> managedLocalPaths,
128            MMap<Key, Holder<Collection<Exclusion>>> managedExclusions) {
129        ArrayList<AbstractDependencyManager> path = new ArrayList<>(this.path);
130        path.add(this);
131        return new TransitiveDependencyManager(
132                path,
133                depth + 1,
134                deriveUntil,
135                applyFrom,
136                managedVersions,
137                managedScopes,
138                managedOptionals,
139                managedLocalPaths,
140                managedExclusions,
141                systemDependencyScope);
142    }
143
144    /**
145     * Controls inheritance-based property derivation for scope and optional properties.
146     * <p>
147     * <strong>Why scope and optional are special:</strong> In dependency graphs, these two properties
148     * are subject to inheritance during graph transformation (which is outside ModelBuilder's scope).
149     * Therefore, scope and optional are derived only from the root to prevent interference with
150     * inheritance logic.
151     * </p>
152     * <p>
153     * <strong>The inheritance problem:</strong> If we managed scope/optional from sources below the root,
154     * we would mark nodes as "managed" in the dependency graph. The "managed" flag means "do not touch it,
155     * it is as it should be", which would prevent proper inheritance application during later graph
156     * transformation, causing nodes to end up with incorrect scope or optional states.
157     * </p>
158     * <p>
159     * <strong>Special case:</strong> The "system" scope has special handling due to its unique path requirements.
160     * </p>
161     *
162     * @return true only at depth 0 (root level) to ensure inheritance-based properties are only
163     *         derived from the root, false otherwise
164     */
165    @Override
166    protected boolean isInheritedDerived() {
167        return depth == 0;
168    }
169}