View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.util.graph.manager;
20  
21  import java.util.Collection;
22  
23  import org.eclipse.aether.collection.DependencyCollectionContext;
24  import org.eclipse.aether.collection.DependencyManager;
25  import org.eclipse.aether.graph.Exclusion;
26  import org.eclipse.aether.scope.ScopeManager;
27  import org.eclipse.aether.scope.SystemDependencyScope;
28  
29  /**
30   * A dependency manager that mimics the way Maven 2.x works for backward compatibility.
31   *
32   * <h2>Overview</h2>
33   * <p>
34   * This manager was used throughout all Maven 3.x versions for backward compatibility reasons.
35   * It provides the exact same dependency management behavior as Maven 2.x, which differs
36   * significantly from modern dependency management approaches.
37   * </p>
38   *
39   * <h2>Key Characteristics</h2>
40   * <ul>
41   * <li><strong>Exclusion Handling:</strong> Ignores exclusions introduced by direct dependencies</li>
42   * <li><strong>Management Scope:</strong> Only obeys root management, ignoring intermediate management</li>
43   * <li><strong>Depth Behavior:</strong> {@code deriveUntil=2}, {@code applyFrom=2} with special "hop" at {@code depth=1}</li>
44   * <li><strong>Level 1 Skip:</strong> Ignores context from depth=1 for Maven 2.x compatibility</li>
45   * </ul>
46   *
47   * <h2>When to Use</h2>
48   * <p>
49   * Use this manager when you need exact Maven 2.x compatibility behavior or when working
50   * with legacy projects that depend on Maven 2.x dependency resolution semantics.
51   * </p>
52   *
53   * <h2>Comparison with Other Managers</h2>
54   * <p>
55   * Unlike {@link TransitiveDependencyManager} and {@link DefaultDependencyManager}, this manager
56   * deliberately ignores certain dependency management rules to maintain backward compatibility.
57   * See {@code MavenITmng4720DependencyManagementExclusionMergeTest} for behavioral differences.
58   * </p>
59   *
60   * @see TransitiveDependencyManager
61   * @see DefaultDependencyManager
62   */
63  public final class ClassicDependencyManager extends AbstractDependencyManager {
64      /**
65       * Creates a new dependency manager without any management information.
66       */
67      public ClassicDependencyManager() {
68          this(null);
69      }
70  
71      /**
72       * Creates a new dependency manager without any management information.
73       * <p>
74       * This constructor initializes the manager with Maven 2.x compatible behavior:
75       * <ul>
76       * <li>deriveUntil = 2 (collect rules only from root level)</li>
77       * <li>applyFrom = 2 (apply rules starting from depth 2)</li>
78       * <li>Special depth=1 handling for backward compatibility</li>
79       * </ul>
80       *
81       * @param scopeManager application-specific scope manager for handling system dependencies,
82       *                     may be null to use legacy system dependency scope handling
83       * @since 2.0.12
84       */
85      public ClassicDependencyManager(ScopeManager scopeManager) {
86          super(2, 2, scopeManager);
87      }
88  
89      @SuppressWarnings("checkstyle:ParameterNumber")
90      private ClassicDependencyManager(
91              AbstractDependencyManager parent,
92              int depth,
93              int deriveUntil,
94              int applyFrom,
95              MMap<Key, String> managedVersions,
96              MMap<Key, String> managedScopes,
97              MMap<Key, Boolean> managedOptionals,
98              MMap<Key, String> managedLocalPaths,
99              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 }