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 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  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          if ( !StringUtils.isEmpty( include ) )
75          {
76              includes = this.parseIncludes( include );
77          }
78  
79          if ( includes == null || includes.isEmpty() )
80          {
81              throw new MojoExecutionException( "Parameter manualIncludes must be specified" );
82          }
83  
84          for ( String gavPattern : includes )
85          {
86              if ( StringUtils.isEmpty( gavPattern ) )
87              {
88                  getLog().debug( "Skipping empty gav pattern: " + gavPattern );
89                  continue;
90              }
91  
92              String relativePath = gavToPath( gavPattern );
93              if ( StringUtils.isEmpty( relativePath ) )
94              {
95                  continue;
96              }
97  
98              File purgeDir = new File( localRepository.getBasedir(), relativePath );
99              if ( purgeDir.exists() )
100             {
101                 getLog().debug( "Deleting directory: " + purgeDir );
102                 try
103                 {
104                     FileUtils.deleteDirectory( purgeDir );
105                 }
106                 catch ( IOException e )
107                 {
108                     throw new MojoExecutionException( "Unable to purge directory: " + purgeDir );
109                 }
110             }
111         }
112     }
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         if ( StringUtils.isEmpty( gav ) )
123         {
124             return null;
125         }
126         
127         String[] pathComponents = gav.split( ":" );
128 
129         StringBuffer path = new StringBuffer( pathComponents[0].replace( '.', '/' ) );
130         
131         for ( int i=1; i<pathComponents.length; ++i )
132         {
133             path.append( "/" +  pathComponents[i] );
134         }
135 
136         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         List<String> includes = new ArrayList<String>();
148 
149         if ( include != null )
150         {
151             String[] elements = include.split( "," );
152             includes.addAll( Arrays.asList( elements ) );
153         }
154 
155         return includes;
156     }
157 
158 }