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 728546 2008-12-21 22:56:51Z bentmann $
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 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 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 exclusionPatterns = buildExclusionPatternsList();
178 
179         for ( Iterator it = projects.iterator(); it.hasNext(); )
180         {
181             MavenProject project = (MavenProject) it.next();
182 
183             try
184             {
185                 refreshDependenciesForProject( project, exclusionPatterns );
186             }
187             catch ( ArtifactResolutionException e )
188             {
189                 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                 failure.initCause( e );
195 
196                 throw failure;
197             }
198         }
199     }
200 
201     private List buildExclusionPatternsList()
202     {
203         List patterns = new ArrayList();
204 
205         if ( exclude != null )
206         {
207             String[] elements = exclude.split( " ?, ?" );
208 
209             patterns.addAll( Arrays.asList( elements ) );
210         }
211         else if ( excludes != null && !excludes.isEmpty() )
212         {
213             patterns.addAll( excludes );
214         }
215 
216         return patterns;
217     }
218 
219     private Map createArtifactMap( MavenProject project )
220     {
221         Map artifactMap = Collections.EMPTY_MAP;
222 
223         List dependencies = project.getDependencies();
224 
225         List remoteRepositories = Collections.EMPTY_LIST;
226 
227         Set dependencyArtifacts = new HashSet();
228 
229         for ( Iterator it = dependencies.iterator(); it.hasNext(); )
230         {
231             Dependency dependency = (Dependency) it.next();
232 
233             VersionRange vr = VersionRange.createFromVersion( dependency.getVersion() );
234 
235             Artifact artifact = factory.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(),
236                                                                   vr, dependency.getType(), dependency.getClassifier(),
237                                                                   dependency.getScope() );
238             dependencyArtifacts.add( artifact );
239         }
240 
241         if ( actTransitively )
242         {
243             try
244             {
245                 ArtifactResolutionResult result = resolver.resolveTransitively( dependencyArtifacts, project
246                     .getArtifact(), remoteRepositories, localRepository, source );
247 
248                 artifactMap = ArtifactUtils.artifactMapByVersionlessId( result.getArtifacts() );
249             }
250             catch ( ArtifactResolutionException e )
251             {
252                 verbose( "Skipping: " + e.getArtifactId() + ". It cannot be resolved." );
253             }
254             catch ( ArtifactNotFoundException e )
255             {
256                 verbose( "Skipping: " + e.getArtifactId() + ". It cannot be resolved." );
257             }
258         }
259         else
260         {
261             artifactMap = new HashMap();
262             for ( Iterator it = dependencyArtifacts.iterator(); it.hasNext(); )
263             {
264                 Artifact artifact = (Artifact) it.next();
265 
266                 try
267                 {
268                     resolver.resolve( artifact, remoteRepositories, localRepository );
269 
270                     artifactMap.put( ArtifactUtils.versionlessKey( artifact ), artifact );
271                 }
272                 catch ( ArtifactResolutionException e )
273                 {
274                     verbose( "Skipping: " + e.getArtifactId() + ". It cannot be resolved." );
275                 }
276                 catch ( ArtifactNotFoundException e )
277                 {
278                     verbose( "Skipping: " + e.getArtifactId() + ". It cannot be resolved." );
279                 }
280             }
281         }
282 
283         return artifactMap;
284     }
285 
286     private void verbose( String message )
287     {
288         if ( verbose || getLog().isDebugEnabled() )
289         {
290             getLog().info( message );
291         }
292     }
293 
294     private void refreshDependenciesForProject( MavenProject project, List exclusionPatterns )
295         throws ArtifactResolutionException, MojoFailureException
296     {
297         Map deps = createArtifactMap( project );
298 
299         if ( deps.isEmpty() )
300         {
301             getLog().info( "Nothing to do for project: " + project.getId() );
302             return;
303         }
304 
305         if ( !exclusionPatterns.isEmpty() )
306         {
307             for ( Iterator it = exclusionPatterns.iterator(); it.hasNext(); )
308             {
309                 String excludedKey = (String) it.next();
310 
311                 verbose( "Excluding: " + excludedKey + " from refresh operation for project: " + project.getId() );
312 
313                 deps.remove( excludedKey );
314             }
315         }
316 
317         verbose( "Processing dependencies for project: " + project.getId() );
318 
319         List missingArtifacts = new ArrayList();
320         for ( Iterator it = deps.entrySet().iterator(); it.hasNext(); )
321         {
322             Map.Entry entry = (Map.Entry) it.next();
323 
324             Artifact artifact = (Artifact) entry.getValue();
325 
326             verbose( "Processing artifact: " + artifact.getId() );
327 
328             File deleteTarget = findDeleteTarget( artifact );
329 
330             verbose( "Deleting: " + deleteTarget );
331 
332             if ( deleteTarget.isDirectory() )
333             {
334                 try
335                 {
336                     FileUtils.deleteDirectory( deleteTarget );
337                 }
338                 catch ( IOException e )
339                 {
340                     throw new MojoFailureException( this, "Cannot delete dependency from the local repository: "
341                         + artifact.getId(), "Failed to delete: " + deleteTarget );
342                 }
343             }
344             else
345             {
346                 deleteTarget.delete();
347             }
348 
349             if ( reResolve )
350             {
351                 verbose( "Re-resolving." );
352 
353                 artifact.setResolved( false );
354 
355                 try
356                 {
357                     resolver.resolveAlways( artifact, project.getRemoteArtifactRepositories(), localRepository );
358                 }
359                 catch ( ArtifactResolutionException e )
360                 {
361                     getLog().debug( e.getMessage() );
362                     missingArtifacts.add( artifact );
363                 }
364                 catch ( ArtifactNotFoundException e )
365                 {
366                     getLog().debug( e.getMessage() );
367                     missingArtifacts.add( artifact );
368                 }
369             }
370         }
371 
372         if ( missingArtifacts.size() > 0 )
373         {
374             String message = "required artifacts missing:\n";
375             for ( Iterator i = missingArtifacts.iterator(); i.hasNext(); )
376             {
377                 Artifact missingArtifact = (Artifact) i.next();
378                 message += "  " + missingArtifact.getId() + "\n";
379             }
380             message += "\nfor the artifact:";
381 
382             throw new ArtifactResolutionException( message, project.getArtifact(), project
383                 .getRemoteArtifactRepositories() );
384         }
385 
386     }
387 
388     private File findDeleteTarget( Artifact artifact )
389     {
390         File deleteTarget = artifact.getFile();
391 
392         if ( GROUP_ID_FUZZINESS.equals( resolutionFuzziness ) )
393         {
394             // get the artifactId dir.
395             deleteTarget = deleteTarget.getParentFile().getParentFile();
396 
397             // get the first groupId dir.
398             deleteTarget = deleteTarget.getParentFile();
399 
400             String[] path = localRepository.pathOf( artifact ).split( "\\/" );
401 
402             // subtract the artifact filename, version dir, artifactId dir, and
403             // the first groupId
404             // dir, since we've accounted for those above.
405             int groupParts = path.length - 4;
406 
407             File parent = deleteTarget.getParentFile();
408             int count = 0;
409             while ( count++ < groupParts )
410             {
411                 // prune empty dirs back to the beginning of the groupId, if
412                 // possible.
413 
414                 // if the parent dir only has the one child file, then it's okay
415                 // to prune.
416                 if ( parent.list().length < 2 )
417                 {
418                     deleteTarget = parent;
419 
420                     // check the parent of this newly checked dir
421                     parent = deleteTarget.getParentFile();
422                 }
423                 else
424                 {
425                     // if there are more files than the one that we're
426                     // interested in killing, stop.
427                     break;
428                 }
429             }
430 
431         }
432         else if ( ARTIFACT_ID_FUZZINESS.equals( resolutionFuzziness ) )
433         {
434             // get the artifactId dir.
435             deleteTarget = deleteTarget.getParentFile().getParentFile();
436         }
437         else if ( VERSION_FUZZINESS.equals( resolutionFuzziness ) )
438         {
439             // get the version dir.
440             deleteTarget = deleteTarget.getParentFile();
441         }
442         // else it's file fuzziness.
443 
444         return deleteTarget;
445     }
446 
447 }