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  
37  import static org.eclipse.aether.impl.scope.BuildScopeQuery.all;
38  import static org.eclipse.aether.impl.scope.BuildScopeQuery.byBuildPath;
39  import static org.eclipse.aether.impl.scope.BuildScopeQuery.byProjectPath;
40  import static org.eclipse.aether.impl.scope.BuildScopeQuery.select;
41  import static org.eclipse.aether.impl.scope.BuildScopeQuery.singleton;
42  import static org.eclipse.aether.impl.scope.BuildScopeQuery.union;
43  
44  /**
45   * Maven3 scope configurations. Configures scope manager to support Maven3 scopes.
46   * <p>
47   * This manager supports the old Maven 3 dependency scopes + new "compile-only".
48   * <p>
49   * Note: Maven3 CANNOT support Maven 4 scopes "test-only" and "test-runtime", as it does not distinguish
50   * resolution scope (the class {@code ResolutionScope} has only "TEST", instead of "TEST_COMPILE" and "TEST_RUNTIME").
51   *
52   * @since 2.0.11
53   */
54  public final class Maven3ScopeManagerConfiguration implements ScopeManagerConfiguration {
55      public static final Maven3ScopeManagerConfiguration INSTANCE = new Maven3ScopeManagerConfiguration();
56      public static final String DS_NONE = "none";
57      public static final String DS_COMPILE = "compile"; // JavaScopes.COMPILE;
58      public static final String DS_COMPILE_ONLY = "compile-only";
59      public static final String DS_RUNTIME = "runtime"; // JavaScopes.RUNTIME;
60      public static final String DS_PROVIDED = "provided"; // JavaScopes.PROVIDED;
61      public static final String DS_SYSTEM = "system"; // JavaScopes.SYSTEM;
62      public static final String DS_TEST = "test"; // JavaScopes.TEST;
63      public static final String DS_TEST_ONLY = "test-only";
64      public static final String DS_TEST_RUNTIME = "test-runtime";
65  
66      public static final String RS_NONE = "none";
67      public static final String RS_MAIN_COMPILE = "main-compile";
68      public static final String RS_MAIN_COMPILE_PLUS_RUNTIME = "main-compilePlusRuntime";
69      public static final String RS_MAIN_RUNTIME = "main-runtime";
70      public static final String RS_MAIN_RUNTIME_PLUS_SYSTEM = "main-runtimePlusSystem";
71      public static final String RS_TEST = "test";
72      public static final String RS_TEST_COMPILE = "test-compile";
73      public static final String RS_TEST_RUNTIME = "test-runtime";
74  
75      private Maven3ScopeManagerConfiguration() {}
76  
77      @Override
78      public String getId() {
79          return "Maven3";
80      }
81  
82      @Override
83      public boolean isStrictDependencyScopes() {
84          return false;
85      }
86  
87      @Override
88      public boolean isStrictResolutionScopes() {
89          return false;
90      }
91  
92      @Override
93      public BuildScopeSource getBuildScopeSource() {
94          return new BuildScopeMatrixSource(
95                  Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
96                  Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME),
97                  CommonBuilds.MAVEN_TEST_BUILD_SCOPE);
98      }
99  
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 }