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. This manager was used throughout all Maven 3.x versions.
032 * <p>
033 * This manager has {@code deriveUntil=2} and {@code applyFrom=2}.
034 * <p>
035 * Note regarding transitivity: it is broken, and should not be used.
036 */
037public final class ClassicDependencyManager extends AbstractDependencyManager {
038    /**
039     * Creates a new dependency manager without any management information.
040     *
041     * @deprecated Use constructor that provides consumer application specific predicate.
042     */
043    @Deprecated
044    public ClassicDependencyManager() {
045        this(null);
046    }
047
048    public ClassicDependencyManager(ScopeManager scopeManager) {
049        this(false, scopeManager);
050    }
051
052    /**
053     * Creates a new dependency manager without any management information.
054     *
055     * @param transitive If true, this manager will collect (derive) until last node on graph. If false,
056     *                   it will work as original Maven 3 "classic" dependency manager, collect only up to
057     *                   depth of 2.
058     *
059     * @since 2.0.0
060     */
061    public ClassicDependencyManager(boolean transitive, ScopeManager scopeManager) {
062        super(transitive ? Integer.MAX_VALUE : 2, 2, scopeManager);
063    }
064
065    @SuppressWarnings("checkstyle:ParameterNumber")
066    private ClassicDependencyManager(
067            ArrayList<AbstractDependencyManager> path,
068            int depth,
069            int deriveUntil,
070            int applyFrom,
071            MMap<Key, String> managedVersions,
072            MMap<Key, String> managedScopes,
073            MMap<Key, Boolean> managedOptionals,
074            MMap<Key, String> managedLocalPaths,
075            MMap<Key, Holder<Collection<Exclusion>>> managedExclusions,
076            SystemDependencyScope systemDependencyScope) {
077        super(
078                path,
079                depth,
080                deriveUntil,
081                applyFrom,
082                managedVersions,
083                managedScopes,
084                managedOptionals,
085                managedLocalPaths,
086                managedExclusions,
087                systemDependencyScope);
088    }
089
090    @Override
091    public DependencyManager deriveChildManager(DependencyCollectionContext context) {
092        // MNG-4720: Maven2 backward compatibility
093        // Removing this IF makes one IT fail here (read comment above):
094        // https://github.com/apache/maven-integration-testing/blob/b4e8fd52b99a058336f9c7c5ec44fdbc1427759c/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng4720DependencyManagementExclusionMergeTest.java#L67
095        if (depth == 1) {
096            return newInstance(managedVersions, managedScopes, managedOptionals, managedLocalPaths, managedExclusions);
097        }
098        return super.deriveChildManager(context);
099    }
100
101    @Override
102    protected DependencyManager newInstance(
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        ArrayList<AbstractDependencyManager> path = new ArrayList<>(this.path);
109        path.add(this);
110        return new ClassicDependencyManager(
111                path,
112                depth + 1,
113                deriveUntil,
114                applyFrom,
115                managedVersions,
116                managedScopes,
117                managedOptionals,
118                managedLocalPaths,
119                managedExclusions,
120                systemDependencyScope);
121    }
122}