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