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