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