1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.repository.internal.scopes;
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.apache.maven.api.DependencyScope;
28  import org.apache.maven.repository.internal.artifact.MavenArtifactProperties;
29  import org.eclipse.aether.impl.scope.BuildScopeMatrixSource;
30  import org.eclipse.aether.impl.scope.BuildScopeSource;
31  import org.eclipse.aether.impl.scope.CommonBuilds;
32  import org.eclipse.aether.impl.scope.InternalScopeManager;
33  import org.eclipse.aether.impl.scope.ScopeManagerConfiguration;
34  import org.eclipse.aether.internal.impl.scope.ScopeManagerDump;
35  
36  import static org.eclipse.aether.impl.scope.BuildScopeQuery.all;
37  import static org.eclipse.aether.impl.scope.BuildScopeQuery.byBuildPath;
38  import static org.eclipse.aether.impl.scope.BuildScopeQuery.byProjectPath;
39  import static org.eclipse.aether.impl.scope.BuildScopeQuery.select;
40  import static org.eclipse.aether.impl.scope.BuildScopeQuery.singleton;
41  import static org.eclipse.aether.impl.scope.BuildScopeQuery.union;
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  @Deprecated(since = "4.0.0")
52  public final class Maven4ScopeManagerConfiguration implements ScopeManagerConfiguration {
53      public static final Maven4ScopeManagerConfiguration INSTANCE = new Maven4ScopeManagerConfiguration();
54  
55      public static final String RS_NONE = "none";
56      public static final String RS_MAIN_COMPILE = "main-compile";
57      public static final String RS_MAIN_COMPILE_PLUS_RUNTIME = "main-compilePlusRuntime";
58      public static final String RS_MAIN_RUNTIME = "main-runtime";
59      public static final String RS_MAIN_RUNTIME_PLUS_SYSTEM = "main-runtimePlusSystem";
60      public static final String RS_TEST_COMPILE = "test-compile";
61      public static final String RS_TEST_RUNTIME = "test-runtime";
62  
63      private Maven4ScopeManagerConfiguration() {}
64  
65      @Override
66      public String getId() {
67          return "Maven4";
68      }
69  
70      @Override
71      public boolean isStrictDependencyScopes() {
72          return false;
73      }
74  
75      @Override
76      public boolean isStrictResolutionScopes() {
77          return false;
78      }
79  
80      @Override
81      public BuildScopeSource getBuildScopeSource() {
82          return new BuildScopeMatrixSource(
83                  Arrays.asList(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.PROJECT_PATH_TEST),
84                  Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME));
85      }
86  
87      @Override
88      public Collection<org.eclipse.aether.scope.DependencyScope> buildDependencyScopes(
89              InternalScopeManager internalScopeManager) {
90          ArrayList<org.eclipse.aether.scope.DependencyScope> result = new ArrayList<>();
91          result.add(internalScopeManager.createDependencyScope(
92                  DependencyScope.COMPILE.id(), DependencyScope.COMPILE.isTransitive(), all()));
93          result.add(internalScopeManager.createDependencyScope(
94                  DependencyScope.RUNTIME.id(),
95                  DependencyScope.RUNTIME.isTransitive(),
96                  byBuildPath(CommonBuilds.BUILD_PATH_RUNTIME)));
97          result.add(internalScopeManager.createDependencyScope(
98                  DependencyScope.PROVIDED.id(),
99                  DependencyScope.PROVIDED.isTransitive(),
100                 union(
101                         byBuildPath(CommonBuilds.BUILD_PATH_COMPILE),
102                         select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME))));
103         result.add(internalScopeManager.createDependencyScope(
104                 DependencyScope.TEST.id(),
105                 DependencyScope.TEST.isTransitive(),
106                 byProjectPath(CommonBuilds.PROJECT_PATH_TEST)));
107         result.add(internalScopeManager.createDependencyScope(
108                 DependencyScope.NONE.id(), DependencyScope.NONE.isTransitive(), Collections.emptySet()));
109         result.add(internalScopeManager.createDependencyScope(
110                 DependencyScope.COMPILE_ONLY.id(),
111                 DependencyScope.COMPILE_ONLY.isTransitive(),
112                 singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_COMPILE)));
113         result.add(internalScopeManager.createDependencyScope(
114                 DependencyScope.TEST_RUNTIME.id(),
115                 DependencyScope.TEST_RUNTIME.isTransitive(),
116                 singleton(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME)));
117         result.add(internalScopeManager.createDependencyScope(
118                 DependencyScope.TEST_ONLY.id(),
119                 DependencyScope.TEST_ONLY.isTransitive(),
120                 singleton(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_COMPILE)));
121 
122         
123         result.add(internalScopeManager.createSystemDependencyScope(
124                 DependencyScope.SYSTEM.id(),
125                 DependencyScope.SYSTEM.isTransitive(),
126                 all(),
127                 MavenArtifactProperties.LOCAL_PATH));
128 
129         
130         if (result.size() != org.apache.maven.api.DependencyScope.values().length - 1) { 
131             throw new IllegalStateException("Maven4 API dependency scope mismatch");
132         }
133 
134         return result;
135     }
136 
137     @Override
138     public Collection<org.eclipse.aether.scope.ResolutionScope> buildResolutionScopes(
139             InternalScopeManager internalScopeManager) {
140         Collection<org.eclipse.aether.scope.DependencyScope> allDependencyScopes =
141                 internalScopeManager.getDependencyScopeUniverse();
142         Collection<org.eclipse.aether.scope.DependencyScope> nonTransitiveDependencyScopes =
143                 allDependencyScopes.stream().filter(s -> !s.isTransitive()).collect(Collectors.toSet());
144         org.eclipse.aether.scope.DependencyScope system = internalScopeManager
145                 .getDependencyScope(DependencyScope.SYSTEM.id())
146                 .orElse(null);
147 
148         ArrayList<org.eclipse.aether.scope.ResolutionScope> result = new ArrayList<>();
149         result.add(internalScopeManager.createResolutionScope(
150                 RS_NONE,
151                 InternalScopeManager.Mode.REMOVE,
152                 Collections.emptySet(),
153                 Collections.emptySet(),
154                 allDependencyScopes));
155         result.add(internalScopeManager.createResolutionScope(
156                 RS_MAIN_COMPILE,
157                 InternalScopeManager.Mode.ELIMINATE,
158                 singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_COMPILE),
159                 Collections.singletonList(system),
160                 nonTransitiveDependencyScopes));
161         result.add(internalScopeManager.createResolutionScope(
162                 RS_MAIN_COMPILE_PLUS_RUNTIME,
163                 InternalScopeManager.Mode.ELIMINATE,
164                 byProjectPath(CommonBuilds.PROJECT_PATH_MAIN),
165                 Collections.singletonList(system),
166                 nonTransitiveDependencyScopes));
167         result.add(internalScopeManager.createResolutionScope(
168                 RS_MAIN_RUNTIME,
169                 InternalScopeManager.Mode.REMOVE,
170                 singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_RUNTIME),
171                 Collections.emptySet(),
172                 nonTransitiveDependencyScopes));
173         result.add(internalScopeManager.createResolutionScope(
174                 RS_MAIN_RUNTIME_PLUS_SYSTEM,
175                 InternalScopeManager.Mode.REMOVE,
176                 singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_RUNTIME),
177                 Collections.singletonList(system),
178                 nonTransitiveDependencyScopes));
179         result.add(internalScopeManager.createResolutionScope(
180                 RS_TEST_COMPILE,
181                 InternalScopeManager.Mode.ELIMINATE,
182                 select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_COMPILE),
183                 Collections.singletonList(system),
184                 nonTransitiveDependencyScopes));
185         result.add(internalScopeManager.createResolutionScope(
186                 RS_TEST_RUNTIME,
187                 InternalScopeManager.Mode.ELIMINATE,
188                 select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME),
189                 Collections.singletonList(system),
190                 nonTransitiveDependencyScopes));
191         return result;
192     }
193 
194     
195 
196     public static void main(String... args) {
197         ScopeManagerDump.dump(Maven4ScopeManagerConfiguration.INSTANCE);
198     }
199 }