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   * Maven4 scope configurations. Configures scope manager to support Maven4 scopes.
46   *
47   * @since 2.0.0
48   */
49  public final class Maven4ScopeManagerConfiguration implements ScopeManagerConfiguration {
50      public static final Maven4ScopeManagerConfiguration INSTANCE = new Maven4ScopeManagerConfiguration();
51  
52      public static final String DS_NONE = "none";
53      public static final String DS_COMPILE = "compile";
54      public static final String DS_COMPILE_ONLY = "compileOnly";
55      public static final String DS_RUNTIME = "runtime";
56      public static final String DS_PROVIDED = "provided";
57      public static final String DS_SYSTEM = "system";
58      public static final String DS_TEST = "test";
59      public static final String DS_TEST_RUNTIME = "testRuntime";
60      public static final String DS_TEST_ONLY = "testOnly";
61      public static final String RS_NONE = "none";
62      public static final String RS_MAIN_COMPILE = "main-compile";
63      public static final String RS_MAIN_COMPILE_PLUS_RUNTIME = "main-compilePlusRuntime";
64      public static final String RS_MAIN_RUNTIME = "main-runtime";
65      public static final String RS_MAIN_RUNTIME_PLUS_SYSTEM = "main-runtimePlusSystem";
66      public static final String RS_TEST_COMPILE = "test-compile";
67      public static final String RS_TEST_RUNTIME = "test-runtime";
68  
69      private Maven4ScopeManagerConfiguration() {}
70  
71      @Override
72      public String getId() {
73          return "Maven4";
74      }
75  
76      @Override
77      public boolean isStrictDependencyScopes() {
78          return false;
79      }
80  
81      @Override
82      public boolean isStrictResolutionScopes() {
83          return false;
84      }
85  
86      @Override
87      public BuildScopeSource getBuildScopeSource() {
88          return new BuildScopeMatrixSource(
89                  Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
90                  Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME));
91      }
92  
93      @Override
94      public Collection<DependencyScope> buildDependencyScopes(InternalScopeManager internalScopeManager) {
95          ArrayList<DependencyScope> result = new ArrayList<>();
96          result.add(internalScopeManager.createDependencyScope(DS_COMPILE, true, all()));
97          result.add(internalScopeManager.createDependencyScope(
98                  DS_RUNTIME, true, byBuildPath(CommonBuilds.BUILD_PATH_RUNTIME)));
99          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 }