View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.eclipse;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.io.FileUtils;
27  import org.apache.commons.io.filefilter.SuffixFileFilter;
28  import org.apache.commons.io.filefilter.TrueFileFilter;
29  import org.apache.maven.artifact.repository.ArtifactRepository;
30  import org.apache.maven.plugin.AbstractMojo;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.MojoFailureException;
33  import org.apache.maven.plugin.ide.IdeUtils;
34  import org.apache.maven.plugins.annotations.Mojo;
35  import org.apache.maven.plugins.annotations.Parameter;
36  
37  /**
38   * Removes the not-available marker files from the repository.
39   *
40   * @author <a href="mailto:baerrach@apache.org">Barrie Treloar</a>
41   * @version $Id: RemoveCacheMojo.java 1672305 2015-04-09 12:10:45Z khmarbaise $
42   */
43  @Mojo( name = "remove-cache" )
44  public class RemoveCacheMojo
45      extends AbstractMojo
46  {
47      /**
48       * Local maven repository.
49       */
50      @Parameter( property = "localRepository", required = true, readonly = true )
51      private ArtifactRepository localRepository;
52  
53      public void execute()
54          throws MojoExecutionException, MojoFailureException
55      {
56          getLog().info( Messages.getString( "RemoveCacheMojo.checking" ) );
57          List notAvailableMarkerFiles = getNotAvailableMarkerFiles();
58          if ( !notAvailableMarkerFiles.isEmpty() )
59          {
60              deleteMarkerFiles( notAvailableMarkerFiles );
61          }
62          getLog().info( Messages.getString( "RemoveCacheMojo.complete" ) );
63      }
64  
65      /**
66       * Delete each file in the notAvailableMarkerFiles list.
67       * 
68       * @param notAvailableMarkerFiles the list of marker files to delete.
69       */
70      private void deleteMarkerFiles( List/* <File> */notAvailableMarkerFiles )
71      {
72          for (Object notAvailableMarkerFile : notAvailableMarkerFiles) {
73              File markerFile = (File) notAvailableMarkerFile;
74              try {
75                  IdeUtils.delete(markerFile, getLog());
76              } catch (MojoExecutionException e) {
77                  getLog().warn(e.getMessage(), e);
78              }
79          }
80      }
81  
82      /**
83       * A list of all the not available marker <code>File</code>s in the localRepository. If there are no marker files
84       * then an empty list is returned.
85       * 
86       * @return all the not available marker files in the localRepository or an empty list.
87       */
88      private List/* <File> */getNotAvailableMarkerFiles()
89      {
90          File localRepositoryBaseDirectory = new File( localRepository.getBasedir() );
91          List markerFiles = new ArrayList();
92  
93          Iterator iterator =
94              FileUtils.iterateFiles( localRepositoryBaseDirectory,
95                                      new SuffixFileFilter( IdeUtils.NOT_AVAILABLE_MARKER_FILE_SUFFIX ),
96                                      TrueFileFilter.INSTANCE );
97          while ( iterator.hasNext() )
98          {
99              File notAvailableMarkerFile = (File) iterator.next();
100             markerFiles.add( notAvailableMarkerFile );
101         }
102         return markerFiles;
103     }
104 
105 }