1 | |
package org.apache.maven.plugin.dependency; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Arrays; |
26 | |
import java.util.Collections; |
27 | |
import java.util.HashMap; |
28 | |
import java.util.HashSet; |
29 | |
import java.util.Iterator; |
30 | |
import java.util.List; |
31 | |
import java.util.Map; |
32 | |
import java.util.Set; |
33 | |
|
34 | |
import org.apache.maven.artifact.Artifact; |
35 | |
import org.apache.maven.artifact.ArtifactUtils; |
36 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
37 | |
import org.apache.maven.artifact.metadata.ArtifactMetadataSource; |
38 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
39 | |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
40 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
41 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionResult; |
42 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
43 | |
import org.apache.maven.artifact.versioning.VersionRange; |
44 | |
import org.apache.maven.model.Dependency; |
45 | |
import org.apache.maven.plugin.AbstractMojo; |
46 | |
import org.apache.maven.plugin.MojoExecutionException; |
47 | |
import org.apache.maven.plugin.MojoFailureException; |
48 | |
import org.apache.maven.project.MavenProject; |
49 | |
import org.codehaus.plexus.util.FileUtils; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | 0 | public class PurgeLocalRepositoryMojo |
63 | |
extends AbstractMojo |
64 | |
{ |
65 | |
|
66 | |
public static final String FILE_FUZZINESS = "file"; |
67 | |
|
68 | |
public static final String VERSION_FUZZINESS = "version"; |
69 | |
|
70 | |
public static final String ARTIFACT_ID_FUZZINESS = "artifactId"; |
71 | |
|
72 | |
public static final String GROUP_ID_FUZZINESS = "groupId"; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
private List projects; |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
private List excludes; |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
private String exclude; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
private boolean reResolve; |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
private ArtifactRepository localRepository; |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
private ArtifactResolver resolver; |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
private ArtifactMetadataSource source; |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
private String resolutionFuzziness; |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
private boolean actTransitively; |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
private ArtifactFactory factory; |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
private boolean verbose; |
173 | |
|
174 | |
public void execute() |
175 | |
throws MojoExecutionException, MojoFailureException |
176 | |
{ |
177 | 0 | List exclusionPatterns = buildExclusionPatternsList(); |
178 | |
|
179 | 0 | for ( Iterator it = projects.iterator(); it.hasNext(); ) |
180 | |
{ |
181 | 0 | MavenProject project = (MavenProject) it.next(); |
182 | |
|
183 | |
try |
184 | |
{ |
185 | 0 | refreshDependenciesForProject( project, exclusionPatterns ); |
186 | |
} |
187 | 0 | catch ( ArtifactResolutionException e ) |
188 | |
{ |
189 | 0 | MojoFailureException failure = new MojoFailureException( this, |
190 | |
"Failed to refresh project dependencies for: " |
191 | |
+ project.getId(), |
192 | |
"Artifact resolution failed for project: " |
193 | |
+ project.getId() ); |
194 | 0 | failure.initCause( e ); |
195 | |
|
196 | 0 | throw failure; |
197 | 0 | } |
198 | 0 | } |
199 | 0 | } |
200 | |
|
201 | |
private List buildExclusionPatternsList() |
202 | |
{ |
203 | 0 | List patterns = new ArrayList(); |
204 | |
|
205 | 0 | if ( exclude != null ) |
206 | |
{ |
207 | 0 | String[] elements = exclude.split( " ?, ?" ); |
208 | |
|
209 | 0 | patterns.addAll( Arrays.asList( elements ) ); |
210 | 0 | } |
211 | 0 | else if ( excludes != null && !excludes.isEmpty() ) |
212 | |
{ |
213 | 0 | patterns.addAll( excludes ); |
214 | |
} |
215 | |
|
216 | 0 | return patterns; |
217 | |
} |
218 | |
|
219 | |
private Map createArtifactMap( MavenProject project ) |
220 | |
{ |
221 | 0 | Map artifactMap = Collections.EMPTY_MAP; |
222 | |
|
223 | 0 | List dependencies = project.getDependencies(); |
224 | |
|
225 | 0 | List remoteRepositories = Collections.EMPTY_LIST; |
226 | |
|
227 | 0 | Set dependencyArtifacts = new HashSet(); |
228 | |
|
229 | 0 | for ( Iterator it = dependencies.iterator(); it.hasNext(); ) |
230 | |
{ |
231 | 0 | Dependency dependency = (Dependency) it.next(); |
232 | |
|
233 | 0 | VersionRange vr = VersionRange.createFromVersion( dependency.getVersion() ); |
234 | |
|
235 | 0 | Artifact artifact = factory.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(), |
236 | |
vr, dependency.getType(), dependency.getClassifier(), |
237 | |
dependency.getScope() ); |
238 | 0 | dependencyArtifacts.add( artifact ); |
239 | 0 | } |
240 | |
|
241 | 0 | if ( actTransitively ) |
242 | |
{ |
243 | |
try |
244 | |
{ |
245 | 0 | ArtifactResolutionResult result = resolver.resolveTransitively( dependencyArtifacts, project |
246 | |
.getArtifact(), remoteRepositories, localRepository, source ); |
247 | |
|
248 | 0 | artifactMap = ArtifactUtils.artifactMapByVersionlessId( result.getArtifacts() ); |
249 | |
} |
250 | 0 | catch ( ArtifactResolutionException e ) |
251 | |
{ |
252 | 0 | verbose( "Skipping: " + e.getArtifactId() + ". It cannot be resolved." ); |
253 | |
} |
254 | 0 | catch ( ArtifactNotFoundException e ) |
255 | |
{ |
256 | 0 | verbose( "Skipping: " + e.getArtifactId() + ". It cannot be resolved." ); |
257 | 0 | } |
258 | |
} |
259 | |
else |
260 | |
{ |
261 | 0 | artifactMap = new HashMap(); |
262 | 0 | for ( Iterator it = dependencyArtifacts.iterator(); it.hasNext(); ) |
263 | |
{ |
264 | 0 | Artifact artifact = (Artifact) it.next(); |
265 | |
|
266 | |
try |
267 | |
{ |
268 | 0 | resolver.resolve( artifact, remoteRepositories, localRepository ); |
269 | |
|
270 | 0 | artifactMap.put( ArtifactUtils.versionlessKey( artifact ), artifact ); |
271 | |
} |
272 | 0 | catch ( ArtifactResolutionException e ) |
273 | |
{ |
274 | 0 | verbose( "Skipping: " + e.getArtifactId() + ". It cannot be resolved." ); |
275 | |
} |
276 | 0 | catch ( ArtifactNotFoundException e ) |
277 | |
{ |
278 | 0 | verbose( "Skipping: " + e.getArtifactId() + ". It cannot be resolved." ); |
279 | 0 | } |
280 | 0 | } |
281 | |
} |
282 | |
|
283 | 0 | return artifactMap; |
284 | |
} |
285 | |
|
286 | |
private void verbose( String message ) |
287 | |
{ |
288 | 0 | if ( verbose || getLog().isDebugEnabled() ) |
289 | |
{ |
290 | 0 | getLog().info( message ); |
291 | |
} |
292 | 0 | } |
293 | |
|
294 | |
private void refreshDependenciesForProject( MavenProject project, List exclusionPatterns ) |
295 | |
throws ArtifactResolutionException, MojoFailureException |
296 | |
{ |
297 | 0 | Map deps = createArtifactMap( project ); |
298 | |
|
299 | 0 | if ( deps.isEmpty() ) |
300 | |
{ |
301 | 0 | getLog().info( "Nothing to do for project: " + project.getId() ); |
302 | 0 | return; |
303 | |
} |
304 | |
|
305 | 0 | if ( !exclusionPatterns.isEmpty() ) |
306 | |
{ |
307 | 0 | for ( Iterator it = exclusionPatterns.iterator(); it.hasNext(); ) |
308 | |
{ |
309 | 0 | String excludedKey = (String) it.next(); |
310 | |
|
311 | 0 | verbose( "Excluding: " + excludedKey + " from refresh operation for project: " + project.getId() ); |
312 | |
|
313 | 0 | deps.remove( excludedKey ); |
314 | 0 | } |
315 | |
} |
316 | |
|
317 | 0 | verbose( "Processing dependencies for project: " + project.getId() ); |
318 | |
|
319 | 0 | List missingArtifacts = new ArrayList(); |
320 | 0 | for ( Iterator it = deps.entrySet().iterator(); it.hasNext(); ) |
321 | |
{ |
322 | 0 | Map.Entry entry = (Map.Entry) it.next(); |
323 | |
|
324 | 0 | Artifact artifact = (Artifact) entry.getValue(); |
325 | |
|
326 | 0 | verbose( "Processing artifact: " + artifact.getId() ); |
327 | |
|
328 | 0 | File deleteTarget = findDeleteTarget( artifact ); |
329 | |
|
330 | 0 | verbose( "Deleting: " + deleteTarget ); |
331 | |
|
332 | 0 | if ( deleteTarget.isDirectory() ) |
333 | |
{ |
334 | |
try |
335 | |
{ |
336 | 0 | FileUtils.deleteDirectory( deleteTarget ); |
337 | |
} |
338 | 0 | catch ( IOException e ) |
339 | |
{ |
340 | 0 | throw new MojoFailureException( this, "Cannot delete dependency from the local repository: " |
341 | |
+ artifact.getId(), "Failed to delete: " + deleteTarget ); |
342 | 0 | } |
343 | |
} |
344 | |
else |
345 | |
{ |
346 | 0 | deleteTarget.delete(); |
347 | |
} |
348 | |
|
349 | 0 | if ( reResolve ) |
350 | |
{ |
351 | 0 | verbose( "Re-resolving." ); |
352 | |
|
353 | 0 | artifact.setResolved( false ); |
354 | |
|
355 | |
try |
356 | |
{ |
357 | 0 | resolver.resolveAlways( artifact, project.getRemoteArtifactRepositories(), localRepository ); |
358 | |
} |
359 | 0 | catch ( ArtifactResolutionException e ) |
360 | |
{ |
361 | 0 | getLog().debug( e.getMessage() ); |
362 | 0 | missingArtifacts.add( artifact ); |
363 | |
} |
364 | 0 | catch ( ArtifactNotFoundException e ) |
365 | |
{ |
366 | 0 | getLog().debug( e.getMessage() ); |
367 | 0 | missingArtifacts.add( artifact ); |
368 | 0 | } |
369 | |
} |
370 | 0 | } |
371 | |
|
372 | 0 | if ( missingArtifacts.size() > 0 ) |
373 | |
{ |
374 | 0 | String message = "required artifacts missing:\n"; |
375 | 0 | for ( Iterator i = missingArtifacts.iterator(); i.hasNext(); ) |
376 | |
{ |
377 | 0 | Artifact missingArtifact = (Artifact) i.next(); |
378 | 0 | message += " " + missingArtifact.getId() + "\n"; |
379 | 0 | } |
380 | 0 | message += "\nfor the artifact:"; |
381 | |
|
382 | 0 | throw new ArtifactResolutionException( message, project.getArtifact(), project |
383 | |
.getRemoteArtifactRepositories() ); |
384 | |
} |
385 | |
|
386 | 0 | } |
387 | |
|
388 | |
private File findDeleteTarget( Artifact artifact ) |
389 | |
{ |
390 | 0 | File deleteTarget = artifact.getFile(); |
391 | |
|
392 | 0 | if ( GROUP_ID_FUZZINESS.equals( resolutionFuzziness ) ) |
393 | |
{ |
394 | |
|
395 | 0 | deleteTarget = deleteTarget.getParentFile().getParentFile(); |
396 | |
|
397 | |
|
398 | 0 | deleteTarget = deleteTarget.getParentFile(); |
399 | |
|
400 | 0 | String[] path = localRepository.pathOf( artifact ).split( "\\/" ); |
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | 0 | int groupParts = path.length - 4; |
406 | |
|
407 | 0 | File parent = deleteTarget.getParentFile(); |
408 | 0 | int count = 0; |
409 | 0 | while ( count++ < groupParts ) |
410 | |
{ |
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | 0 | if ( parent.list().length < 2 ) |
417 | |
{ |
418 | 0 | deleteTarget = parent; |
419 | |
|
420 | |
|
421 | 0 | parent = deleteTarget.getParentFile(); |
422 | |
} |
423 | |
else |
424 | |
{ |
425 | |
|
426 | |
|
427 | |
break; |
428 | |
} |
429 | |
} |
430 | |
|
431 | 0 | } |
432 | 0 | else if ( ARTIFACT_ID_FUZZINESS.equals( resolutionFuzziness ) ) |
433 | |
{ |
434 | |
|
435 | 0 | deleteTarget = deleteTarget.getParentFile().getParentFile(); |
436 | |
} |
437 | 0 | else if ( VERSION_FUZZINESS.equals( resolutionFuzziness ) ) |
438 | |
{ |
439 | |
|
440 | 0 | deleteTarget = deleteTarget.getParentFile(); |
441 | |
} |
442 | |
|
443 | |
|
444 | 0 | return deleteTarget; |
445 | |
} |
446 | |
|
447 | |
} |