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 * Maven4 scope configurations. Configures scope manager to support Maven4 scopes.
046 *
047 * @since 2.0.0
048 */
049public final class Maven4ScopeManagerConfiguration implements ScopeManagerConfiguration {
050    public static final Maven4ScopeManagerConfiguration INSTANCE = new Maven4ScopeManagerConfiguration();
051
052    public static final String DS_NONE = "none";
053    public static final String DS_COMPILE = "compile";
054    public static final String DS_COMPILE_ONLY = "compileOnly";
055    public static final String DS_RUNTIME = "runtime";
056    public static final String DS_PROVIDED = "provided";
057    public static final String DS_SYSTEM = "system";
058    public static final String DS_TEST = "test";
059    public static final String DS_TEST_RUNTIME = "testRuntime";
060    public static final String DS_TEST_ONLY = "testOnly";
061    public static final String RS_NONE = "none";
062    public static final String RS_MAIN_COMPILE = "main-compile";
063    public static final String RS_MAIN_COMPILE_PLUS_RUNTIME = "main-compilePlusRuntime";
064    public static final String RS_MAIN_RUNTIME = "main-runtime";
065    public static final String RS_MAIN_RUNTIME_PLUS_SYSTEM = "main-runtimePlusSystem";
066    public static final String RS_TEST_COMPILE = "test-compile";
067    public static final String RS_TEST_RUNTIME = "test-runtime";
068
069    private Maven4ScopeManagerConfiguration() {}
070
071    @Override
072    public String getId() {
073        return "Maven4";
074    }
075
076    @Override
077    public boolean isStrictDependencyScopes() {
078        return false;
079    }
080
081    @Override
082    public boolean isStrictResolutionScopes() {
083        return false;
084    }
085
086    @Override
087    public BuildScopeSource getBuildScopeSource() {
088        return new BuildScopeMatrixSource(
089                Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
090                Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME));
091    }
092
093    @Override
094    public Collection<DependencyScope> buildDependencyScopes(InternalScopeManager internalScopeManager) {
095        ArrayList<DependencyScope> result = new ArrayList<>();
096        result.add(internalScopeManager.createDependencyScope(DS_COMPILE, true, all()));
097        result.add(internalScopeManager.createDependencyScope(
098                DS_RUNTIME, true, byBuildPath(CommonBuilds.BUILD_PATH_RUNTIME)));
099        result.add(internalScopeManager.createDependencyScope(
100                DS_PROVIDED,
101                false,
102                union(
103                        byBuildPath(CommonBuilds.BUILD_PATH_COMPILE),
104                        select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME))));
105        result.add(internalScopeManager.createDependencyScope(
106                DS_TEST, false, byProjectPath(CommonBuilds.PROJECT_PATH_TEST)));
107        result.add(internalScopeManager.createSystemDependencyScope(
108                DS_SYSTEM, false, all(), ArtifactProperties.LOCAL_PATH));
109        result.add(internalScopeManager.createDependencyScope(DS_NONE, false, Collections.emptySet()));
110        result.add(internalScopeManager.createDependencyScope(
111                DS_COMPILE_ONLY, false, singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_COMPILE)));
112        result.add(internalScopeManager.createDependencyScope(
113                DS_TEST_RUNTIME, false, singleton(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME)));
114        result.add(internalScopeManager.createDependencyScope(
115                DS_TEST_ONLY, false, singleton(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_COMPILE)));
116        return result;
117    }
118
119    @Override
120    public Collection<ResolutionScope> buildResolutionScopes(InternalScopeManager internalScopeManager) {
121        Collection<DependencyScope> allDependencyScopes = internalScopeManager.getDependencyScopeUniverse();
122        Collection<DependencyScope> nonTransitiveDependencyScopes =
123                allDependencyScopes.stream().filter(s -> !s.isTransitive()).collect(Collectors.toSet());
124        DependencyScope system =
125                internalScopeManager.getDependencyScope(DS_SYSTEM).orElse(null);
126
127        ArrayList<ResolutionScope> result = new ArrayList<>();
128        result.add(internalScopeManager.createResolutionScope(
129                RS_NONE,
130                InternalScopeManager.Mode.REMOVE,
131                Collections.emptySet(),
132                Collections.emptySet(),
133                allDependencyScopes));
134        result.add(internalScopeManager.createResolutionScope(
135                RS_MAIN_COMPILE,
136                InternalScopeManager.Mode.ELIMINATE,
137                singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_COMPILE),
138                Collections.singletonList(system),
139                nonTransitiveDependencyScopes));
140        result.add(internalScopeManager.createResolutionScope(
141                RS_MAIN_COMPILE_PLUS_RUNTIME,
142                InternalScopeManager.Mode.ELIMINATE,
143                byProjectPath(CommonBuilds.PROJECT_PATH_MAIN),
144                Collections.singletonList(system),
145                nonTransitiveDependencyScopes));
146        result.add(internalScopeManager.createResolutionScope(
147                RS_MAIN_RUNTIME,
148                InternalScopeManager.Mode.REMOVE,
149                singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_RUNTIME),
150                Collections.emptySet(),
151                nonTransitiveDependencyScopes));
152        result.add(internalScopeManager.createResolutionScope(
153                RS_MAIN_RUNTIME_PLUS_SYSTEM,
154                InternalScopeManager.Mode.REMOVE,
155                singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_RUNTIME),
156                Collections.singletonList(system),
157                nonTransitiveDependencyScopes));
158        result.add(internalScopeManager.createResolutionScope(
159                RS_TEST_COMPILE,
160                InternalScopeManager.Mode.ELIMINATE,
161                select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_COMPILE),
162                Collections.singletonList(system),
163                nonTransitiveDependencyScopes));
164        result.add(internalScopeManager.createResolutionScope(
165                RS_TEST_RUNTIME,
166                InternalScopeManager.Mode.ELIMINATE,
167                select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME),
168                Collections.singletonList(system),
169                nonTransitiveDependencyScopes));
170        return result;
171    }
172
173    // ===
174
175    public static void main(String... args) {
176        ScopeManagerDump.dump(Maven4ScopeManagerConfiguration.INSTANCE);
177    }
178}