Coverage Report - org.apache.maven.plugin.dependency.PurgeLocalRepositoryMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
PurgeLocalRepositoryMojo
0%
0/116
0%
0/44
6.333
 
 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  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  
      * 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  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  
             // get the artifactId dir.
 395  0
             deleteTarget = deleteTarget.getParentFile().getParentFile();
 396  
 
 397  
             // get the first groupId dir.
 398  0
             deleteTarget = deleteTarget.getParentFile();
 399  
 
 400  0
             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  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  
                 // 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  0
                 if ( parent.list().length < 2 )
 417  
                 {
 418  0
                     deleteTarget = parent;
 419  
 
 420  
                     // check the parent of this newly checked dir
 421  0
                     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  0
         }
 432  0
         else if ( ARTIFACT_ID_FUZZINESS.equals( resolutionFuzziness ) )
 433  
         {
 434  
             // get the artifactId dir.
 435  0
             deleteTarget = deleteTarget.getParentFile().getParentFile();
 436  
         }
 437  0
         else if ( VERSION_FUZZINESS.equals( resolutionFuzziness ) )
 438  
         {
 439  
             // get the version dir.
 440  0
             deleteTarget = deleteTarget.getParentFile();
 441  
         }
 442  
         // else it's file fuzziness.
 443  
 
 444  0
         return deleteTarget;
 445  
     }
 446  
 
 447  
 }