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  
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.eclipse.writers.workspace.EclipseWorkspaceWriter;
26  import org.apache.maven.plugin.ide.IdeUtils;
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       * Packaging for the current project.
54       * 
55       * @parameter expression="${project.packaging}"
56       */
57      private String packaging;
58  
59      /**
60       * The root directory of the project
61       * 
62       * @parameter expression="${basedir}"
63       */
64      private File basedir;
65  
66      /**
67       * Skip the operation when true.
68       * 
69       * @parameter expression="${eclipse.skip}" default-value="false"
70       */
71      private boolean skip;
72  
73      /**
74       * additional generic configuration files for eclipse
75       * 
76       * @parameter
77       */
78      private EclipseConfigFile[] additionalConfig;
79  
80      /**
81       * @see org.apache.maven.plugin.AbstractMojo#execute()
82       */
83      public void execute()
84          throws MojoExecutionException
85      {
86          if ( skip )
87          {
88              return;
89          }
90  
91          if ( Constants.PROJECT_PACKAGING_POM.equals( this.packaging ) )
92          {
93              return;
94          }
95  
96          delete( new File( basedir, FILE_DOT_PROJECT ) );
97          delete( new File( basedir, FILE_DOT_CLASSPATH ) );
98          delete( new File( basedir, FILE_DOT_WTPMODULES ) );
99  
100         File settingsDir = new File( basedir, EclipseWorkspaceWriter.DIR_DOT_SETTINGS );
101         if ( settingsDir.exists() && settingsDir.isDirectory() && settingsDir.list().length == 0 )
102         {
103             delete( settingsDir );
104         }
105 
106         if ( additionalConfig != null )
107         {
108             for ( int i = 0; i < additionalConfig.length; i++ )
109             {
110                 delete( new File( basedir, additionalConfig[i].getName() ) );
111             }
112         }
113 
114         cleanExtras();
115     }
116 
117     protected void cleanExtras()
118         throws MojoExecutionException
119     {
120         // extension point.
121     }
122 
123     /**
124      * Delete a file, handling log messages and exceptions
125      * 
126      * @param f File to be deleted
127      * @throws MojoExecutionException only if a file exists and can't be deleted
128      */
129     protected void delete( File f )
130         throws MojoExecutionException
131     {
132         IdeUtils.delete( f, getLog() );
133     }
134 
135     /**
136      * Getter for <code>basedir</code>.
137      * 
138      * @return Returns the basedir.
139      */
140     public File getBasedir()
141     {
142         return this.basedir;
143     }
144 
145     /**
146      * Setter for <code>basedir</code>.
147      * 
148      * @param basedir The basedir to set.
149      */
150     public void setBasedir( File basedir )
151     {
152         this.basedir = basedir;
153     }
154 
155     /**
156      * @return the packaging
157      */
158     public String getPackaging()
159     {
160         return this.packaging;
161     }
162 
163     /**
164      * @param packaging the packaging to set
165      */
166     public void setPackaging( String packaging )
167     {
168         this.packaging = packaging;
169     }
170 
171 }