1 | 2 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
package com.jcabi.aether; |
31 | |
|
32 | |
import com.jcabi.aspects.Loggable; |
33 | |
import java.io.File; |
34 | |
import java.util.AbstractSet; |
35 | |
import java.util.Arrays; |
36 | |
import java.util.Collection; |
37 | |
import java.util.HashSet; |
38 | |
import java.util.Iterator; |
39 | |
import java.util.LinkedHashSet; |
40 | |
import java.util.LinkedList; |
41 | |
import java.util.Set; |
42 | |
import java.util.concurrent.TimeUnit; |
43 | |
import javax.validation.constraints.NotNull; |
44 | |
import lombok.EqualsAndHashCode; |
45 | |
import org.apache.commons.lang3.StringUtils; |
46 | |
import org.apache.maven.artifact.DependencyResolutionRequiredException; |
47 | |
import org.apache.maven.model.Dependency; |
48 | |
import org.apache.maven.project.MavenProject; |
49 | |
import org.sonatype.aether.artifact.Artifact; |
50 | |
import org.sonatype.aether.resolution.DependencyResolutionException; |
51 | |
import org.sonatype.aether.util.artifact.DefaultArtifact; |
52 | |
import org.sonatype.aether.util.artifact.JavaScopes; |
53 | |
import org.sonatype.aether.util.version.GenericVersionScheme; |
54 | |
import org.sonatype.aether.version.InvalidVersionSpecificationException; |
55 | |
import org.sonatype.aether.version.VersionScheme; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | 2 | @EqualsAndHashCode(callSuper = false, of = { "project", "aether", "scopes" }) |
81 | |
@Loggable( |
82 | |
value = Loggable.DEBUG, |
83 | |
limit = 1, unit = TimeUnit.MINUTES, |
84 | |
trim = false |
85 | |
) |
86 | |
@SuppressWarnings("PMD.TooManyMethods") |
87 | |
public final class Classpath extends AbstractSet<File> { |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
private final transient MavenProject project; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
private final transient Aether aether; |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
private final transient Set<String> scopes; |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public Classpath(@NotNull final MavenProject prj, |
111 | |
@NotNull final File repo, @NotNull final String scp) { |
112 | 5 | this(prj, repo, Arrays.asList(scp)); |
113 | 5 | } |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public Classpath(@NotNull final MavenProject prj, |
122 | |
@NotNull final File repo, @NotNull final Collection<String> scps) { |
123 | 5 | super(); |
124 | 5 | this.project = prj; |
125 | 5 | this.aether = new Aether(prj, repo); |
126 | 5 | this.scopes = new HashSet<String>(scps); |
127 | 5 | } |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
@Override |
133 | |
public String toString() { |
134 | 1 | return StringUtils.join(this.roots(), "\n"); |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
@Override |
141 | |
public Iterator<File> iterator() { |
142 | |
try { |
143 | 14 | return this.fetch().iterator(); |
144 | 0 | } catch (final DependencyResolutionException ex) { |
145 | 0 | throw new IllegalStateException(ex); |
146 | |
} |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
@Override |
153 | |
public int size() { |
154 | |
try { |
155 | 0 | return this.fetch().size(); |
156 | 0 | } catch (final DependencyResolutionException ex) { |
157 | 0 | throw new IllegalStateException(ex); |
158 | |
} |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") |
167 | |
private Set<File> fetch() throws DependencyResolutionException { |
168 | 7 | final Set<File> files = new LinkedHashSet<File>(0); |
169 | 7 | for (final String path : this.elements()) { |
170 | 5 | files.add(new File(path)); |
171 | 5 | } |
172 | 7 | for (final Artifact artifact : this.artifacts()) { |
173 | 20 | files.add(artifact.getFile()); |
174 | 20 | } |
175 | 7 | return files; |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
private Collection<String> elements() { |
183 | 7 | final Collection<String> elements = new LinkedList<String>(); |
184 | |
try { |
185 | 7 | if (this.scopes.contains(JavaScopes.TEST)) { |
186 | 5 | elements.addAll(this.project.getTestClasspathElements()); |
187 | |
} |
188 | 7 | if (this.scopes.contains(JavaScopes.RUNTIME)) { |
189 | 0 | elements.addAll(this.project.getRuntimeClasspathElements()); |
190 | |
} |
191 | 7 | if (this.scopes.contains(JavaScopes.SYSTEM)) { |
192 | 0 | elements.addAll(this.project.getSystemClasspathElements()); |
193 | |
} |
194 | 7 | if (this.scopes.contains(JavaScopes.COMPILE) |
195 | |
|| this.scopes.contains(JavaScopes.PROVIDED)) { |
196 | 2 | elements.addAll(this.project.getCompileClasspathElements()); |
197 | |
} |
198 | 0 | } catch (final DependencyResolutionRequiredException ex) { |
199 | 0 | throw new IllegalStateException("Failed to read classpath", ex); |
200 | 7 | } |
201 | 7 | return elements; |
202 | |
} |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
private Set<Artifact> artifacts() throws DependencyResolutionException { |
214 | 7 | final Set<Artifact> artifacts = new LinkedHashSet<Artifact>(0); |
215 | 7 | for (final RootArtifact root : this.roots()) { |
216 | 9 | for (final Artifact child : root.children()) { |
217 | 22 | if (Classpath.contains(child, artifacts)) { |
218 | 2 | final Artifact found = Classpath.find(child, artifacts); |
219 | 2 | if (found.getVersion().equals(child.getVersion())) { |
220 | 0 | continue; |
221 | |
} |
222 | 2 | final Artifact newer = Classpath.newer(child, found); |
223 | 2 | if (newer.equals(child)) { |
224 | 2 | artifacts.remove(found); |
225 | 2 | artifacts.add(newer); |
226 | |
} |
227 | |
} |
228 | 22 | if (root.excluded(child)) { |
229 | 0 | continue; |
230 | |
} |
231 | 22 | artifacts.add(child); |
232 | 22 | } |
233 | 9 | } |
234 | 7 | return artifacts; |
235 | |
} |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
private static Artifact newer(final Artifact child, final Artifact found) { |
244 | 2 | final VersionScheme scheme = new GenericVersionScheme(); |
245 | |
final Artifact newer; |
246 | |
try { |
247 | 2 | if (scheme.parseVersion(child.getVersion()) |
248 | |
.compareTo(scheme.parseVersion(found.getVersion())) < 0) { |
249 | 0 | newer = found; |
250 | |
} else { |
251 | 2 | newer = child; |
252 | |
} |
253 | 0 | } catch (final InvalidVersionSpecificationException ex) { |
254 | 0 | throw new IllegalStateException(ex); |
255 | 2 | } |
256 | 2 | return newer; |
257 | |
} |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
private Set<RootArtifact> roots() { |
269 | 8 | final Set<RootArtifact> roots = new LinkedHashSet<RootArtifact>(0); |
270 | 8 | for (final Dependency dep : this.project.getDependencies()) { |
271 | 10 | if (!this.scopes.contains(dep.getScope())) { |
272 | 0 | continue; |
273 | |
} |
274 | 10 | roots.add(this.root(dep)); |
275 | 10 | } |
276 | 8 | return roots; |
277 | |
} |
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
private RootArtifact root(final Dependency dep) { |
285 | 10 | return new RootArtifact( |
286 | |
this.aether, |
287 | |
new DefaultArtifact( |
288 | |
dep.getGroupId(), |
289 | |
dep.getArtifactId(), |
290 | |
dep.getClassifier(), |
291 | |
dep.getType(), |
292 | |
dep.getVersion() |
293 | |
), |
294 | |
dep.getExclusions() |
295 | |
); |
296 | |
} |
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
private static boolean contains(final Artifact artifact, |
305 | |
final Collection<Artifact> artifacts) { |
306 | 22 | boolean contains = false; |
307 | 22 | for (final Artifact exists : artifacts) { |
308 | 35 | if (artifact.getArtifactId().equals(exists.getArtifactId()) |
309 | |
&& artifact.getGroupId().equals(exists.getGroupId()) |
310 | |
&& artifact.getClassifier().equals(exists.getClassifier())) { |
311 | 2 | contains = true; |
312 | 2 | break; |
313 | |
} |
314 | 33 | } |
315 | 22 | return contains; |
316 | |
} |
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
private static Artifact find(final Artifact artifact, |
328 | |
final Collection<Artifact> artifacts) { |
329 | 2 | for (final Artifact exists : artifacts) { |
330 | 2 | if (artifact.getArtifactId().equals(exists.getArtifactId()) |
331 | |
&& artifact.getGroupId().equals(exists.getGroupId()) |
332 | |
&& artifact.getClassifier().equals(exists.getClassifier())) { |
333 | 2 | return exists; |
334 | |
} |
335 | 0 | } |
336 | 0 | throw new IllegalArgumentException("Artifact not found"); |
337 | |
} |
338 | |
} |
339 | |
|