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 * additional generic configuration files for eclipse
100 *
101 * @parameter
102 */
103 private EclipseConfigFile[] additionalConfig;
104
105 /**
106 * @see org.apache.maven.plugin.AbstractMojo#execute()
107 */
108 public void execute()
109 throws MojoExecutionException
110 {
111 if ( skip )
112 {
113 return;
114 }
115
116 if ( Constants.PROJECT_PACKAGING_POM.equals( this.packaging ) )
117 {
118 return;
119 }
120
121 delete( new File( basedir, FILE_DOT_PROJECT ) );
122 delete( new File( basedir, FILE_DOT_CLASSPATH ) );
123 delete( new File( basedir, FILE_DOT_WTPMODULES ) );
124
125 delete( new File( basedir, FILE_DOT_COMPONENT ) );
126 delete( new File( basedir, FILE_DOT_COMPONENT_15 ) );
127 delete( new File( basedir, FILE_FACET_CORE_XML ) );
128 delete( new File( basedir, FILE_ECLIPSE_JDT_CORE_PREFS ) );
129
130 File settingsDir = new File( basedir, DIR_DOT_SETTINGS );
131 if ( settingsDir.exists() && settingsDir.isDirectory() && settingsDir.list().length == 0 )
132 {
133 delete( settingsDir );
134 }
135
136 if ( additionalConfig != null )
137 {
138 for ( int i = 0; i < additionalConfig.length; i++ )
139 {
140 delete( new File( basedir, additionalConfig[i].getName() ) );
141 }
142 }
143
144 cleanExtras();
145 }
146
147 protected void cleanExtras()
148 throws MojoExecutionException
149 {
150 // extension point.
151 }
152
153 /**
154 * Delete a file, handling log messages and exceptions
155 *
156 * @param f File to be deleted
157 * @throws MojoExecutionException only if a file exists and can't be deleted
158 */
159 protected void delete( File f )
160 throws MojoExecutionException
161 {
162 if ( f.isDirectory() )
163 {
164 getLog().info( Messages.getString( "EclipseCleanMojo.deletingDirectory", f.getName() ) ); //$NON-NLS-1$
165 }
166 else
167 {
168 getLog().info( Messages.getString( "EclipseCleanMojo.deletingFile", f.getName() ) ); //$NON-NLS-1$
169 }
170
171 if ( f.exists() )
172 {
173 if ( !f.delete() )
174 {
175 try
176 {
177 FileUtils.forceDelete( f );
178 }
179 catch ( IOException e )
180 {
181 throw new MojoExecutionException( Messages.getString( "EclipseCleanMojo.failedtodelete", //$NON-NLS-1$
182 new Object[] { f.getName(),
183 f.getAbsolutePath() } ) );
184 }
185 }
186 }
187 else
188 {
189 getLog().debug( Messages.getString( "EclipseCleanMojo.nofilefound", f.getName() ) ); //$NON-NLS-1$
190 }
191 }
192
193 /**
194 * Getter for <code>basedir</code>.
195 *
196 * @return Returns the basedir.
197 */
198 public File getBasedir()
199 {
200 return this.basedir;
201 }
202
203 /**
204 * Setter for <code>basedir</code>.
205 *
206 * @param basedir The basedir to set.
207 */
208 public void setBasedir( File basedir )
209 {
210 this.basedir = basedir;
211 }
212
213 /**
214 * @return the packaging
215 */
216 public String getPackaging()
217 {
218 return this.packaging;
219 }
220
221 /**
222 * @param packaging the packaging to set
223 */
224 public void setPackaging( String packaging )
225 {
226 this.packaging = packaging;
227 }
228
229 }