|
1 | 1 | /* |
2 | | - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.cli.compiler.grape; |
18 | 18 |
|
19 | | -import java.io.File; |
20 | | -import java.net.MalformedURLException; |
21 | | -import java.net.URI; |
22 | | -import java.util.ArrayList; |
23 | | -import java.util.Collection; |
24 | | -import java.util.Collections; |
25 | 19 | import java.util.List; |
26 | | -import java.util.Map; |
27 | 20 |
|
28 | 21 | import groovy.grape.GrapeEngine; |
29 | 22 | import groovy.lang.GroovyClassLoader; |
30 | 23 | import org.eclipse.aether.DefaultRepositorySystemSession; |
31 | 24 | import org.eclipse.aether.RepositorySystem; |
32 | | -import org.eclipse.aether.artifact.Artifact; |
33 | | -import org.eclipse.aether.artifact.DefaultArtifact; |
34 | | -import org.eclipse.aether.collection.CollectRequest; |
35 | | -import org.eclipse.aether.graph.Dependency; |
36 | | -import org.eclipse.aether.graph.Exclusion; |
37 | 25 | import org.eclipse.aether.repository.RemoteRepository; |
38 | | -import org.eclipse.aether.resolution.ArtifactResolutionException; |
39 | | -import org.eclipse.aether.resolution.ArtifactResult; |
40 | | -import org.eclipse.aether.resolution.DependencyRequest; |
41 | | -import org.eclipse.aether.resolution.DependencyResult; |
42 | | -import org.eclipse.aether.util.artifact.JavaScopes; |
43 | | -import org.eclipse.aether.util.filter.DependencyFilterUtils; |
44 | 26 |
|
45 | 27 | /** |
46 | 28 | * A {@link GrapeEngine} implementation that uses |
|
50 | 32 | * @author Andy Wilkinson |
51 | 33 | * @author Phillip Webb |
52 | 34 | * @since 1.0.0 |
| 35 | + * @deprecated since 2.5.9 for removal in 2.8.0 in favor of |
| 36 | + * {@link MavenResolverGrapeEngine} |
53 | 37 | */ |
54 | | -@SuppressWarnings("rawtypes") |
55 | | -public class AetherGrapeEngine implements GrapeEngine { |
56 | | - |
57 | | - private static final Collection<Exclusion> WILDCARD_EXCLUSION; |
58 | | - |
59 | | - static { |
60 | | - List<Exclusion> exclusions = new ArrayList<>(); |
61 | | - exclusions.add(new Exclusion("*", "*", "*", "*")); |
62 | | - WILDCARD_EXCLUSION = Collections.unmodifiableList(exclusions); |
63 | | - } |
64 | | - |
65 | | - private final DependencyResolutionContext resolutionContext; |
66 | | - |
67 | | - private final ProgressReporter progressReporter; |
68 | | - |
69 | | - private final GroovyClassLoader classLoader; |
70 | | - |
71 | | - private final DefaultRepositorySystemSession session; |
72 | | - |
73 | | - private final RepositorySystem repositorySystem; |
74 | | - |
75 | | - private final List<RemoteRepository> repositories; |
| 38 | +@Deprecated |
| 39 | +public class AetherGrapeEngine extends MavenResolverGrapeEngine { |
76 | 40 |
|
77 | 41 | public AetherGrapeEngine(GroovyClassLoader classLoader, RepositorySystem repositorySystem, |
78 | 42 | DefaultRepositorySystemSession repositorySystemSession, List<RemoteRepository> remoteRepositories, |
79 | 43 | DependencyResolutionContext resolutionContext, boolean quiet) { |
80 | | - this.classLoader = classLoader; |
81 | | - this.repositorySystem = repositorySystem; |
82 | | - this.session = repositorySystemSession; |
83 | | - this.resolutionContext = resolutionContext; |
84 | | - this.repositories = new ArrayList<>(); |
85 | | - List<RemoteRepository> remotes = new ArrayList<>(remoteRepositories); |
86 | | - Collections.reverse(remotes); // priority is reversed in addRepository |
87 | | - for (RemoteRepository repository : remotes) { |
88 | | - addRepository(repository); |
89 | | - } |
90 | | - this.progressReporter = getProgressReporter(this.session, quiet); |
91 | | - } |
92 | | - |
93 | | - private ProgressReporter getProgressReporter(DefaultRepositorySystemSession session, boolean quiet) { |
94 | | - String progressReporter = (quiet ? "none" |
95 | | - : System.getProperty("org.springframework.boot.cli.compiler.grape.ProgressReporter")); |
96 | | - if ("detail".equals(progressReporter) || Boolean.getBoolean("groovy.grape.report.downloads")) { |
97 | | - return new DetailedProgressReporter(session, System.out); |
98 | | - } |
99 | | - if ("none".equals(progressReporter)) { |
100 | | - return () -> { |
101 | | - }; |
102 | | - } |
103 | | - return new SummaryProgressReporter(session, System.out); |
104 | | - } |
105 | | - |
106 | | - @Override |
107 | | - public Object grab(Map args) { |
108 | | - return grab(args, args); |
109 | | - } |
110 | | - |
111 | | - @Override |
112 | | - public Object grab(Map args, Map... dependencyMaps) { |
113 | | - List<Exclusion> exclusions = createExclusions(args); |
114 | | - List<Dependency> dependencies = createDependencies(dependencyMaps, exclusions); |
115 | | - try { |
116 | | - List<File> files = resolve(dependencies); |
117 | | - GroovyClassLoader classLoader = getClassLoader(args); |
118 | | - for (File file : files) { |
119 | | - classLoader.addURL(file.toURI().toURL()); |
120 | | - } |
121 | | - } |
122 | | - catch (ArtifactResolutionException | MalformedURLException ex) { |
123 | | - throw new DependencyResolutionFailedException(ex); |
124 | | - } |
125 | | - return null; |
126 | | - } |
127 | | - |
128 | | - @SuppressWarnings("unchecked") |
129 | | - private List<Exclusion> createExclusions(Map<?, ?> args) { |
130 | | - List<Exclusion> exclusions = new ArrayList<>(); |
131 | | - if (args != null) { |
132 | | - List<Map<String, Object>> exclusionMaps = (List<Map<String, Object>>) args.get("excludes"); |
133 | | - if (exclusionMaps != null) { |
134 | | - for (Map<String, Object> exclusionMap : exclusionMaps) { |
135 | | - exclusions.add(createExclusion(exclusionMap)); |
136 | | - } |
137 | | - } |
138 | | - } |
139 | | - return exclusions; |
140 | | - } |
141 | | - |
142 | | - private Exclusion createExclusion(Map<String, Object> exclusionMap) { |
143 | | - String group = (String) exclusionMap.get("group"); |
144 | | - String module = (String) exclusionMap.get("module"); |
145 | | - return new Exclusion(group, module, "*", "*"); |
146 | | - } |
147 | | - |
148 | | - private List<Dependency> createDependencies(Map<?, ?>[] dependencyMaps, List<Exclusion> exclusions) { |
149 | | - List<Dependency> dependencies = new ArrayList<>(dependencyMaps.length); |
150 | | - for (Map<?, ?> dependencyMap : dependencyMaps) { |
151 | | - dependencies.add(createDependency(dependencyMap, exclusions)); |
152 | | - } |
153 | | - return dependencies; |
154 | | - } |
155 | | - |
156 | | - private Dependency createDependency(Map<?, ?> dependencyMap, List<Exclusion> exclusions) { |
157 | | - Artifact artifact = createArtifact(dependencyMap); |
158 | | - if (isTransitive(dependencyMap)) { |
159 | | - return new Dependency(artifact, JavaScopes.COMPILE, false, exclusions); |
160 | | - } |
161 | | - return new Dependency(artifact, JavaScopes.COMPILE, null, WILDCARD_EXCLUSION); |
162 | | - } |
163 | | - |
164 | | - private Artifact createArtifact(Map<?, ?> dependencyMap) { |
165 | | - String group = (String) dependencyMap.get("group"); |
166 | | - String module = (String) dependencyMap.get("module"); |
167 | | - String version = (String) dependencyMap.get("version"); |
168 | | - if (version == null) { |
169 | | - version = this.resolutionContext.getManagedVersion(group, module); |
170 | | - } |
171 | | - String classifier = (String) dependencyMap.get("classifier"); |
172 | | - String type = determineType(dependencyMap); |
173 | | - return new DefaultArtifact(group, module, classifier, type, version); |
174 | | - } |
175 | | - |
176 | | - private String determineType(Map<?, ?> dependencyMap) { |
177 | | - String type = (String) dependencyMap.get("type"); |
178 | | - String ext = (String) dependencyMap.get("ext"); |
179 | | - if (type == null) { |
180 | | - type = ext; |
181 | | - if (type == null) { |
182 | | - type = "jar"; |
183 | | - } |
184 | | - } |
185 | | - else if (ext != null && !type.equals(ext)) { |
186 | | - throw new IllegalArgumentException("If both type and ext are specified they must have the same value"); |
187 | | - } |
188 | | - return type; |
189 | | - } |
190 | | - |
191 | | - private boolean isTransitive(Map<?, ?> dependencyMap) { |
192 | | - Boolean transitive = (Boolean) dependencyMap.get("transitive"); |
193 | | - return (transitive != null) ? transitive : true; |
194 | | - } |
195 | | - |
196 | | - private List<Dependency> getDependencies(DependencyResult dependencyResult) { |
197 | | - List<Dependency> dependencies = new ArrayList<>(); |
198 | | - for (ArtifactResult artifactResult : dependencyResult.getArtifactResults()) { |
199 | | - dependencies.add(new Dependency(artifactResult.getArtifact(), JavaScopes.COMPILE)); |
200 | | - } |
201 | | - return dependencies; |
202 | | - } |
203 | | - |
204 | | - private List<File> getFiles(DependencyResult dependencyResult) { |
205 | | - List<File> files = new ArrayList<>(); |
206 | | - for (ArtifactResult result : dependencyResult.getArtifactResults()) { |
207 | | - files.add(result.getArtifact().getFile()); |
208 | | - } |
209 | | - return files; |
210 | | - } |
211 | | - |
212 | | - private GroovyClassLoader getClassLoader(Map args) { |
213 | | - GroovyClassLoader classLoader = (GroovyClassLoader) args.get("classLoader"); |
214 | | - return (classLoader != null) ? classLoader : this.classLoader; |
215 | | - } |
216 | | - |
217 | | - @Override |
218 | | - public void addResolver(Map<String, Object> args) { |
219 | | - String name = (String) args.get("name"); |
220 | | - String root = (String) args.get("root"); |
221 | | - RemoteRepository.Builder builder = new RemoteRepository.Builder(name, "default", root); |
222 | | - RemoteRepository repository = builder.build(); |
223 | | - addRepository(repository); |
224 | | - } |
225 | | - |
226 | | - protected void addRepository(RemoteRepository repository) { |
227 | | - if (this.repositories.contains(repository)) { |
228 | | - return; |
229 | | - } |
230 | | - repository = getPossibleMirror(repository); |
231 | | - repository = applyProxy(repository); |
232 | | - repository = applyAuthentication(repository); |
233 | | - this.repositories.add(0, repository); |
234 | | - } |
235 | | - |
236 | | - private RemoteRepository getPossibleMirror(RemoteRepository remoteRepository) { |
237 | | - RemoteRepository mirror = this.session.getMirrorSelector().getMirror(remoteRepository); |
238 | | - if (mirror != null) { |
239 | | - return mirror; |
240 | | - } |
241 | | - return remoteRepository; |
242 | | - } |
243 | | - |
244 | | - private RemoteRepository applyProxy(RemoteRepository repository) { |
245 | | - if (repository.getProxy() == null) { |
246 | | - RemoteRepository.Builder builder = new RemoteRepository.Builder(repository); |
247 | | - builder.setProxy(this.session.getProxySelector().getProxy(repository)); |
248 | | - repository = builder.build(); |
249 | | - } |
250 | | - return repository; |
251 | | - } |
252 | | - |
253 | | - private RemoteRepository applyAuthentication(RemoteRepository repository) { |
254 | | - if (repository.getAuthentication() == null) { |
255 | | - RemoteRepository.Builder builder = new RemoteRepository.Builder(repository); |
256 | | - builder.setAuthentication(this.session.getAuthenticationSelector().getAuthentication(repository)); |
257 | | - repository = builder.build(); |
258 | | - } |
259 | | - return repository; |
260 | | - } |
261 | | - |
262 | | - @Override |
263 | | - public Map<String, Map<String, List<String>>> enumerateGrapes() { |
264 | | - throw new UnsupportedOperationException("Grape enumeration is not supported"); |
265 | | - } |
266 | | - |
267 | | - @Override |
268 | | - public URI[] resolve(Map args, Map... dependencyMaps) { |
269 | | - return resolve(args, null, dependencyMaps); |
270 | | - } |
271 | | - |
272 | | - @Override |
273 | | - public URI[] resolve(Map args, List depsInfo, Map... dependencyMaps) { |
274 | | - List<Exclusion> exclusions = createExclusions(args); |
275 | | - List<Dependency> dependencies = createDependencies(dependencyMaps, exclusions); |
276 | | - try { |
277 | | - List<File> files = resolve(dependencies); |
278 | | - List<URI> uris = new ArrayList<>(files.size()); |
279 | | - for (File file : files) { |
280 | | - uris.add(file.toURI()); |
281 | | - } |
282 | | - return uris.toArray(new URI[0]); |
283 | | - } |
284 | | - catch (Exception ex) { |
285 | | - throw new DependencyResolutionFailedException(ex); |
286 | | - } |
287 | | - } |
288 | | - |
289 | | - private List<File> resolve(List<Dependency> dependencies) throws ArtifactResolutionException { |
290 | | - try { |
291 | | - CollectRequest collectRequest = getCollectRequest(dependencies); |
292 | | - DependencyRequest dependencyRequest = getDependencyRequest(collectRequest); |
293 | | - DependencyResult result = this.repositorySystem.resolveDependencies(this.session, dependencyRequest); |
294 | | - addManagedDependencies(result); |
295 | | - return getFiles(result); |
296 | | - } |
297 | | - catch (Exception ex) { |
298 | | - throw new DependencyResolutionFailedException(ex); |
299 | | - } |
300 | | - finally { |
301 | | - this.progressReporter.finished(); |
302 | | - } |
303 | | - } |
304 | | - |
305 | | - private CollectRequest getCollectRequest(List<Dependency> dependencies) { |
306 | | - CollectRequest collectRequest = new CollectRequest((Dependency) null, dependencies, |
307 | | - new ArrayList<>(this.repositories)); |
308 | | - collectRequest.setManagedDependencies(this.resolutionContext.getManagedDependencies()); |
309 | | - return collectRequest; |
310 | | - } |
311 | | - |
312 | | - private DependencyRequest getDependencyRequest(CollectRequest collectRequest) { |
313 | | - return new DependencyRequest(collectRequest, |
314 | | - DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE, JavaScopes.RUNTIME)); |
315 | | - } |
316 | | - |
317 | | - private void addManagedDependencies(DependencyResult result) { |
318 | | - this.resolutionContext.addManagedDependencies(getDependencies(result)); |
319 | | - } |
320 | | - |
321 | | - @Override |
322 | | - public Map[] listDependencies(ClassLoader classLoader) { |
323 | | - throw new UnsupportedOperationException("Listing dependencies is not supported"); |
324 | | - } |
325 | | - |
326 | | - @Override |
327 | | - public Object grab(String endorsedModule) { |
328 | | - throw new UnsupportedOperationException("Grabbing an endorsed module is not supported"); |
| 44 | + super(classLoader, repositorySystem, repositorySystemSession, remoteRepositories, resolutionContext, quiet); |
329 | 45 | } |
330 | 46 |
|
331 | 47 | } |
0 commit comments