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.Collection;
022
023import org.eclipse.aether.collection.DependencyCollectionContext;
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 mimics the way Maven 2.x works for backward compatibility.
031 *
032 * <h2>Overview</h2>
033 * <p>
034 * This manager was used throughout all Maven 3.x versions for backward compatibility reasons.
035 * It provides the exact same dependency management behavior as Maven 2.x, which differs
036 * significantly from modern dependency management approaches.
037 * </p>
038 *
039 * <h2>Key Characteristics</h2>
040 * <ul>
041 * <li><strong>Exclusion Handling:</strong> Ignores exclusions introduced by direct dependencies</li>
042 * <li><strong>Management Scope:</strong> Only obeys root management, ignoring intermediate management</li>
043 * <li><strong>Depth Behavior:</strong> {@code deriveUntil=2}, {@code applyFrom=2} with special "hop" at {@code depth=1}</li>
044 * <li><strong>Level 1 Skip:</strong> Ignores context from depth=1 for Maven 2.x compatibility</li>
045 * </ul>
046 *
047 * <h2>When to Use</h2>
048 * <p>
049 * Use this manager when you need exact Maven 2.x compatibility behavior or when working
050 * with legacy projects that depend on Maven 2.x dependency resolution semantics.
051 * </p>
052 *
053 * <h2>Comparison with Other Managers</h2>
054 * <p>
055 * Unlike {@link TransitiveDependencyManager} and {@link DefaultDependencyManager}, this manager
056 * deliberately ignores certain dependency management rules to maintain backward compatibility.
057 * See {@code MavenITmng4720DependencyManagementExclusionMergeTest} for behavioral differences.
058 * </p>
059 *
060 * @see TransitiveDependencyManager
061 * @see DefaultDependencyManager
062 */
063public final class ClassicDependencyManager extends AbstractDependencyManager {
064    /**
065     * Creates a new dependency manager without any management information.
066     */
067    public ClassicDependencyManager() {
068        this(null);
069    }
070
071    /**
072     * Creates a new dependency manager without any management information.
073     * <p>
074     * This constructor initializes the manager with Maven 2.x compatible behavior:
075     * <ul>
076     * <li>deriveUntil = 2 (collect rules only from root level)</li>
077     * <li>applyFrom = 2 (apply rules starting from depth 2)</li>
078     * <li>Special depth=1 handling for backward compatibility</li>
079     * </ul>
080     *
081     * @param scopeManager application-specific scope manager for handling system dependencies,
082     *                     may be null to use legacy system dependency scope handling
083     * @since 2.0.12
084     */
085    public ClassicDependencyManager(ScopeManager scopeManager) {
086        super(2, 2, scopeManager);
087    }
088
089    @SuppressWarnings("checkstyle:ParameterNumber")
090    private ClassicDependencyManager(
091            AbstractDependencyManager parent,
092            int depth,
093            int deriveUntil,
094            int applyFrom,
095            MMap<Key, String> managedVersions,
096            MMap<Key, String> managedScopes,
097            MMap<Key, Boolean> managedOptionals,
098            MMap<Key, String> managedLocalPaths,
099            MMap<Key, Holder<Collection<Exclusion>>> managedExclusions,
100            SystemDependencyScope systemDependencyScope) {
101        super(
102                parent,
103                depth,
104                deriveUntil,
105                applyFrom,
106                managedVersions,
107                managedScopes,
108                managedOptionals,
109                managedLocalPaths,
110                managedExclusions,
111                systemDependencyScope);
112    }
113
114    /**
115     * Derives a child manager with Maven 2.x compatibility behavior.
116     * <p>
117     * <strong>Critical Maven 2.x Compatibility:</strong> This method implements a special
118     * "hop" at depth=1 that skips dependency management collection at that level. This
119     * behavior is essential for Maven 2.x compatibility and is verified by integration tests.
120     * </p>
121     * <p>
122     * <strong>Why the depth=1 skip is necessary:</strong> Maven 2.x did not collect dependency
123     * management from first-level dependencies, only from the root. Removing this skip would
124     * break backward compatibility with Maven 2.x projects.
125     * </p>
126     *
127     * @param context the dependency collection context
128     * @return a new child manager or the current instance with passed-through management
129     * @see <a href="https://github.com/apache/maven-integration-testing/blob/master/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng4720DependencyManagementExclusionMergeTest.java">MNG-4720 Integration Test</a>
130     */
131    @Override
132    public DependencyManager deriveChildManager(DependencyCollectionContext context) {
133        // MNG-4720: Maven2 backward compatibility
134        // Removing this IF makes one IT fail here (read comment above):
135        // https://github.com/apache/maven-integration-testing/blob/b4e8fd52b99a058336f9c7c5ec44fdbc1427759c/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng4720DependencyManagementExclusionMergeTest.java#L67
136        // Skipping level=1 (maven2 compatibility); see MavenITmng4720DependencyManagementExclusionMergeTest
137        if (depth == 1) {
138            return newInstance(managedVersions, managedScopes, managedOptionals, managedLocalPaths, managedExclusions);
139        }
140        return super.deriveChildManager(context);
141    }
142
143    @Override
144    protected DependencyManager newInstance(
145            MMap<Key, String> managedVersions,
146            MMap<Key, String> managedScopes,
147            MMap<Key, Boolean> managedOptionals,
148            MMap<Key, String> managedLocalPaths,
149            MMap<Key, Holder<Collection<Exclusion>>> managedExclusions) {
150        return new ClassicDependencyManager(
151                this,
152                depth + 1,
153                deriveUntil,
154                applyFrom,
155                managedVersions,
156                managedScopes,
157                managedOptionals,
158                managedLocalPaths,
159                managedExclusions,
160                systemDependencyScope);
161    }
162}