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