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