View Javadoc

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.io.IOException;
23  
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.codehaus.plexus.util.FileUtils;
27  
28  /**
29   * Deletes the .project, .classpath, .wtpmodules files and .settings folder used by Eclipse.
30   * 
31   * @goal clean
32   */
33  public class EclipseCleanMojo
34      extends AbstractMojo
35  {
36  
37      /**
38       * Definition file for Eclipse Web Tools project.
39       */
40      private static final String FILE_DOT_WTPMODULES = ".wtpmodules"; //$NON-NLS-1$
41  
42      /**
43       * Classpath definition file for an Eclipse Java project.
44       */
45      private static final String FILE_DOT_CLASSPATH = ".classpath"; //$NON-NLS-1$
46  
47      /**
48       * Project definition file for an Eclipse Project.
49       */
50      private static final String FILE_DOT_PROJECT = ".project"; //$NON-NLS-1$
51  
52      /**
53       * Web Project definition file for Eclipse Web Tools Project (Release 1.0x).
54       */
55      private static final String DIR_DOT_SETTINGS = ".settings"; //$NON-NLS-1$
56  
57      /**
58       * File name where the WTP component settings will be stored - WTP 1.0 name.
59       */
60      private static final String FILE_DOT_COMPONENT = ".settings/.component"; //$NON-NLS-1$
61  
62      /**
63       * File name where the WTP component settings will be stored - WTP 1.5 name.
64       */
65      private static final String FILE_DOT_COMPONENT_15 = ".settings/org.eclipse.wst.common.component"; //$NON-NLS-1$
66  
67      /**
68       * File name where Eclipse Project's Facet configuration will be stored.
69       */
70      private static final String FILE_FACET_CORE_XML = ".settings/org.eclipse.wst.common.project.facet.core.xml"; //$NON-NLS-1$
71  
72      /**
73       * General project preferences.
74       */
75      private static final String FILE_ECLIPSE_JDT_CORE_PREFS = ".settings/org.eclipse.jdt.core.prefs"; //$NON-NLS-1$
76  
77      /**
78       * Packaging for the current project.
79       * 
80       * @parameter expression="${project.packaging}"
81       */
82      private String packaging;
83  
84      /**
85       * The root directory of the project
86       * 
87       * @parameter expression="${basedir}"
88       */
89      private File basedir;
90  
91      /**
92       * Skip the operation when true.
93       * 
94       * @parameter expression="${eclipse.skip}" default-value="false"
95       */
96      private boolean skip;
97  
98      /**
99       * @see org.apache.maven.plugin.AbstractMojo#execute()
100      */
101     public void execute()
102         throws MojoExecutionException
103     {
104         if ( skip )
105         {
106             return;
107         }
108 
109         if ( Constants.PROJECT_PACKAGING_POM.equals( this.packaging ) )
110         {
111             return;
112         }
113 
114         delete( new File( basedir, FILE_DOT_PROJECT ) );
115         delete( new File( basedir, FILE_DOT_CLASSPATH ) );
116         delete( new File( basedir, FILE_DOT_WTPMODULES ) );
117 
118         delete( new File( basedir, FILE_DOT_COMPONENT ) );
119         delete( new File( basedir, FILE_DOT_COMPONENT_15 ) );
120         delete( new File( basedir, FILE_FACET_CORE_XML ) );
121         delete( new File( basedir, FILE_ECLIPSE_JDT_CORE_PREFS ) );
122 
123         File settingsDir = new File( basedir, DIR_DOT_SETTINGS );
124         if ( settingsDir.exists() && settingsDir.isDirectory() && settingsDir.list().length == 0 )
125         {
126             delete( settingsDir );
127         }
128 
129         cleanExtras();
130     }
131 
132     protected void cleanExtras()
133         throws MojoExecutionException
134     {
135         // extension point.
136     }
137 
138     /**
139      * Delete a file, handling log messages and exceptions
140      * 
141      * @param f File to be deleted
142      * @throws MojoExecutionException only if a file exists and can't be deleted
143      */
144     protected void delete( File f )
145         throws MojoExecutionException
146     {
147         if ( f.isDirectory() )
148         {
149             getLog().info( Messages.getString( "EclipseCleanMojo.deletingDirectory", f.getName() ) ); //$NON-NLS-1$
150         }
151         else
152         {
153             getLog().info( Messages.getString( "EclipseCleanMojo.deletingFile", f.getName() ) ); //$NON-NLS-1$
154         }
155 
156         if ( f.exists() )
157         {
158             if ( !f.delete() )
159             {
160                 try
161                 {
162                     FileUtils.forceDelete( f );
163                 }
164                 catch ( IOException e )
165                 {
166                     throw new MojoExecutionException( Messages.getString( "EclipseCleanMojo.failedtodelete", //$NON-NLS-1$
167                                                                           new Object[] { f.getName(),
168                                                                               f.getAbsolutePath() } ) );
169                 }
170             }
171         }
172         else
173         {
174             getLog().debug( Messages.getString( "EclipseCleanMojo.nofilefound", f.getName() ) ); //$NON-NLS-1$
175         }
176     }
177 
178     /**
179      * Getter for <code>basedir</code>.
180      * 
181      * @return Returns the basedir.
182      */
183     public File getBasedir()
184     {
185         return this.basedir;
186     }
187 
188     /**
189      * Setter for <code>basedir</code>.
190      * 
191      * @param basedir The basedir to set.
192      */
193     public void setBasedir( File basedir )
194     {
195         this.basedir = basedir;
196     }
197 
198     /**
199      * @return the packaging
200      */
201     public String getPackaging()
202     {
203         return this.packaging;
204     }
205 
206     /**
207      * @param packaging the packaging to set
208      */
209     public void setPackaging( String packaging )
210     {
211         this.packaging = packaging;
212     }
213 
214 }