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