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.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   * Maven3 scope configurations. Configures scope manager to support Maven3 scopes.
46   * <p>
47   * This manager supports the old Maven 3 dependency scopes.
48   *
49   * @since 2.0.0
50   */
51  public final class Maven3ScopeManagerConfiguration implements ScopeManagerConfiguration {
52      public static final Maven3ScopeManagerConfiguration INSTANCE = new Maven3ScopeManagerConfiguration();
53      public static final String DS_COMPILE = "compile";
54      public static final String DS_RUNTIME = "runtime";
55      public static final String DS_PROVIDED = "provided";
56      public static final String DS_SYSTEM = "system";
57      public static final String DS_TEST = "test";
58      public static final String RS_NONE = "none";
59      public static final String RS_MAIN_COMPILE = "main-compile";
60      public static final String RS_MAIN_COMPILE_PLUS_RUNTIME = "main-compilePlusRuntime";
61      public static final String RS_MAIN_RUNTIME = "main-runtime";
62      public static final String RS_MAIN_RUNTIME_PLUS_SYSTEM = "main-runtimePlusSystem";
63      public static final String RS_TEST_COMPILE = "test-compile";
64      public static final String RS_TEST_RUNTIME = "test-runtime";
65  
66      private Maven3ScopeManagerConfiguration() {}
67  
68      @Override
69      public String getId() {
70          return "Maven3";
71      }
72  
73      @Override
74      public boolean isStrictDependencyScopes() {
75          return false;
76      }
77  
78      @Override
79      public boolean isStrictResolutionScopes() {
80          return false;
81      }
82  
83      @Override
84      public BuildScopeSource getBuildScopeSource() {
85          return new BuildScopeMatrixSource(
86                  Collections.singletonList(CommonBuilds.PROJECT_PATH_MAIN),
87                  Arrays.asList(CommonBuilds.BUILD_PATH_COMPILE, CommonBuilds.BUILD_PATH_RUNTIME),
88                  CommonBuilds.MAVEN_TEST_BUILD_SCOPE);
89      }
90  
91      @Override
92      public Collection<DependencyScope> buildDependencyScopes(InternalScopeManager internalScopeManager) {
93          ArrayList<DependencyScope> result = new ArrayList<>();
94          result.add(internalScopeManager.createDependencyScope(DS_COMPILE, true, all()));
95          result.add(internalScopeManager.createDependencyScope(
96                  DS_RUNTIME, true, byBuildPath(CommonBuilds.BUILD_PATH_RUNTIME)));
97          result.add(internalScopeManager.createDependencyScope(
98                  DS_PROVIDED,
99                  false,
100                 union(
101                         byBuildPath(CommonBuilds.BUILD_PATH_COMPILE),
102                         select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME))));
103         result.add(internalScopeManager.createDependencyScope(
104                 DS_TEST, false, byProjectPath(CommonBuilds.PROJECT_PATH_TEST)));
105         result.add(internalScopeManager.createSystemDependencyScope(
106                 DS_SYSTEM, false, all(), ArtifactProperties.LOCAL_PATH));
107         return result;
108     }
109 
110     @Override
111     public Collection<ResolutionScope> buildResolutionScopes(InternalScopeManager internalScopeManager) {
112         Collection<DependencyScope> allDependencyScopes = internalScopeManager.getDependencyScopeUniverse();
113         Collection<DependencyScope> nonTransitiveDependencyScopes =
114                 allDependencyScopes.stream().filter(s -> !s.isTransitive()).collect(Collectors.toSet());
115         DependencyScope system =
116                 internalScopeManager.getDependencyScope(DS_SYSTEM).orElse(null);
117 
118         ArrayList<ResolutionScope> result = new ArrayList<>();
119         result.add(internalScopeManager.createResolutionScope(
120                 RS_NONE,
121                 InternalScopeManager.Mode.REMOVE,
122                 Collections.emptySet(),
123                 Collections.emptySet(),
124                 allDependencyScopes));
125         result.add(internalScopeManager.createResolutionScope(
126                 RS_MAIN_COMPILE,
127                 InternalScopeManager.Mode.ELIMINATE,
128                 singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_COMPILE),
129                 Collections.singletonList(system),
130                 nonTransitiveDependencyScopes));
131         result.add(internalScopeManager.createResolutionScope(
132                 RS_MAIN_COMPILE_PLUS_RUNTIME,
133                 InternalScopeManager.Mode.ELIMINATE,
134                 byProjectPath(CommonBuilds.PROJECT_PATH_MAIN),
135                 Collections.singletonList(system),
136                 nonTransitiveDependencyScopes));
137         result.add(internalScopeManager.createResolutionScope(
138                 RS_MAIN_RUNTIME,
139                 InternalScopeManager.Mode.ELIMINATE,
140                 singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_RUNTIME),
141                 Collections.emptySet(),
142                 nonTransitiveDependencyScopes));
143         result.add(internalScopeManager.createResolutionScope(
144                 RS_MAIN_RUNTIME_PLUS_SYSTEM,
145                 InternalScopeManager.Mode.ELIMINATE,
146                 singleton(CommonBuilds.PROJECT_PATH_MAIN, CommonBuilds.BUILD_PATH_RUNTIME),
147                 Collections.singletonList(system),
148                 nonTransitiveDependencyScopes));
149         result.add(internalScopeManager.createResolutionScope(
150                 RS_TEST_COMPILE,
151                 InternalScopeManager.Mode.ELIMINATE,
152                 select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_COMPILE),
153                 Collections.singletonList(system),
154                 nonTransitiveDependencyScopes));
155         result.add(internalScopeManager.createResolutionScope(
156                 RS_TEST_RUNTIME,
157                 InternalScopeManager.Mode.ELIMINATE,
158                 select(CommonBuilds.PROJECT_PATH_TEST, CommonBuilds.BUILD_PATH_RUNTIME),
159                 Collections.singletonList(system),
160                 nonTransitiveDependencyScopes));
161         return result;
162     }
163 
164     // ===
165 
166     public static void main(String... args) {
167         ScopeManagerDump.dump(Maven3ScopeManagerConfiguration.INSTANCE);
168     }
169 }