Coverage Report - org.apache.maven.plugin.dependency.ManualPurgeLocalRepositoryMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
ManualPurgeLocalRepositoryMojo
0%
0/33
0%
0/20
5.667
 
 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 org.apache.maven.artifact.repository.ArtifactRepository;
 23  
 import org.apache.maven.plugin.AbstractMojo;
 24  
 import org.apache.maven.plugin.MojoExecutionException;
 25  
 import org.apache.maven.plugin.MojoFailureException;
 26  
 import org.apache.maven.plugins.annotations.Mojo;
 27  
 import org.apache.maven.plugins.annotations.Parameter;
 28  
 import org.codehaus.plexus.util.FileUtils;
 29  
 import org.codehaus.plexus.util.StringUtils;
 30  
 
 31  
 import java.io.File;
 32  
 import java.io.IOException;
 33  
 import java.util.ArrayList;
 34  
 import java.util.Arrays;
 35  
 import java.util.List;
 36  
 
 37  
 /**
 38  
  * Remove/purge artifacts from the local repository.
 39  
  * 
 40  
  * @version $Id: PurgeLocalRepositoryMojo.java 1400764 2012-10-22 05:31:26Z pgier $
 41  
  * @since 2.6
 42  
  */
 43  
 @Mojo( name = "manual-purge-local-repository", threadSafe = true, requiresProject = false )
 44  0
 public class ManualPurgeLocalRepositoryMojo
 45  
     extends AbstractMojo
 46  
 {
 47  
 
 48  
     /**
 49  
      * The local repository, from which to delete artifacts.
 50  
      */
 51  
     @Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
 52  
     private ArtifactRepository localRepository;
 53  
 
 54  
     /**
 55  
      * The list of artifacts in the form of groupId:artifactId:version which should be deleted/purged from the local
 56  
      * repository. To delete all versions for a specific artifact, leave the version empty (groupId:artifactId).
 57  
      * To delete all artifacts for a specific groupId, just provide the groupId by itself.
 58  
      */
 59  
     @Parameter
 60  
     private List<String> includes;
 61  
 
 62  
     /**
 63  
      * Comma-separated list of groupId:artifactId entries, which should be used to manually include artifacts for
 64  
      * deletion. This is a command-line alternative to the <code>manualIncludes</code> parameter, since List parameters
 65  
      * are not currently compatible with CLI specification.
 66  
      */
 67  
     @Parameter( property = "include" )
 68  
     private String include;
 69  
 
 70  
 
 71  
     public void execute()
 72  
         throws MojoExecutionException, MojoFailureException
 73  
     {
 74  0
         if ( !StringUtils.isEmpty( include ) )
 75  
         {
 76  0
             includes = this.parseIncludes( include );
 77  
         }
 78  
 
 79  0
         if ( includes == null || includes.isEmpty() )
 80  
         {
 81  0
             throw new MojoExecutionException( "Parameter manualIncludes must be specified" );
 82  
         }
 83  
 
 84  0
         for ( String gavPattern : includes )
 85  
         {
 86  0
             if ( StringUtils.isEmpty( gavPattern ) )
 87  
             {
 88  0
                 getLog().debug( "Skipping empty gav pattern: " + gavPattern );
 89  0
                 continue;
 90  
             }
 91  
 
 92  0
             String relativePath = gavToPath( gavPattern );
 93  0
             if ( StringUtils.isEmpty( relativePath ) )
 94  
             {
 95  0
                 continue;
 96  
             }
 97  
 
 98  0
             File purgeDir = new File( localRepository.getBasedir(), relativePath );
 99  0
             if ( purgeDir.exists() )
 100  
             {
 101  0
                 getLog().debug( "Deleting directory: " + purgeDir );
 102  
                 try
 103  
                 {
 104  0
                     FileUtils.deleteDirectory( purgeDir );
 105  
                 }
 106  0
                 catch ( IOException e )
 107  
                 {
 108  0
                     throw new MojoExecutionException( "Unable to purge directory: " + purgeDir );
 109  0
                 }
 110  
             }
 111  0
         }
 112  0
     }
 113  
 
 114  
     /**
 115  
      * Convert a groupId:artifactId:version to a file system path
 116  
      * 
 117  
      * @param gav, the groupId:artifactId:version string
 118  
      * @return
 119  
      */
 120  
     private String gavToPath( String gav )
 121  
     {
 122  0
         if ( StringUtils.isEmpty( gav ) )
 123  
         {
 124  0
             return null;
 125  
         }
 126  
         
 127  0
         String[] pathComponents = gav.split( ":" );
 128  
 
 129  0
         StringBuffer path = new StringBuffer( pathComponents[0].replace( '.', '/' ) );
 130  
         
 131  0
         for ( int i=1; i<pathComponents.length; ++i )
 132  
         {
 133  0
             path.append( "/" +  pathComponents[i] );
 134  
         }
 135  
 
 136  0
         return path.toString();
 137  
     }
 138  
 
 139  
     /**
 140  
      * Convert comma separated list of includes to List object
 141  
      * 
 142  
      * @param include
 143  
      * @return the includes list
 144  
      */
 145  
     private List<String> parseIncludes( String include )
 146  
     {
 147  0
         List<String> includes = new ArrayList<String>();
 148  
 
 149  0
         if ( include != null )
 150  
         {
 151  0
             String[] elements = include.split( "," );
 152  0
             includes.addAll( Arrays.asList( elements ) );
 153  
         }
 154  
 
 155  0
         return includes;
 156  
     }
 157  
 
 158  
 }