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