View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.supplier;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.stream.Collectors;
26  
27  import org.eclipse.aether.artifact.ArtifactProperties;
28  import org.eclipse.aether.impl.scope.BuildScopeMatrixSource;
29  import org.eclipse.aether.impl.scope.BuildScopeSource;
30  import org.eclipse.aether.impl.scope.CommonBuilds;
31  import org.eclipse.aether.impl.scope.InternalScopeManager;
32  import org.eclipse.aether.impl.scope.ScopeManagerConfiguration;
33  import org.eclipse.aether.internal.impl.scope.ScopeManagerDump;
34  import org.eclipse.aether.scope.DependencyScope;
35  import org.eclipse.aether.scope.ResolutionScope;
36  import org.eclipse.aether.util.artifact.JavaScopes;
37  
38  import static org.eclipse.aether.impl.scope.BuildScopeQuery.all;
39  import static org.eclipse.aether.impl.scope.BuildScopeQuery.byBuildPath;
40  import static org.eclipse.aether.impl.scope.BuildScopeQuery.byProjectPath;
41  import static org.eclipse.aether.impl.scope.BuildScopeQuery.select;
42  import static org.eclipse.aether.impl.scope.BuildScopeQuery.singleton;
43  import static org.eclipse.aether.impl.scope.BuildScopeQuery.union;
44  
45  /**
46   * Maven3 scope configurations. Configures scope manager to support Maven3 scopes.
47   * <p>
48   * This manager supports the old Maven 3 dependency scopes + new scopes.
49   * <p>
50   * Note: Maven3 CANNOT support new scopes "test-only" and "test-runtime", as it does not distinguish
51   * resolution scope (the class {@code ResolutionScope} has only "TEST", instead of "TEST_COMPILE" and "TEST_RUNTIME").
52   * <em>This scope manager configuration is not used in Maven 3!</em>
53   *
54   * @since 2.0.11
55   */
56  public final class Maven3ScopeManagerConfiguration implements ScopeManagerConfiguration {
57      public static final Maven3ScopeManagerConfiguration INSTANCE = new Maven3ScopeManagerConfiguration();
58      public static final String DS_NONE = "none";
59      public static final String DS_COMPILE = JavaScopes.COMPILE;
60      public static final String DS_RUNTIME = JavaScopes.RUNTIME;
61      public static final String DS_PROVIDED = JavaScopes.PROVIDED;
62      public static final String DS_SYSTEM = JavaScopes.SYSTEM;
63      public static final String DS_TEST = JavaScopes.TEST;
64  
65      public static final String DS_COMPILE_ONLY = "compile-only";
66      public static final String DS_TEST_ONLY = "test-only";
67      public static final String DS_TEST_RUNTIME = "test-runtime";
68  
69      public static final String RS_NONE = "none";
70      public static final String RS_MAIN_COMPILE = "main-compile";
71      public static final String RS_MAIN_COMPILE_PLUS_RUNTIME = "main-compilePlusRuntime";
72      public static final String RS_MAIN_RUNTIME = "main-runtime";
73      public static final String RS_MAIN_RUNTIME_PLUS_SYSTEM = "main-runtimePlusSystem";
74      public static final String RS_TEST = "test";
75      public static final String RS_TEST_COMPILE = "test-compile";
76      public static final String RS_TEST_RUNTIME = "test-runtime";
77  
78      private Maven3ScopeManagerConfiguration() {}
79  
80      @Override
81      public String getId() {
82          return "Maven3";
83      }
84  
85      @Override
86      public boolean isStrictDependencyScopes() {
87          return false;
88      }
89  
90      @Override
91      public boolean isStrictResolutionScopes() {
92          return false;
93      }
94  
95      @Override
96      public BuildScopeSource getBuildScopeSource() {
97          return new BuildScopeMatrixSource(
98                  Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
99                  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 }