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
35 /**
36 * Removes the not-available marker files from the repository.
37 *
38 * @author <a href="mailto:baerrach@apache.org">Barrie Treloar</a>
39 * @version $Id: RemoveCacheMojo.java 728546 2008-12-21 22:56:51Z bentmann $
40 * @goal remove-cache
41 */
42 public class RemoveCacheMojo
43 extends AbstractMojo
44 {
45 /**
46 * Local maven repository.
47 *
48 * @parameter expression="${localRepository}"
49 * @required
50 * @readonly
51 */
52 private ArtifactRepository localRepository;
53
54 public void execute()
55 throws MojoExecutionException, MojoFailureException
56 {
57 getLog().info( Messages.getString( "RemoveCacheMojo.checking" ) );
58 List notAvailableMarkerFiles = getNotAvailableMarkerFiles();
59 if ( !notAvailableMarkerFiles.isEmpty() )
60 {
61 deleteMarkerFiles( notAvailableMarkerFiles );
62 }
63 getLog().info( Messages.getString( "RemoveCacheMojo.complete" ) );
64 }
65
66 /**
67 * Delete each file in the notAvailableMarkerFiles list.
68 *
69 * @param notAvailableMarkerFiles the list of marker files to delete.
70 */
71 private void deleteMarkerFiles( List/* <File> */notAvailableMarkerFiles )
72 {
73 for ( Iterator iter = notAvailableMarkerFiles.iterator(); iter.hasNext(); )
74 {
75 File markerFile = (File) iter.next();
76 try
77 {
78 IdeUtils.delete( markerFile, getLog() );
79 }
80 catch ( MojoExecutionException e )
81 {
82 getLog().warn( e.getMessage(), e );
83 }
84 }
85 }
86
87 /**
88 * A list of all the not available marker <code>File</code>s in the localRepository. If there are no marker files
89 * then an empty list is returned.
90 *
91 * @return all the not available marker files in the localRepository or an empty list.
92 */
93 private List/* <File> */getNotAvailableMarkerFiles()
94 {
95 File localRepositoryBaseDirectory = new File( localRepository.getBasedir() );
96 List markerFiles = new ArrayList();
97
98 Iterator iterator =
99 FileUtils.iterateFiles( localRepositoryBaseDirectory,
100 new SuffixFileFilter( IdeUtils.NOT_AVAILABLE_MARKER_FILE_SUFFIX ),
101 TrueFileFilter.INSTANCE );
102 while ( iterator.hasNext() )
103 {
104 File notAvailableMarkerFile = (File) iterator.next();
105 markerFiles.add( notAvailableMarkerFile );
106 }
107 return markerFiles;
108 }
109
110 }