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   * @deprecated since 4.0.0, use {@code maven-api-impl} jar instead
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         // system
123         result.add(internalScopeManager.createSystemDependencyScope(
124                 DependencyScope.SYSTEM.id(),
125                 DependencyScope.SYSTEM.isTransitive(),
126                 all(),
127                 MavenArtifactProperties.LOCAL_PATH));
128 
129         // == sanity check
130         if (result.size() != org.apache.maven.api.DependencyScope.values().length - 1) { // sans "undefined"
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 }