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.Artifact; |
47 | |
import org.apache.maven.artifact.DefaultArtifact; |
48 | |
import org.apache.maven.artifact.DependencyResolutionRequiredException; |
49 | |
import org.apache.maven.artifact.handler.DefaultArtifactHandler; |
50 | |
import org.apache.maven.artifact.resolver.filter.ArtifactFilter; |
51 | |
import org.apache.maven.execution.MavenSession; |
52 | |
import org.apache.maven.model.Dependency; |
53 | |
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder; |
54 | |
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException; |
55 | |
import org.apache.maven.shared.dependency.graph.DependencyNode; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 2 | @EqualsAndHashCode(callSuper = false, of = { "builder", "scopes" }) |
65 | |
@Loggable( |
66 | |
value = Loggable.DEBUG, |
67 | |
limit = 1, unit = TimeUnit.MINUTES, |
68 | |
trim = false |
69 | |
) |
70 | 0 | public final class MavenClasspath extends AbstractSet<File> { |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public static final String TEST_SCOPE = "test"; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public static final String RUNTIME_SCOPE = "runtime"; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public static final String SYSTEM_SCOPE = "system"; |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public static final String COMPILE_SCOPE = "compile"; |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public static final String PROVIDED_SCOPE = "provided"; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
private final transient Set<String> scopes; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private final transient DependencyGraphBuilder builder; |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
private final transient MavenSession session; |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public MavenClasspath(@NotNull final DependencyGraphBuilder bldr, |
119 | |
@NotNull final MavenSession sess, |
120 | |
@NotNull final String scp) { |
121 | 2 | this(bldr, sess, Arrays.asList(scp)); |
122 | 2 | } |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public MavenClasspath(@NotNull final DependencyGraphBuilder bldr, |
131 | |
@NotNull final MavenSession sess, |
132 | |
@NotNull final Collection<String> scps) { |
133 | 2 | super(); |
134 | 2 | this.builder = bldr; |
135 | 2 | this.session = sess; |
136 | 2 | this.scopes = new HashSet<String>(scps); |
137 | 2 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
@Override |
143 | |
public String toString() { |
144 | 0 | return StringUtils.join(this.roots(), "\n"); |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
@Override |
151 | |
public Iterator<File> iterator() { |
152 | 4 | return this.fetch().iterator(); |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
@Override |
159 | |
public int size() { |
160 | 0 | return this.fetch().size(); |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") |
168 | |
private Set<File> fetch() { |
169 | 2 | final Set<File> files = new LinkedHashSet<File>(0); |
170 | 2 | for (final String path : this.elements()) { |
171 | 2 | files.add(new File(path)); |
172 | 2 | } |
173 | |
try { |
174 | 2 | files.addAll(this.dependencies(this.graph(), this.scopes)); |
175 | 0 | } catch (final DependencyGraphBuilderException ex) { |
176 | 0 | throw new IllegalStateException(ex); |
177 | 2 | } |
178 | 2 | return files; |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
private DependencyNode graph() throws DependencyGraphBuilderException { |
187 | 2 | return this.builder.buildDependencyGraph( |
188 | |
this.session.getCurrentProject(), |
189 | 2 | new ArtifactFilter() { |
190 | |
@Override |
191 | |
public boolean include( |
192 | |
final Artifact artifact) { |
193 | 0 | return MavenClasspath.this.scopes |
194 | |
.contains(artifact.getScope()); |
195 | |
} |
196 | |
} |
197 | |
); |
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
private Set<MavenRootArtifact> roots() { |
210 | 0 | final Set<MavenRootArtifact> roots = |
211 | |
new LinkedHashSet<MavenRootArtifact>(0); |
212 | |
for (final Dependency dep |
213 | 0 | : this.session.getCurrentProject().getDependencies()) { |
214 | 0 | if (!this.scopes.contains(dep.getScope())) { |
215 | 0 | continue; |
216 | |
} |
217 | 0 | roots.add(this.root(dep)); |
218 | 0 | } |
219 | 0 | return roots; |
220 | |
} |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
private MavenRootArtifact root(final Dependency dep) { |
228 | 0 | final DefaultArtifact artifact = new DefaultArtifact( |
229 | |
dep.getGroupId(), |
230 | |
dep.getArtifactId(), |
231 | |
dep.getVersion(), |
232 | |
dep.getScope(), |
233 | |
dep.getType(), |
234 | |
dep.getClassifier(), |
235 | |
new DefaultArtifactHandler() |
236 | |
); |
237 | |
try { |
238 | 0 | final Collection<Artifact> children = new LinkedList<Artifact>(); |
239 | 0 | for (final DependencyNode child : this.graph().getChildren()) { |
240 | 0 | children.add(child.getArtifact()); |
241 | 0 | } |
242 | 0 | return new MavenRootArtifact( |
243 | |
artifact, |
244 | |
dep.getExclusions(), |
245 | |
children |
246 | |
); |
247 | 0 | } catch (final DependencyGraphBuilderException ex) { |
248 | 0 | throw new IllegalStateException(ex); |
249 | |
} |
250 | |
} |
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
private Collection<String> elements() { |
257 | 2 | final Collection<String> elements = new LinkedList<String>(); |
258 | |
try { |
259 | 2 | if (this.scopes.contains(TEST_SCOPE)) { |
260 | 2 | elements.addAll( |
261 | |
this.session.getCurrentProject().getTestClasspathElements() |
262 | |
); |
263 | |
} |
264 | 2 | if (this.scopes.contains(RUNTIME_SCOPE)) { |
265 | 0 | elements.addAll( |
266 | |
this.session.getCurrentProject() |
267 | |
.getRuntimeClasspathElements() |
268 | |
); |
269 | |
} |
270 | 2 | if (this.scopes.contains(SYSTEM_SCOPE)) { |
271 | 0 | elements.addAll( |
272 | |
this.session.getCurrentProject() |
273 | |
.getSystemClasspathElements() |
274 | |
); |
275 | |
} |
276 | 2 | if (this.scopes.contains(COMPILE_SCOPE) |
277 | |
|| this.scopes.contains(PROVIDED_SCOPE)) { |
278 | 0 | elements.addAll( |
279 | |
this.session.getCurrentProject() |
280 | |
.getCompileClasspathElements() |
281 | |
); |
282 | |
} |
283 | 0 | } catch (final DependencyResolutionRequiredException ex) { |
284 | 0 | throw new IllegalStateException("Failed to read classpath", ex); |
285 | 2 | } |
286 | 2 | return elements; |
287 | |
} |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
private Collection<File> dependencies(final DependencyNode node, |
296 | |
final Collection<String> scps) { |
297 | 2 | final Artifact artifact = node.getArtifact(); |
298 | 2 | final Collection<File> files = new LinkedList<File>(); |
299 | 2 | if ((artifact.getScope() == null) |
300 | |
|| scps.contains(artifact.getScope())) { |
301 | 2 | if (artifact.getScope() == null) { |
302 | 2 | files.add(artifact.getFile()); |
303 | |
} else { |
304 | 0 | files.add( |
305 | |
this.session.getLocalRepository().find(artifact).getFile() |
306 | |
); |
307 | |
} |
308 | 2 | for (final DependencyNode child : node.getChildren()) { |
309 | 0 | if (child.getArtifact().compareTo(node.getArtifact()) != 0) { |
310 | 0 | files.addAll(this.dependencies(child, scps)); |
311 | |
} |
312 | 0 | } |
313 | |
} |
314 | 2 | return files; |
315 | |
} |
316 | |
} |
317 | |
|