View Javadoc

1   package org.apache.maven.plugin.dependency;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
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   * Remove the project dependencies from the local repository, and optionally
53   * re-resolve them.
54   * 
55   * @author jdcasey
56   * @since 2.0
57   * @goal purge-local-repository
58   * @aggregator
59   * @version $Id: PurgeLocalRepositoryMojo.java 1085777 2011-03-26 18:13:19Z hboutemy $
60   * 
61   */
62  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       * The projects in the current build. Each of these is subject to
76       * refreshing.
77       * 
78       * @parameter default-value="${reactorProjects}"
79       * @required
80       * @readonly
81       */
82      private List<MavenProject> projects;
83  
84      /**
85       * The list of dependencies in the form of groupId:artifactId which should
86       * NOT be deleted/refreshed. This is useful for third-party artifacts.
87       * 
88       * @parameter
89       */
90      private List<String> excludes;
91  
92      /**
93       * Comma-separated list of groupId:artifactId entries, which should be used
94       * to exclude artifacts from deletion/refresh. This is a command-line
95       * alternative to the <code>excludes</code> parameter, since List
96       * parameters are not currently compatible with CLI specification.
97       * 
98       * @parameter expression="${exclude}"
99       */
100     private String exclude;
101 
102     /**
103      * Whether to re-resolve the artifacts once they have been deleted from the
104      * local repository. If you are running this mojo from the command-line, you
105      * may want to disable this. By default, artifacts will be re-resolved.
106      * 
107      * @parameter expression="${reResolve}" default-value="true"
108      */
109     private boolean reResolve;
110 
111     /**
112      * The local repository, from which to delete artifacts.
113      * 
114      * @parameter default-value="${localRepository}"
115      * @required
116      * @readonly
117      */
118     private ArtifactRepository localRepository;
119 
120     /**
121      * The artifact resolver used to re-resolve dependencies, if that option is
122      * enabled.
123      * 
124      * @component
125      */
126     private ArtifactResolver resolver;
127 
128     /**
129      * The artifact metadata source used to resolve dependencies
130      * 
131      * @component
132      */
133     private ArtifactMetadataSource source;
134 
135     /**
136      * Determines how liberally the plugin will delete an artifact from the
137      * local repository. Values are: <br/>
138      * <ul>
139      * <li><b>file</b> <i>(default)</i> - Eliminate only the artifact's file.</li>
140      * <li><b>version</b> - Eliminate all files associated with the artifact's
141      * version.</li>
142      * <li><b>artifactId</b> - Eliminate all files associated with the
143      * artifact's artifactId.</li>
144      * <li><b>groupId</b> - Eliminate all files associated with the artifact's
145      * groupId.</li>
146      * </ul>
147      * 
148      * @parameter expression="${resolutionFuzziness}" default-value="file"
149      */
150     private String resolutionFuzziness;
151 
152     /**
153      * Whether this mojo should act on all transitive dependencies. Default
154      * value is true.
155      * 
156      * @parameter expression="${actTransitively}" default-value="true"
157      */
158     private boolean actTransitively;
159 
160     /**
161      * Used to construct artifacts for deletion/resolution...
162      * 
163      * @component
164      */
165     private ArtifactFactory factory;
166 
167     /**
168      * Whether this plugin should output verbose messages. Default is false.
169      * 
170      * @parameter expression="${verbose}" default-value="false"
171      */
172     private boolean verbose;
173 
174     public void execute()
175         throws MojoExecutionException, MojoFailureException
176     {
177         List<String> exclusionPatterns = buildExclusionPatternsList();
178 
179         for ( MavenProject project : projects )
180         {
181             try
182             {
183                 refreshDependenciesForProject( project, exclusionPatterns );
184             }
185             catch ( ArtifactResolutionException e )
186             {
187                 MojoFailureException failure = new MojoFailureException( this,
188                                                                          "Failed to refresh project dependencies for: "
189                                                                              + project.getId(),
190                                                                          "Artifact resolution failed for project: "
191                                                                              + project.getId() );
192                 failure.initCause( e );
193 
194                 throw failure;
195             }
196         }
197     }
198 
199     private List<String> buildExclusionPatternsList()
200     {
201         List<String> patterns = new ArrayList<String>();
202 
203         if ( exclude != null )
204         {
205             String[] elements = exclude.split( " ?, ?" );
206 
207             patterns.addAll( Arrays.asList( elements ) );
208         }
209         else if ( excludes != null && !excludes.isEmpty() )
210         {
211             patterns.addAll( excludes );
212         }
213 
214         return patterns;
215     }
216 
217     private Map<String, Artifact> createArtifactMap( MavenProject project )
218     {
219         Map<String, Artifact> artifactMap = Collections.emptyMap();
220 
221         List<Dependency> dependencies = project.getDependencies();
222 
223         List<ArtifactRepository> remoteRepositories = Collections.emptyList();
224 
225         Set<Artifact> dependencyArtifacts = new HashSet<Artifact>();
226 
227         for ( Dependency dependency : dependencies )
228         {
229             VersionRange vr = VersionRange.createFromVersion( dependency.getVersion() );
230 
231             Artifact artifact = factory.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(),
232                                                                   vr, dependency.getType(), dependency.getClassifier(),
233                                                                   dependency.getScope() );
234             dependencyArtifacts.add( artifact );
235         }
236 
237         if ( actTransitively )
238         {
239             try
240             {
241                 ArtifactResolutionResult result = resolver.resolveTransitively( dependencyArtifacts, project
242                     .getArtifact(), remoteRepositories, localRepository, source );
243 
244                 artifactMap = ArtifactUtils.artifactMapByVersionlessId( result.getArtifacts() );
245             }
246             catch ( ArtifactResolutionException e )
247             {
248                 verbose( "Skipping: " + e.getArtifactId() + ". It cannot be resolved." );
249             }
250             catch ( ArtifactNotFoundException e )
251             {
252                 verbose( "Skipping: " + e.getArtifactId() + ". It cannot be resolved." );
253             }
254         }
255         else
256         {
257             artifactMap = new HashMap<String, Artifact>();
258             for ( Artifact artifact : dependencyArtifacts )
259             {
260                 try
261                 {
262                     resolver.resolve( artifact, remoteRepositories, localRepository );
263 
264                     artifactMap.put( ArtifactUtils.versionlessKey( artifact ), artifact );
265                 }
266                 catch ( ArtifactResolutionException e )
267                 {
268                     verbose( "Skipping: " + e.getArtifactId() + ". It cannot be resolved." );
269                 }
270                 catch ( ArtifactNotFoundException e )
271                 {
272                     verbose( "Skipping: " + e.getArtifactId() + ". It cannot be resolved." );
273                 }
274             }
275         }
276 
277         return artifactMap;
278     }
279 
280     private void verbose( String message )
281     {
282         if ( verbose || getLog().isDebugEnabled() )
283         {
284             getLog().info( message );
285         }
286     }
287 
288     private void refreshDependenciesForProject( MavenProject project, List<String> exclusionPatterns )
289         throws ArtifactResolutionException, MojoFailureException
290     {
291         Map<String, Artifact> deps = createArtifactMap( project );
292 
293         if ( deps.isEmpty() )
294         {
295             getLog().info( "Nothing to do for project: " + project.getId() );
296             return;
297         }
298 
299         if ( !exclusionPatterns.isEmpty() )
300         {
301             for ( String excludedKey : exclusionPatterns )
302             {
303                 if ( GROUP_ID_FUZZINESS.equals( resolutionFuzziness ) )
304                 {
305                     verbose( "Excluding groupId: " + excludedKey + " from refresh operation for project: "
306                         + project.getId() );
307 
308                     for ( Iterator<Map.Entry<String, Artifact>> deps_it = deps.entrySet().iterator(); deps_it.hasNext(); )
309                     {
310                         Map.Entry<String, Artifact> dependency = deps_it.next();
311             			
312             			Artifact artifact = dependency.getValue();
313             			
314                         if ( artifact.getGroupId().equals( excludedKey ) )
315             			{
316             				deps_it.remove();
317             			}
318             		}
319             	}
320             	else
321             	{
322 	                verbose( "Excluding: " + excludedKey + " from refresh operation for project: " + project.getId() );
323 	
324 	                deps.remove( excludedKey );
325             	}
326             }
327         }
328 
329         verbose( "Processing dependencies for project: " + project.getId() );
330 
331         List<Artifact> missingArtifacts = new ArrayList<Artifact>();
332         for ( Map.Entry<String, Artifact> entry : deps.entrySet() )
333         {
334             Artifact artifact = entry.getValue();
335 
336             verbose( "Processing artifact: " + artifact.getId() );
337 
338             File deleteTarget = findDeleteTarget( artifact );
339 
340             verbose( "Deleting: " + deleteTarget );
341 
342             if ( deleteTarget.isDirectory() )
343             {
344                 try
345                 {
346                     FileUtils.deleteDirectory( deleteTarget );
347                 }
348                 catch ( IOException e )
349                 {
350                     throw new MojoFailureException( this, "Cannot delete dependency from the local repository: "
351                         + artifact.getId(), "Failed to delete: " + deleteTarget );
352                 }
353             }
354             else
355             {
356                 deleteTarget.delete();
357             }
358 
359             if ( reResolve )
360             {
361                 verbose( "Re-resolving." );
362 
363                 artifact.setResolved( false );
364 
365                 try
366                 {
367                     resolver.resolveAlways( artifact, project.getRemoteArtifactRepositories(), localRepository );
368                 }
369                 catch ( ArtifactResolutionException e )
370                 {
371                     getLog().debug( e.getMessage() );
372                     missingArtifacts.add( artifact );
373                 }
374                 catch ( ArtifactNotFoundException e )
375                 {
376                     getLog().debug( e.getMessage() );
377                     missingArtifacts.add( artifact );
378                 }
379             }
380         }
381 
382         if ( missingArtifacts.size() > 0 )
383         {
384             String message = "required artifacts missing:\n";
385             for ( Artifact missingArtifact : missingArtifacts )
386             {
387                 message += "  " + missingArtifact.getId() + "\n";
388             }
389             message += "\nfor the artifact:";
390 
391             throw new ArtifactResolutionException( message, project.getArtifact(), project
392                 .getRemoteArtifactRepositories() );
393         }
394 
395     }
396 
397     private File findDeleteTarget( Artifact artifact )
398     {
399         File deleteTarget = artifact.getFile();
400 
401         if ( GROUP_ID_FUZZINESS.equals( resolutionFuzziness ) )
402         {
403             // get the artifactId dir.
404             deleteTarget = deleteTarget.getParentFile().getParentFile();
405 
406             // get the first groupId dir.
407             deleteTarget = deleteTarget.getParentFile();
408 
409             String[] path = localRepository.pathOf( artifact ).split( "\\/" );
410 
411             // subtract the artifact filename, version dir, artifactId dir, and
412             // the first groupId
413             // dir, since we've accounted for those above.
414             int groupParts = path.length - 4;
415 
416             File parent = deleteTarget.getParentFile();
417             int count = 0;
418             while ( count++ < groupParts )
419             {
420                 // prune empty dirs back to the beginning of the groupId, if
421                 // possible.
422 
423                 // if the parent dir only has the one child file, then it's okay
424                 // to prune.
425                 if ( parent.list().length < 2 )
426                 {
427                     deleteTarget = parent;
428 
429                     // check the parent of this newly checked dir
430                     parent = deleteTarget.getParentFile();
431                 }
432                 else
433                 {
434                     // if there are more files than the one that we're
435                     // interested in killing, stop.
436                     break;
437                 }
438             }
439 
440         }
441         else if ( ARTIFACT_ID_FUZZINESS.equals( resolutionFuzziness ) )
442         {
443             // get the artifactId dir.
444             deleteTarget = deleteTarget.getParentFile().getParentFile();
445         }
446         else if ( VERSION_FUZZINESS.equals( resolutionFuzziness ) )
447         {
448             // get the version dir.
449             deleteTarget = deleteTarget.getParentFile();
450         }
451         // else it's file fuzziness.
452 
453         return deleteTarget;
454     }
455 
456 }