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.ArrayList;
22 import java.util.Collection;
23
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 provides proper transitive dependency management for modern Maven usage.
31 *
32 * <h2>Overview</h2>
33 * <p>
34 * This manager implements proper "transitive dependency management" that works harmoniously
35 * with Maven's ModelBuilder. It produces more precise results regarding versions by respecting
36 * transitive management rules while allowing higher-level management to override lower-level rules.
37 * </p>
38 *
39 * <h2>Key Characteristics</h2>
40 * <ul>
41 * <li><strong>Transitive Management:</strong> {@code deriveUntil=Integer.MAX_VALUE}, {@code applyFrom=2}</li>
42 * <li><strong>ModelBuilder Friendly:</strong> Works in conjunction with, not against, ModelBuilder</li>
43 * <li><strong>Inheritance Aware:</strong> Special handling for scope and optional properties</li>
44 * <li><strong>Precise Versioning:</strong> Obeys transitive management unless managed at higher levels</li>
45 * </ul>
46 *
47 * <h2>Inheritance Handling</h2>
48 * <p>
49 * This manager provides special care for "scope" and "optional" properties that are subject
50 * to inheritance in the dependency graph during later graph transformation steps. These
51 * properties are only derived from the root to prevent interference with inheritance logic.
52 * </p>
53 *
54 * <h2>When to Use</h2>
55 * <p>
56 * This is the <strong>recommended manager for modern Maven projects</strong> that need proper
57 * transitive dependency management while maintaining compatibility with Maven's ModelBuilder.
58 * </p>
59 *
60 * <h2>Comparison with Other Managers</h2>
61 * <ul>
62 * <li>{@link ClassicDependencyManager}: Maven 2.x compatibility, limited transitive support</li>
63 * <li>{@link DefaultDependencyManager}: Aggressive but interferes with ModelBuilder</li>
64 * <li><strong>This manager:</strong> Modern, transitive, ModelBuilder-compatible (recommended)</li>
65 * </ul>
66 *
67 * @author Christian Schulte
68 * @since 1.4.0
69 * @see ClassicDependencyManager
70 * @see DefaultDependencyManager
71 */
72 public final class TransitiveDependencyManager extends AbstractDependencyManager {
73 /**
74 * Creates a new dependency manager without any management information.
75 */
76 public TransitiveDependencyManager() {
77 this(null);
78 }
79
80 /**
81 * Creates a new transitive dependency manager with ModelBuilder-compatible behavior.
82 * <p>
83 * This constructor initializes the manager with settings optimized for modern Maven usage:
84 * <ul>
85 * <li>deriveUntil = Integer.MAX_VALUE (collect management rules at all levels)</li>
86 * <li>applyFrom = 2 (apply management starting from depth 2, respecting ModelBuilder)</li>
87 * <li>Special inheritance handling for scope and optional properties</li>
88 * </ul>
89 *
90 * @param scopeManager application-specific scope manager for handling system dependencies,
91 * may be null to use legacy system dependency scope handling
92 */
93 public TransitiveDependencyManager(ScopeManager scopeManager) {
94 super(Integer.MAX_VALUE, 2, scopeManager);
95 }
96
97 @SuppressWarnings("checkstyle:ParameterNumber")
98 private TransitiveDependencyManager(
99 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 }