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