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 org.apache.maven.plugin.AbstractMojo;
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.eclipse.writers.workspace.EclipseWorkspaceWriter;
24  import org.apache.maven.plugin.ide.IdeUtils;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  
28  import java.io.File;
29  
30  /**
31   * Deletes the .project, .classpath, .wtpmodules files and .settings folder used by Eclipse.
32   */
33  @Mojo( name = "clean" )
34  public class EclipseCleanMojo
35      extends AbstractMojo
36  {
37  
38      /**
39       * Definition file for Eclipse Web Tools project.
40       */
41      private static final String FILE_DOT_WTPMODULES = ".wtpmodules"; //$NON-NLS-1$
42  
43      /**
44       * Classpath definition file for an Eclipse Java project.
45       */
46      private static final String FILE_DOT_CLASSPATH = ".classpath"; //$NON-NLS-1$
47  
48      /**
49       * Project definition file for an Eclipse Project.
50       */
51      private static final String FILE_DOT_PROJECT = ".project"; //$NON-NLS-1$
52  
53      /**
54       * Packaging for the current project.
55       */
56      @Parameter( property = "project.packaging" )
57      private String packaging;
58  
59      /**
60       * The root directory of the project
61       */
62      @Parameter( property = "basedir" )
63      private File basedir;
64  
65      /**
66       * Skip the operation when true.
67       */
68      @Parameter( property = "eclipse.skip", defaultValue = "false" )
69      private boolean skip;
70  
71      /**
72       * additional generic configuration files for eclipse
73       */
74      @Parameter
75      private EclipseConfigFile[] additionalConfig;
76  
77      /**
78       * @see org.apache.maven.plugin.AbstractMojo#execute()
79       */
80      public void execute()
81          throws MojoExecutionException
82      {
83          if ( skip )
84          {
85              return;
86          }
87  
88          if ( Constants.PROJECT_PACKAGING_POM.equals( this.packaging ) )
89          {
90              return;
91          }
92  
93          delete( new File( basedir, FILE_DOT_PROJECT ) );
94          delete( new File( basedir, FILE_DOT_CLASSPATH ) );
95          delete( new File( basedir, FILE_DOT_WTPMODULES ) );
96  
97          delete( new File( basedir, EclipseWorkspaceWriter.DIR_DOT_SETTINGS ) );
98  
99          if ( additionalConfig != null )
100         {
101             for (EclipseConfigFile anAdditionalConfig : additionalConfig) {
102                 delete(new File(basedir, anAdditionalConfig.getName()));
103             }
104         }
105 
106         cleanExtras();
107     }
108 
109     protected void cleanExtras()
110         throws MojoExecutionException
111     {
112         // extension point.
113     }
114 
115     /**
116      * Delete a file, handling log messages and exceptions
117      *
118      * @param f File to be deleted
119      * @throws MojoExecutionException only if a file exists and can't be deleted
120      */
121     protected void delete( File f )
122         throws MojoExecutionException
123     {
124         IdeUtils.delete( f, getLog() );
125     }
126 
127     /**
128      * Getter for <code>basedir</code>.
129      *
130      * @return Returns the basedir.
131      */
132     public File getBasedir()
133     {
134         return this.basedir;
135     }
136 
137     /**
138      * Setter for <code>basedir</code>.
139      *
140      * @param basedir The basedir to set.
141      */
142     public void setBasedir( File basedir )
143     {
144         this.basedir = basedir;
145     }
146 
147     /**
148      * @return the packaging
149      */
150     public String getPackaging()
151     {
152         return this.packaging;
153     }
154 
155     /**
156      * @param packaging the packaging to set
157      */
158     public void setPackaging( String packaging )
159     {
160         this.packaging = packaging;
161     }
162 
163 }