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;
036import org.eclipse.aether.util.artifact.JavaScopes;
037
038import static org.eclipse.aether.impl.scope.BuildScopeQuery.all;
039import static org.eclipse.aether.impl.scope.BuildScopeQuery.byBuildPath;
040import static org.eclipse.aether.impl.scope.BuildScopeQuery.byProjectPath;
041import static org.eclipse.aether.impl.scope.BuildScopeQuery.select;
042import static org.eclipse.aether.impl.scope.BuildScopeQuery.singleton;
043import static org.eclipse.aether.impl.scope.BuildScopeQuery.union;
044
045/**
046 * Maven3 scope configurations. Configures scope manager to support Maven3 scopes.
047 * <p>
048 * This manager supports the old Maven 3 dependency scopes + new scopes.
049 * <p>
050 * Note: Maven3 CANNOT support new scopes "test-only" and "test-runtime", as it does not distinguish
051 * resolution scope (the class {@code ResolutionScope} has only "TEST", instead of "TEST_COMPILE" and "TEST_RUNTIME").
052 * <em>This scope manager configuration is not used in Maven 3!</em>
053 *
054 * @since 2.0.11
055 */
056public final class Maven3ScopeManagerConfiguration implements ScopeManagerConfiguration {
057    public static final Maven3ScopeManagerConfiguration INSTANCE = new Maven3ScopeManagerConfiguration();
058    public static final String DS_NONE = "none";
059    public static final String DS_COMPILE = JavaScopes.COMPILE;
060    public static final String DS_RUNTIME = JavaScopes.RUNTIME;
061    public static final String DS_PROVIDED = JavaScopes.PROVIDED;
062    public static final String DS_SYSTEM = JavaScopes.SYSTEM;
063    public static final String DS_TEST = JavaScopes.TEST;
064
065    public static final String DS_COMPILE_ONLY = "compile-only";
066    public static final String DS_TEST_ONLY = "test-only";
067    public static final String DS_TEST_RUNTIME = "test-runtime";
068
069    public static final String RS_NONE = "none";
070    public static final String RS_MAIN_COMPILE = "main-compile";
071    public static final String RS_MAIN_COMPILE_PLUS_RUNTIME = "main-compilePlusRuntime";
072    public static final String RS_MAIN_RUNTIME = "main-runtime";
073    public static final String RS_MAIN_RUNTIME_PLUS_SYSTEM = "main-runtimePlusSystem";
074    public static final String RS_TEST = "test";
075    public static final String RS_TEST_COMPILE = "test-compile";
076    public static final String RS_TEST_RUNTIME = "test-runtime";
077
078    private Maven3ScopeManagerConfiguration() {}
079
080    @Override
081    public String getId() {
082        return "Maven3";
083    }
084
085    @Override
086    public boolean isStrictDependencyScopes() {
087        return false;
088    }
089
090    @Override
091    public boolean isStrictResolutionScopes() {
092        return false;
093    }
094
095    @Override
096    public BuildScopeSource getBuildScopeSource() {
097        return new BuildScopeMatrixSource(
098                Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
099                Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME),
100                CommonBuilds.MAVEN_TEST_BUILD_SCOPE);
101    }
102
103    @Override
104    public Collection<DependencyScope> buildDependencyScopes(InternalScopeManager internalScopeManager) {
105        ArrayList<DependencyScope> result = new ArrayList<>();
106        result.add(internalScopeManager.createDependencyScope(DS_NONE, false, Collections.emptySet()));
107        result.add(internalScopeManager.createDependencyScope(DS_COMPILE, true, all()));
108        result.add(internalScopeManager.createDependencyScope(
109                DS_COMPILE_ONLY, false, select(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_COMPILE)));
110        result.add(internalScopeManager.createDependencyScope(
111                DS_RUNTIME, true, byBuildPath(CommonBuilds.BUILD_PATH_RUNTIME)));
112        result.add(internalScopeManager.createDependencyScope(
113                DS_PROVIDED,
114                false,
115                union(
116                        byBuildPath(CommonBuilds.BUILD_PATH_COMPILE),
117                        select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME))));
118        result.add(internalScopeManager.createDependencyScope(
119                DS_TEST, false, byProjectPath(CommonBuilds.PROJECT_PATH_TEST)));
120        result.add(internalScopeManager.createDependencyScope(
121                DS_TEST_ONLY, false, singleton(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_COMPILE)));
122        result.add(internalScopeManager.createDependencyScope(
123                DS_TEST_RUNTIME, false, singleton(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME)));
124        result.add(internalScopeManager.createSystemDependencyScope(
125                DS_SYSTEM, false, all(), ArtifactProperties.LOCAL_PATH));
126        return result;
127    }
128
129    @Override
130    public Collection<ResolutionScope> buildResolutionScopes(InternalScopeManager internalScopeManager) {
131        Collection<DependencyScope> allDependencyScopes = internalScopeManager.getDependencyScopeUniverse();
132        Collection<DependencyScope> nonTransitiveDependencyScopes =
133                allDependencyScopes.stream().filter(s -> !s.isTransitive()).collect(Collectors.toSet());
134        DependencyScope system =
135                internalScopeManager.getDependencyScope(DS_SYSTEM).orElse(null);
136
137        ArrayList<ResolutionScope> result = new ArrayList<>();
138        result.add(internalScopeManager.createResolutionScope(
139                RS_NONE,
140                InternalScopeManager.Mode.REMOVE,
141                Collections.emptySet(),
142                Collections.emptySet(),
143                allDependencyScopes));
144        result.add(internalScopeManager.createResolutionScope(
145                RS_MAIN_COMPILE,
146                Collections.singleton("compile"),
147                InternalScopeManager.Mode.ELIMINATE,
148                singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_COMPILE),
149                Collections.singletonList(system),
150                nonTransitiveDependencyScopes));
151        result.add(internalScopeManager.createResolutionScope(
152                RS_MAIN_COMPILE_PLUS_RUNTIME,
153                Collections.singleton("compile+runtime"),
154                InternalScopeManager.Mode.ELIMINATE,
155                byProjectPath(CommonBuilds.PROJECT_PATH_MAIN),
156                Collections.singletonList(system),
157                nonTransitiveDependencyScopes));
158        result.add(internalScopeManager.createResolutionScope(
159                RS_MAIN_RUNTIME,
160                Collections.singleton("runtime"),
161                InternalScopeManager.Mode.ELIMINATE,
162                singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_RUNTIME),
163                Collections.emptySet(),
164                nonTransitiveDependencyScopes));
165        result.add(internalScopeManager.createResolutionScope(
166                RS_MAIN_RUNTIME_PLUS_SYSTEM,
167                Collections.singleton("runtime+system"),
168                InternalScopeManager.Mode.ELIMINATE,
169                singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_RUNTIME),
170                Collections.singletonList(system),
171                nonTransitiveDependencyScopes));
172        result.add(internalScopeManager.createResolutionScope(
173                RS_TEST,
174                InternalScopeManager.Mode.ELIMINATE,
175                byProjectPath(CommonBuilds.PROJECT_PATH_TEST),
176                Collections.singletonList(system),
177                nonTransitiveDependencyScopes));
178        result.add(internalScopeManager.createResolutionScope(
179                RS_TEST_COMPILE,
180                InternalScopeManager.Mode.ELIMINATE,
181                select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_COMPILE),
182                Collections.singletonList(system),
183                nonTransitiveDependencyScopes));
184        result.add(internalScopeManager.createResolutionScope(
185                RS_TEST_RUNTIME,
186                InternalScopeManager.Mode.ELIMINATE,
187                select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME),
188                Collections.singletonList(system),
189                nonTransitiveDependencyScopes));
190        return result;
191    }
192
193    // ===
194
195    public static void main(String... args) {
196        ScopeManagerDump.dump(Maven3ScopeManagerConfiguration.INSTANCE);
197    }
198}