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.supplier;
020
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.Collection;
024import java.util.Collections;
025import java.util.stream.Collectors;
026
027import org.eclipse.aether.artifact.ArtifactProperties;
028import org.eclipse.aether.impl.scope.BuildScopeMatrixSource;
029import org.eclipse.aether.impl.scope.BuildScopeSource;
030import org.eclipse.aether.impl.scope.CommonBuilds;
031import org.eclipse.aether.impl.scope.InternalScopeManager;
032import org.eclipse.aether.impl.scope.ScopeManagerConfiguration;
033import org.eclipse.aether.internal.impl.scope.ScopeManagerDump;
034import org.eclipse.aether.scope.DependencyScope;
035import org.eclipse.aether.scope.ResolutionScope;
036
037import static org.eclipse.aether.impl.scope.BuildScopeQuery.all;
038import static org.eclipse.aether.impl.scope.BuildScopeQuery.byBuildPath;
039import static org.eclipse.aether.impl.scope.BuildScopeQuery.byProjectPath;
040import static org.eclipse.aether.impl.scope.BuildScopeQuery.select;
041import static org.eclipse.aether.impl.scope.BuildScopeQuery.singleton;
042import static org.eclipse.aether.impl.scope.BuildScopeQuery.union;
043
044/**
045 * Maven3 scope configurations. Configures scope manager to support Maven3 scopes.
046 *
047 * @since 2.0.11
048 */
049public final class Maven3ScopeManagerConfiguration implements ScopeManagerConfiguration {
050    public static final Maven3ScopeManagerConfiguration INSTANCE = new Maven3ScopeManagerConfiguration();
051    public static final String DS_COMPILE = "compile";
052    public static final String DS_RUNTIME = "runtime";
053    public static final String DS_PROVIDED = "provided";
054    public static final String DS_SYSTEM = "system";
055    public static final String DS_TEST = "test";
056    public static final String RS_NONE = "none";
057    public static final String RS_MAIN_COMPILE = "main-compile";
058    public static final String RS_MAIN_COMPILE_PLUS_RUNTIME = "main-compilePlusRuntime";
059    public static final String RS_MAIN_RUNTIME = "main-runtime";
060    public static final String RS_MAIN_RUNTIME_PLUS_SYSTEM = "main-runtimePlusSystem";
061    public static final String RS_TEST_COMPILE = "test-compile";
062    public static final String RS_TEST_RUNTIME = "test-runtime";
063
064    private Maven3ScopeManagerConfiguration() {}
065
066    @Override
067    public String getId() {
068        return "Maven3";
069    }
070
071    @Override
072    public boolean isStrictDependencyScopes() {
073        return false;
074    }
075
076    @Override
077    public boolean isStrictResolutionScopes() {
078        return false;
079    }
080
081    @Override
082    public BuildScopeSource getBuildScopeSource() {
083        return new BuildScopeMatrixSource(
084                Collections.singletonList(CommonBuilds.PROJECT_PATH_MAIN),
085                Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME),
086                CommonBuilds.MAVEN_TEST_BUILD_SCOPE);
087    }
088
089    @Override
090    public Collection<DependencyScope> buildDependencyScopes(InternalScopeManager internalScopeManager) {
091        ArrayList<DependencyScope> result = new ArrayList<>();
092        result.add(internalScopeManager.createDependencyScope(DS_COMPILE, true, all()));
093        result.add(internalScopeManager.createDependencyScope(
094                DS_RUNTIME, true, byBuildPath(CommonBuilds.BUILD_PATH_RUNTIME)));
095        result.add(internalScopeManager.createDependencyScope(
096                DS_PROVIDED,
097                false,
098                union(
099                        byBuildPath(CommonBuilds.BUILD_PATH_COMPILE),
100                        select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME))));
101        result.add(internalScopeManager.createDependencyScope(
102                DS_TEST, false, byProjectPath(CommonBuilds.PROJECT_PATH_TEST)));
103        result.add(internalScopeManager.createSystemDependencyScope(
104                DS_SYSTEM, false, all(), ArtifactProperties.LOCAL_PATH));
105        return result;
106    }
107
108    @Override
109    public Collection<ResolutionScope> buildResolutionScopes(InternalScopeManager internalScopeManager) {
110        Collection<DependencyScope> allDependencyScopes = internalScopeManager.getDependencyScopeUniverse();
111        Collection<DependencyScope> nonTransitiveDependencyScopes =
112                allDependencyScopes.stream().filter(s -> !s.isTransitive()).collect(Collectors.toSet());
113        DependencyScope system =
114                internalScopeManager.getDependencyScope(DS_SYSTEM).orElse(null);
115
116        ArrayList<ResolutionScope> result = new ArrayList<>();
117        result.add(internalScopeManager.createResolutionScope(
118                RS_NONE,
119                InternalScopeManager.Mode.REMOVE,
120                Collections.emptySet(),
121                Collections.emptySet(),
122                allDependencyScopes));
123        result.add(internalScopeManager.createResolutionScope(
124                RS_MAIN_COMPILE,
125                InternalScopeManager.Mode.ELIMINATE,
126                singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_COMPILE),
127                Collections.singletonList(system),
128                nonTransitiveDependencyScopes));
129        result.add(internalScopeManager.createResolutionScope(
130                RS_MAIN_COMPILE_PLUS_RUNTIME,
131                InternalScopeManager.Mode.ELIMINATE,
132                byProjectPath(CommonBuilds.PROJECT_PATH_MAIN),
133                Collections.singletonList(system),
134                nonTransitiveDependencyScopes));
135        result.add(internalScopeManager.createResolutionScope(
136                RS_MAIN_RUNTIME,
137                InternalScopeManager.Mode.ELIMINATE,
138                singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_RUNTIME),
139                Collections.emptySet(),
140                nonTransitiveDependencyScopes));
141        result.add(internalScopeManager.createResolutionScope(
142                RS_MAIN_RUNTIME_PLUS_SYSTEM,
143                InternalScopeManager.Mode.ELIMINATE,
144                singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_RUNTIME),
145                Collections.singletonList(system),
146                nonTransitiveDependencyScopes));
147        result.add(internalScopeManager.createResolutionScope(
148                RS_TEST_COMPILE,
149                InternalScopeManager.Mode.ELIMINATE,
150                select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_COMPILE),
151                Collections.singletonList(system),
152                nonTransitiveDependencyScopes));
153        result.add(internalScopeManager.createResolutionScope(
154                RS_TEST_RUNTIME,
155                InternalScopeManager.Mode.ELIMINATE,
156                select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME),
157                Collections.singletonList(system),
158                nonTransitiveDependencyScopes));
159        return result;
160    }
161
162    // ===
163
164    public static void main(String... args) {
165        ScopeManagerDump.dump(Maven3ScopeManagerConfiguration.INSTANCE);
166    }
167}