Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CleanMojo |
|
| 5.0;5 |
1 | package org.apache.maven.plugin.clean; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import org.apache.maven.plugin.AbstractMojo; | |
23 | import org.apache.maven.plugin.MojoExecutionException; | |
24 | ||
25 | import java.io.File; | |
26 | import java.io.IOException; | |
27 | ||
28 | /** | |
29 | * Goal which cleans the build. | |
30 | * | |
31 | * <P>This attempts to clean a project's working directory of the files that | |
32 | * were generated at build-time. By default, it discovers and deletes the | |
33 | * directories configured in <code>project.build.directory</code>, | |
34 | * <code>project.build.outputDirectory</code>, | |
35 | * <code>project.build.testOutputDirectory</code>, and | |
36 | * <code>project.reporting.outputDirectory</code>. </P> | |
37 | * | |
38 | * <P>Files outside the default may also be included in the deletion by | |
39 | * configuring the <code>filesets</code> tag.</P> | |
40 | * | |
41 | * @author <a href="mailto:evenisse@maven.org">Emmanuel Venisse</a> | |
42 | * @version $Id$ | |
43 | * @goal clean | |
44 | * @since 2.0 | |
45 | * @see org.apache.maven.plugin.clean.Fileset | |
46 | */ | |
47 | 8 | public class CleanMojo |
48 | extends AbstractMojo | |
49 | { | |
50 | ||
51 | /** | |
52 | * This is where build results go. | |
53 | * | |
54 | * @parameter default-value="${project.build.directory}" | |
55 | * @required | |
56 | * @readonly | |
57 | */ | |
58 | private File directory; | |
59 | ||
60 | /** | |
61 | * This is where compiled classes go. | |
62 | * | |
63 | * @parameter default-value="${project.build.outputDirectory}" | |
64 | * @required | |
65 | * @readonly | |
66 | */ | |
67 | private File outputDirectory; | |
68 | ||
69 | /** | |
70 | * This is where compiled test classes go. | |
71 | * | |
72 | * @parameter default-value="${project.build.testOutputDirectory}" | |
73 | * @required | |
74 | * @readonly | |
75 | */ | |
76 | private File testOutputDirectory; | |
77 | ||
78 | /** | |
79 | * This is where the site plugin generates its pages. | |
80 | * | |
81 | * @parameter default-value="${project.reporting.outputDirectory}" | |
82 | * @required | |
83 | * @readonly | |
84 | * @since 2.1.1 | |
85 | */ | |
86 | private File reportDirectory; | |
87 | ||
88 | /** | |
89 | * Sets whether the plugin runs in verbose mode. As of plugin version 2.3, the default value is derived from Maven's | |
90 | * global debug flag (compare command line switch <code>-X</code>). | |
91 | * | |
92 | * @parameter expression="${clean.verbose}" | |
93 | * @since 2.1 | |
94 | */ | |
95 | private Boolean verbose; | |
96 | ||
97 | /** | |
98 | * The list of file sets to delete, in addition to the default directories. | |
99 | * | |
100 | * @parameter | |
101 | * @since 2.1 | |
102 | */ | |
103 | private Fileset[] filesets; | |
104 | ||
105 | /** | |
106 | * Sets whether the plugin should follow symbolic links while deleting files from the default output directories of | |
107 | * the project. Not following symlinks requires more IO operations and heap memory, regardless whether symlinks are | |
108 | * actually present. So projects with a huge output directory that knowingly does not contain symlinks can improve | |
109 | * performance by setting this parameter to <code>true</code>. | |
110 | * | |
111 | * @parameter expression="${clean.followSymLinks}" default-value="false" | |
112 | * @since 2.1 | |
113 | */ | |
114 | private boolean followSymLinks; | |
115 | ||
116 | /** | |
117 | * Disables the plugin execution. | |
118 | * | |
119 | * @parameter expression="${clean.skip}" default-value="false" | |
120 | * @since 2.2 | |
121 | */ | |
122 | private boolean skip; | |
123 | ||
124 | /** | |
125 | * Indicates whether the build will continue even if there are clean errors. | |
126 | * | |
127 | * @parameter expression="${maven.clean.failOnError}" default-value="true" | |
128 | * @since 2.2 | |
129 | */ | |
130 | private boolean failOnError; | |
131 | ||
132 | /** | |
133 | * Disables the deletion of the default output directories configured for a project. If set to <code>true</code>, | |
134 | * only the files/directories selected via the parameter {@link #filesets} will be deleted. | |
135 | * | |
136 | * @parameter expression="${clean.excludeDefaultDirectories}" default-value="false" | |
137 | * @since 2.3 | |
138 | */ | |
139 | private boolean excludeDefaultDirectories; | |
140 | ||
141 | /** | |
142 | * Deletes file-sets in the following project build directory order: (source) directory, output directory, test | |
143 | * directory, report directory, and then the additional file-sets. | |
144 | * | |
145 | * @see org.apache.maven.plugin.Mojo#execute() | |
146 | * @throws MojoExecutionException When a directory failed to get deleted. | |
147 | */ | |
148 | public void execute() | |
149 | throws MojoExecutionException | |
150 | { | |
151 | 8 | if ( skip ) |
152 | { | |
153 | 0 | getLog().info( "Clean is skipped." ); |
154 | 0 | return; |
155 | } | |
156 | ||
157 | 8 | Cleaner cleaner = new Cleaner( getLog(), isVerbose() ); |
158 | ||
159 | try | |
160 | { | |
161 | 8 | File[] directories = getDirectories(); |
162 | 32 | for ( int i = 0; i < directories.length; i++ ) |
163 | { | |
164 | 26 | File directory = directories[i]; |
165 | 26 | if ( directory != null ) |
166 | { | |
167 | 10 | cleaner.delete( directory, null, followSymLinks, failOnError ); |
168 | } | |
169 | } | |
170 | ||
171 | 6 | if ( filesets != null ) |
172 | { | |
173 | 3 | for ( int i = 0; i < filesets.length; i++ ) |
174 | { | |
175 | 2 | Fileset fileset = filesets[i]; |
176 | 2 | if ( fileset.getDirectory() == null ) |
177 | { | |
178 | 0 | throw new MojoExecutionException( "Missing base directory for " + fileset ); |
179 | } | |
180 | 2 | GlobSelector selector = new GlobSelector( fileset.getIncludes(), fileset.getExcludes() ); |
181 | 2 | cleaner.delete( fileset.getDirectory(), selector, fileset.isFollowSymlinks(), failOnError ); |
182 | } | |
183 | } | |
184 | } | |
185 | 2 | catch ( IOException e ) |
186 | { | |
187 | 2 | throw new MojoExecutionException( "Failed to clean project: " + e.getMessage(), e ); |
188 | 6 | } |
189 | 6 | } |
190 | ||
191 | /** | |
192 | * Indicates whether verbose output is enabled. | |
193 | * | |
194 | * @return <code>true</code> if verbose output is enabled, <code>false</code> otherwise. | |
195 | */ | |
196 | private boolean isVerbose() | |
197 | { | |
198 | 8 | return ( verbose != null ) ? verbose.booleanValue() : getLog().isDebugEnabled(); |
199 | } | |
200 | ||
201 | /** | |
202 | * Gets the directories to clean (if any). The returned array may contain null entries. | |
203 | * | |
204 | * @return The directories to clean or an empty array if none, never <code>null</code>. | |
205 | */ | |
206 | private File[] getDirectories() | |
207 | { | |
208 | File[] directories; | |
209 | 8 | if ( excludeDefaultDirectories ) |
210 | { | |
211 | 0 | directories = new File[0]; |
212 | } | |
213 | else | |
214 | { | |
215 | 8 | directories = new File[] { directory, outputDirectory, testOutputDirectory, reportDirectory }; |
216 | } | |
217 | 8 | return directories; |
218 | } | |
219 | ||
220 | } |