View Javadoc
1   package org.apache.maven.plugins.clean;
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.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugins.annotations.Mojo;
25  import org.apache.maven.plugins.annotations.Parameter;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  /**
31   * Goal which cleans the build.
32   * <p>
33   * This attempts to clean a project's working directory of the files that were generated at build-time. By default, it
34   * discovers and deletes the directories configured in <code>project.build.directory</code>,
35   * <code>project.build.outputDirectory</code>, <code>project.build.testOutputDirectory</code>, and
36   * <code>project.reporting.outputDirectory</code>.
37   * </p>
38   * <p>
39   * Files outside the default may also be included in the deletion by configuring the <code>filesets</code> tag.
40   * </p>
41   *
42   * @author <a href="mailto:evenisse@maven.org">Emmanuel Venisse</a>
43   * @version $Id$
44   * @see org.apache.maven.plugins.clean.Fileset
45   * @since 2.0
46   */
47  @Mojo( name = "clean", threadSafe = true )
48  public class CleanMojo
49      extends AbstractMojo
50  {
51  
52      /**
53       * This is where build results go.
54       */
55      @Parameter( defaultValue = "${project.build.directory}", readonly = true, required = true )
56      private File directory;
57  
58      /**
59       * This is where compiled classes go.
60       */
61      @Parameter( defaultValue = "${project.build.outputDirectory}", readonly = true, required = true )
62      private File outputDirectory;
63  
64      /**
65       * This is where compiled test classes go.
66       */
67      @Parameter( defaultValue = "${project.build.testOutputDirectory}", readonly = true, required = true )
68      private File testOutputDirectory;
69  
70      /**
71       * This is where the site plugin generates its pages.
72       *
73       * @since 2.1.1
74       */
75      @Parameter( defaultValue = "${project.build.outputDirectory}", readonly = true, required = true )
76      private File reportDirectory;
77  
78      /**
79       * Sets whether the plugin runs in verbose mode. As of plugin version 2.3, the default value is derived from Maven's
80       * global debug flag (compare command line switch <code>-X</code>). <br/>
81       * Starting with <b>3.0.0</b> the property has been renamed from <code>clean.verbose</code> to
82       * <code>maven.clean.verbose</code>.
83       * 
84       * @since 2.1
85       */
86      @Parameter( property = "maven.clean.verbose" )
87      private Boolean verbose;
88  
89      /**
90       * The list of file sets to delete, in addition to the default directories. For example:
91       * 
92       * <pre>
93       * &lt;filesets&gt;
94       *   &lt;fileset&gt;
95       *     &lt;directory&gt;src/main/generated&lt;/directory&gt;
96       *     &lt;followSymlinks&gt;false&lt;/followSymlinks&gt;
97       *     &lt;useDefaultExcludes&gt;true&lt;/useDefaultExcludes&gt;
98       *     &lt;includes&gt;
99       *       &lt;include&gt;*.java&lt;/include&gt;
100      *     &lt;/includes&gt;
101      *     &lt;excludes&gt;
102      *       &lt;exclude&gt;Template*&lt;/exclude&gt;
103      *     &lt;/excludes&gt;
104      *   &lt;/fileset&gt;
105      * &lt;/filesets&gt;
106      * </pre>
107      *
108      * @since 2.1
109      */
110     @Parameter
111     private Fileset[] filesets;
112 
113     /**
114      * Sets whether the plugin should follow symbolic links while deleting files from the default output directories of
115      * the project. Not following symlinks requires more IO operations and heap memory, regardless whether symlinks are
116      * actually present. So projects with a huge output directory that knowingly does not contain symlinks can improve
117      * performance by setting this parameter to <code>true</code>. <br/>
118      * Starting with <code>3.0.0</code> the property has been renamed from <code>clean.followSymLinks</code> to
119      * <code>maven.clean.followSymLinks</code>.
120      * 
121      * @since 2.1
122      */
123     @Parameter( property = "maven.clean.followSymLinks", defaultValue = "false" )
124     private boolean followSymLinks;
125 
126     /**
127      * Disables the plugin execution. <br/>
128      * Starting with <code>3.0.0</code> the property has been renamed from <code>clean.skip</code> to
129      * <code>maven.clean.skip</code>.
130      * 
131      * @since 2.2
132      */
133     @Parameter( property = "maven.clean.skip", defaultValue = "false" )
134     private boolean skip;
135 
136     /**
137      * Indicates whether the build will continue even if there are clean errors.
138      *
139      * @since 2.2
140      */
141     @Parameter( property = "maven.clean.failOnError", defaultValue = "true" )
142     private boolean failOnError;
143 
144     /**
145      * Indicates whether the plugin should undertake additional attempts (after a short delay) to delete a file if the
146      * first attempt failed. This is meant to help deleting files that are temporarily locked by third-party tools like
147      * virus scanners or search indexing.
148      *
149      * @since 2.4.2
150      */
151     @Parameter( property = "maven.clean.retryOnError", defaultValue = "true" )
152     private boolean retryOnError;
153 
154     /**
155      * Disables the deletion of the default output directories configured for a project. If set to <code>true</code>,
156      * only the files/directories selected via the parameter {@link #filesets} will be deleted. <br/>
157      * Starting with <b>3.0.0</b> the property has been renamed from <code>clean.excludeDefaultDirectories</code> to
158      * <code>maven.clean.excludeDefaultDirectories</code>.
159      *
160      * @since 2.3
161      */
162     @Parameter( property = "maven.clean.excludeDefaultDirectories", defaultValue = "false" )
163     private boolean excludeDefaultDirectories;
164 
165     /**
166      * Deletes file-sets in the following project build directory order: (source) directory, output directory, test
167      * directory, report directory, and then the additional file-sets.
168      *
169      * @throws MojoExecutionException When a directory failed to get deleted.
170      * @see org.apache.maven.plugin.Mojo#execute()
171      */
172     public void execute()
173         throws MojoExecutionException
174     {
175         if ( skip )
176         {
177             getLog().info( "Clean is skipped." );
178             return;
179         }
180 
181         Cleaner cleaner = new Cleaner( getLog(), isVerbose() );
182 
183         try
184         {
185             for ( File directoryItem : getDirectories() )
186             {
187                 if ( directoryItem != null )
188                 {
189                     cleaner.delete( directoryItem, null, followSymLinks, failOnError, retryOnError );
190                 }
191             }
192 
193             if ( filesets != null )
194             {
195                 for ( Fileset fileset : filesets )
196                 {
197                     if ( fileset.getDirectory() == null )
198                     {
199                         throw new MojoExecutionException( "Missing base directory for " + fileset );
200                     }
201                     GlobSelector selector = new GlobSelector( fileset.getIncludes(), fileset.getExcludes(),
202                                                               fileset.isUseDefaultExcludes() );
203                     cleaner.delete( fileset.getDirectory(), selector, fileset.isFollowSymlinks(), failOnError,
204                                     retryOnError );
205                 }
206             }
207         }
208         catch ( IOException e )
209         {
210             throw new MojoExecutionException( "Failed to clean project: " + e.getMessage(), e );
211         }
212     }
213 
214     /**
215      * Indicates whether verbose output is enabled.
216      *
217      * @return <code>true</code> if verbose output is enabled, <code>false</code> otherwise.
218      */
219     private boolean isVerbose()
220     {
221         return ( verbose != null ) ? verbose : getLog().isDebugEnabled();
222     }
223 
224     /**
225      * Gets the directories to clean (if any). The returned array may contain null entries.
226      *
227      * @return The directories to clean or an empty array if none, never <code>null</code>.
228      */
229     private File[] getDirectories()
230     {
231         File[] directories;
232         if ( excludeDefaultDirectories )
233         {
234             directories = new File[0];
235         }
236         else
237         {
238             directories = new File[] { directory, outputDirectory, testOutputDirectory, reportDirectory };
239         }
240         return directories;
241     }
242 
243 }