Coverage Report - org.apache.maven.plugin.eclipse.EclipseCleanMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseCleanMojo
77% 
100% 
3
 
 1  
 package org.apache.maven.plugin.eclipse;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * 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, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.IOException;
 21  
 
 22  
 import org.apache.maven.plugin.AbstractMojo;
 23  
 import org.apache.maven.plugin.MojoExecutionException;
 24  
 import org.codehaus.plexus.util.FileUtils;
 25  
 
 26  
 /**
 27  
  * Deletes the .project, .classpath, .wtpmodules files and .settings folder used by Eclipse.
 28  
  * 
 29  
  * @goal clean
 30  
  */
 31  22
 public class EclipseCleanMojo
 32  
     extends AbstractMojo
 33  
 {
 34  
 
 35  
     /**
 36  
      * Definition file for Eclipse Web Tools project.
 37  
      */
 38  
     private static final String FILE_DOT_WTPMODULES = ".wtpmodules"; //$NON-NLS-1$
 39  
 
 40  
     /**
 41  
      * Classpath definition file for an Eclipse Java project.
 42  
      */
 43  
     private static final String FILE_DOT_CLASSPATH = ".classpath"; //$NON-NLS-1$
 44  
 
 45  
     /**
 46  
      * Project definition file for an Eclipse Project.
 47  
      */
 48  
     private static final String FILE_DOT_PROJECT = ".project"; //$NON-NLS-1$
 49  
 
 50  
     /**
 51  
      * Web Project definition file for Eclipse Web Tools Project (Release 1.0x).
 52  
      */
 53  
     private static final String DIR_DOT_SETTINGS = ".settings"; //$NON-NLS-1$
 54  
 
 55  
     /**
 56  
      * File name where the WTP component settings will be stored - WTP 1.0 name.
 57  
      */
 58  
     private static final String FILE_DOT_COMPONENT = ".settings/.component"; //$NON-NLS-1$
 59  
 
 60  
     /**
 61  
      * File name where the WTP component settings will be stored - WTP 1.5 name.
 62  
      */
 63  
     private static final String FILE_DOT_COMPONENT_15 = ".settings/org.eclipse.wst.common.component"; //$NON-NLS-1$
 64  
 
 65  
     /**
 66  
      * File name where Eclipse Project's Facet configuration will be stored.
 67  
      */
 68  
     private static final String FILE_FACET_CORE_XML = ".settings/org.eclipse.wst.common.project.facet.core.xml"; //$NON-NLS-1$
 69  
 
 70  
     /**
 71  
      * General project preferences.
 72  
      */
 73  
     private static final String FILE_ECLIPSE_JDT_CORE_PREFS = ".settings/org.eclipse.jdt.core.prefs"; //$NON-NLS-1$
 74  
 
 75  
     /**
 76  
      * Packaging for the current project.
 77  
      * @parameter expression="${project.packaging}"
 78  
      * @required
 79  
      * @readonly
 80  
      */
 81  
     private String packaging;
 82  
 
 83  
     /**
 84  
      * The root directory of the project
 85  
      *
 86  
      * @parameter expression="${basedir}"
 87  
      */
 88  
     private File basedir;
 89  
 
 90  
     /**
 91  
      * Is it an PDE project?
 92  
      * 
 93  
      * @parameter expression="${eclipse.pde}" default-value="false"
 94  
      */
 95  
     private boolean pde;
 96  
 
 97  
     /**
 98  
      * @see org.apache.maven.plugin.AbstractMojo#execute()
 99  
      */
 100  
     public void execute()
 101  
         throws MojoExecutionException
 102  
     {
 103  
 
 104  
         // since the eclipse plugin doesn't generate configuration for POM projects, it should neither delete it
 105  22
         if ( "pom".equals( packaging ) ) //$NON-NLS-1$
 106  
         {
 107  0
             getLog().info( Messages.getString( "EclipsePlugin.pompackaging" ) ); //$NON-NLS-1$
 108  0
             return;
 109  
         }
 110  
 
 111  22
         delete( new File( basedir, FILE_DOT_PROJECT ) );
 112  22
         delete( new File( basedir, FILE_DOT_CLASSPATH ) );
 113  22
         delete( new File( basedir, FILE_DOT_WTPMODULES ) );
 114  
 
 115  22
         delete( new File( basedir, FILE_DOT_COMPONENT ) );
 116  22
         delete( new File( basedir, FILE_DOT_COMPONENT_15 ) );
 117  22
         delete( new File( basedir, FILE_FACET_CORE_XML ) );
 118  22
         delete( new File( basedir, FILE_ECLIPSE_JDT_CORE_PREFS ) );
 119  
 
 120  22
         File settingsDir = new File( basedir, DIR_DOT_SETTINGS );
 121  22
         if ( settingsDir.exists() && settingsDir.isDirectory() && settingsDir.list().length == 0 )
 122  
         {
 123  2
             delete( settingsDir );
 124  
         }
 125  
 
 126  22
     }
 127  
 
 128  
     /**
 129  
      * Delete a file, handling log messages and exceptions
 130  
      * 
 131  
      * @param f File to be deleted
 132  
      * @throws MojoExecutionException only if a file exists and can't be deleted
 133  
      */
 134  
     private void delete( File f )
 135  
         throws MojoExecutionException
 136  
     {
 137  156
         if ( f.isDirectory() )
 138  
         {
 139  2
             getLog().info( Messages.getString( "EclipseCleanMojo.deletingDirectory", f.getName() ) ); //$NON-NLS-1$
 140  2
         }
 141  
         else
 142  
         {
 143  154
             getLog().info( Messages.getString( "EclipseCleanMojo.deletingFile", f.getName() ) ); //$NON-NLS-1$
 144  
         }
 145  
 
 146  156
         if ( f.exists() )
 147  
         {
 148  28
             if ( !f.delete() )
 149  
             {
 150  
                 try
 151  
                 {
 152  0
                     FileUtils.forceDelete( f );
 153  
                 }
 154  0
                 catch ( IOException e )
 155  
                 {
 156  0
                     throw new MojoExecutionException( Messages.getString( "EclipseCleanMojo.failedtodelete", //$NON-NLS-1$
 157  
                                                                           new Object[] {
 158  
                                                                               f.getName(),
 159  
                                                                               f.getAbsolutePath() } ) );
 160  0
                 }
 161  
             }
 162  
         }
 163  
         else
 164  
         {
 165  128
             getLog().debug( Messages.getString( "EclipseCleanMojo.nofilefound", f.getName() ) ); //$NON-NLS-1$
 166  
         }
 167  156
     }
 168  
 
 169  
     /**
 170  
      * Getter for <code>basedir</code>.
 171  
      * @return Returns the basedir.
 172  
      */
 173  
     public File getBasedir()
 174  
     {
 175  0
         return this.basedir;
 176  
     }
 177  
 
 178  
     /**
 179  
      * Setter for <code>basedir</code>.
 180  
      * @param basedir The basedir to set.
 181  
      */
 182  
     public void setBasedir( File basedir )
 183  
     {
 184  22
         this.basedir = basedir;
 185  22
     }
 186  
 
 187  
 }