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.impl.resolver.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.impl.resolver.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   * Maven3 scope configurations. Configures scope manager to support Maven3 scopes.
45   * <p>
46   * This manager supports the old Maven 3 dependency scopes.
47   *
48   * @since 2.0.0
49   */
50  public final class Maven3ScopeManagerConfiguration implements ScopeManagerConfiguration {
51      public static final Maven3ScopeManagerConfiguration INSTANCE = new Maven3ScopeManagerConfiguration();
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 Maven3ScopeManagerConfiguration() {}
62  
63      @Override
64      public String getId() {
65          return "Maven3";
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                  Collections.singletonList(CommonBuilds.PROJECT_PATH_MAIN),
82                  Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME),
83                  CommonBuilds.MAVEN_TEST_BUILD_SCOPE);
84      }
85  
86      @Override
87      public Collection<org.eclipse.aether.scope.DependencyScope> buildDependencyScopes(
88              InternalScopeManager internalScopeManager) {
89          ArrayList<org.eclipse.aether.scope.DependencyScope> result = new ArrayList<>();
90          result.add(internalScopeManager.createDependencyScope(DependencyScope.COMPILE.id(), true, all()));
91          result.add(internalScopeManager.createDependencyScope(
92                  DependencyScope.RUNTIME.id(), true, byBuildPath(CommonBuilds.BUILD_PATH_RUNTIME)));
93          result.add(internalScopeManager.createDependencyScope(
94                  DependencyScope.PROVIDED.id(),
95                  false,
96                  union(
97                          byBuildPath(CommonBuilds.BUILD_PATH_COMPILE),
98                          select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME))));
99          result.add(internalScopeManager.createDependencyScope(
100                 DependencyScope.TEST.id(), false, byProjectPath(CommonBuilds.PROJECT_PATH_TEST)));
101         result.add(internalScopeManager.createSystemDependencyScope(
102                 DependencyScope.SYSTEM.id(), false, all(), MavenArtifactProperties.LOCAL_PATH));
103         return result;
104     }
105 
106     @Override
107     public Collection<org.eclipse.aether.scope.ResolutionScope> buildResolutionScopes(
108             InternalScopeManager internalScopeManager) {
109         Collection<org.eclipse.aether.scope.DependencyScope> allDependencyScopes =
110                 internalScopeManager.getDependencyScopeUniverse();
111         Collection<org.eclipse.aether.scope.DependencyScope> nonTransitiveDependencyScopes =
112                 allDependencyScopes.stream().filter(s -> !s.isTransitive()).collect(Collectors.toSet());
113         org.eclipse.aether.scope.DependencyScope system = internalScopeManager
114                 .getDependencyScope(DependencyScope.SYSTEM.id())
115                 .orElse(null);
116 
117         ArrayList<org.eclipse.aether.scope.ResolutionScope> result = new ArrayList<>();
118         result.add(internalScopeManager.createResolutionScope(
119                 RS_NONE,
120                 InternalScopeManager.Mode.REMOVE,
121                 Collections.emptySet(),
122                 Collections.emptySet(),
123                 allDependencyScopes));
124         result.add(internalScopeManager.createResolutionScope(
125                 RS_MAIN_COMPILE,
126                 InternalScopeManager.Mode.ELIMINATE,
127                 singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_COMPILE),
128                 Collections.singletonList(system),
129                 nonTransitiveDependencyScopes));
130         result.add(internalScopeManager.createResolutionScope(
131                 RS_MAIN_COMPILE_PLUS_RUNTIME,
132                 InternalScopeManager.Mode.ELIMINATE,
133                 byProjectPath(CommonBuilds.PROJECT_PATH_MAIN),
134                 Collections.singletonList(system),
135                 nonTransitiveDependencyScopes));
136         result.add(internalScopeManager.createResolutionScope(
137                 RS_MAIN_RUNTIME,
138                 InternalScopeManager.Mode.ELIMINATE,
139                 singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_RUNTIME),
140                 Collections.emptySet(),
141                 nonTransitiveDependencyScopes));
142         result.add(internalScopeManager.createResolutionScope(
143                 RS_MAIN_RUNTIME_PLUS_SYSTEM,
144                 InternalScopeManager.Mode.ELIMINATE,
145                 singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_RUNTIME),
146                 Collections.singletonList(system),
147                 nonTransitiveDependencyScopes));
148         result.add(internalScopeManager.createResolutionScope(
149                 RS_TEST_COMPILE,
150                 InternalScopeManager.Mode.ELIMINATE,
151                 select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_COMPILE),
152                 Collections.singletonList(system),
153                 nonTransitiveDependencyScopes));
154         result.add(internalScopeManager.createResolutionScope(
155                 RS_TEST_RUNTIME,
156                 InternalScopeManager.Mode.ELIMINATE,
157                 select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME),
158                 Collections.singletonList(system),
159                 nonTransitiveDependencyScopes));
160         return result;
161     }
162 
163     // ===
164 
165     public static void main(String... args) {
166         ScopeManagerDump.dump(Maven3ScopeManagerConfiguration.INSTANCE);
167     }
168 }