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