18
18
*/
19
19
package org .apache .maven .plugins .clean ;
20
20
21
- import java .io .File ;
22
21
import java .io .IOException ;
22
+ import java .nio .file .Path ;
23
+ import java .nio .file .Paths ;
23
24
24
- import org .apache .maven .execution .MavenSession ;
25
- import org .apache .maven .plugin .AbstractMojo ;
26
- import org .apache .maven .plugin .MojoExecutionException ;
27
- import org .apache .maven .plugins .annotations .Mojo ;
28
- import org .apache .maven .plugins .annotations .Parameter ;
25
+ import org .apache .maven .api .Session ;
26
+ import org .apache .maven .api .di .Inject ;
27
+ import org .apache .maven .api .plugin .Log ;
28
+ import org .apache .maven .api .plugin .MojoException ;
29
+ import org .apache .maven .api .plugin .annotations .Mojo ;
30
+ import org .apache .maven .api .plugin .annotations .Parameter ;
29
31
30
32
/**
31
33
* Goal which cleans the build.
43
45
* @see org.apache.maven.plugins.clean.Fileset
44
46
* @since 2.0
45
47
*/
46
- @ Mojo (name = "clean" , threadSafe = true )
47
- public class CleanMojo extends AbstractMojo {
48
+ @ Mojo (name = "clean" )
49
+ public class CleanMojo implements org . apache . maven . api . plugin . Mojo {
48
50
49
51
public static final String FAST_MODE_BACKGROUND = "background" ;
50
52
51
53
public static final String FAST_MODE_AT_END = "at-end" ;
52
54
53
55
public static final String FAST_MODE_DEFER = "defer" ;
54
56
57
+ @ Inject
58
+ private Log logger ;
59
+
55
60
/**
56
61
* This is where build results go.
57
62
*/
58
63
@ Parameter (defaultValue = "${project.build.directory}" , readonly = true , required = true )
59
- private File directory ;
64
+ private Path directory ;
60
65
61
66
/**
62
67
* This is where compiled classes go.
63
68
*/
64
69
@ Parameter (defaultValue = "${project.build.outputDirectory}" , readonly = true , required = true )
65
- private File outputDirectory ;
70
+ private Path outputDirectory ;
66
71
67
72
/**
68
73
* This is where compiled test classes go.
69
74
*/
70
75
@ Parameter (defaultValue = "${project.build.testOutputDirectory}" , readonly = true , required = true )
71
- private File testOutputDirectory ;
76
+ private Path testOutputDirectory ;
72
77
73
78
/**
74
79
* This is where the site plugin generates its pages.
75
80
*
76
81
* @since 2.1.1
77
82
*/
78
83
@ Parameter (defaultValue = "${project.build.outputDirectory}" , readonly = true , required = true )
79
- private File reportDirectory ;
84
+ private Path reportDirectory ;
80
85
81
86
/**
82
87
* Sets whether the plugin runs in verbose mode. As of plugin version 2.3, the default value is derived from Maven's
@@ -186,11 +191,11 @@ public class CleanMojo extends AbstractMojo {
186
191
* should usually reside on the same volume. The exact conditions are system dependant though, but if an atomic
187
192
* move is not supported, the standard deletion mechanism will be used.
188
193
*
189
- * @see #fast
190
194
* @since 3.2
195
+ * @see #fast
191
196
*/
192
197
@ Parameter (property = "maven.clean.fastDir" )
193
- private File fastDir ;
198
+ private Path fastDir ;
194
199
195
200
/**
196
201
* Mode to use when using fast clean. Values are: <code>background</code> to start deletion immediately and
@@ -199,35 +204,35 @@ public class CleanMojo extends AbstractMojo {
199
204
* the actual file deletion should be started in the background when the session ends (this should only be used
200
205
* when maven is embedded in a long running process).
201
206
*
202
- * @see #fast
203
207
* @since 3.2
208
+ * @see #fast
204
209
*/
205
210
@ Parameter (property = "maven.clean.fastMode" , defaultValue = FAST_MODE_BACKGROUND )
206
211
private String fastMode ;
207
212
208
- @ Parameter ( defaultValue = "${session}" , readonly = true )
209
- private MavenSession session ;
213
+ @ Inject
214
+ private Session session ;
210
215
211
216
/**
212
217
* Deletes file-sets in the following project build directory order: (source) directory, output directory, test
213
218
* directory, report directory, and then the additional file-sets.
214
219
*
215
- * @throws MojoExecutionException When a directory failed to get deleted.
216
- * @see org.apache.maven.plugin.Mojo#execute()
220
+ * @throws MojoException When a directory failed to get deleted.
221
+ * @see org.apache.maven.api. plugin.Mojo#execute()
217
222
*/
218
- public void execute () throws MojoExecutionException {
223
+ public void execute () {
219
224
if (skip ) {
220
225
getLog ().info ("Clean is skipped." );
221
226
return ;
222
227
}
223
228
224
229
String multiModuleProjectDirectory =
225
- session != null ? session .getSystemProperties ().getProperty ("maven.multiModuleProjectDirectory" ) : null ;
226
- File fastDir ;
230
+ session != null ? session .getSystemProperties ().get ("maven.multiModuleProjectDirectory" ) : null ;
231
+ Path fastDir ;
227
232
if (fast && this .fastDir != null ) {
228
233
fastDir = this .fastDir ;
229
234
} else if (fast && multiModuleProjectDirectory != null ) {
230
- fastDir = new File (multiModuleProjectDirectory , "target/.clean" );
235
+ fastDir = Paths . get (multiModuleProjectDirectory , "target/.clean" );
231
236
} else {
232
237
fastDir = null ;
233
238
if (fast ) {
@@ -247,7 +252,7 @@ public void execute() throws MojoExecutionException {
247
252
Cleaner cleaner = new Cleaner (session , getLog (), isVerbose (), fastDir , fastMode );
248
253
249
254
try {
250
- for (File directoryItem : getDirectories ()) {
255
+ for (Path directoryItem : getDirectories ()) {
251
256
if (directoryItem != null ) {
252
257
cleaner .delete (directoryItem , null , followSymLinks , failOnError , retryOnError );
253
258
}
@@ -256,7 +261,7 @@ public void execute() throws MojoExecutionException {
256
261
if (filesets != null ) {
257
262
for (Fileset fileset : filesets ) {
258
263
if (fileset .getDirectory () == null ) {
259
- throw new MojoExecutionException ("Missing base directory for " + fileset );
264
+ throw new MojoException ("Missing base directory for " + fileset );
260
265
}
261
266
final String [] includes = fileset .getIncludes ();
262
267
final String [] excludes = fileset .getExcludes ();
@@ -273,8 +278,9 @@ public void execute() throws MojoExecutionException {
273
278
fileset .getDirectory (), selector , fileset .isFollowSymlinks (), failOnError , retryOnError );
274
279
}
275
280
}
281
+
276
282
} catch (IOException e ) {
277
- throw new MojoExecutionException ("Failed to clean project: " + e .getMessage (), e );
283
+ throw new MojoException ("Failed to clean project: " + e .getMessage (), e );
278
284
}
279
285
}
280
286
@@ -292,13 +298,17 @@ private boolean isVerbose() {
292
298
*
293
299
* @return The directories to clean or an empty array if none, never <code>null</code>.
294
300
*/
295
- private File [] getDirectories () {
296
- File [] directories ;
301
+ private Path [] getDirectories () {
302
+ Path [] directories ;
297
303
if (excludeDefaultDirectories ) {
298
- directories = new File [0 ];
304
+ directories = new Path [0 ];
299
305
} else {
300
- directories = new File [] {directory , outputDirectory , testOutputDirectory , reportDirectory };
306
+ directories = new Path [] {directory , outputDirectory , testOutputDirectory , reportDirectory };
301
307
}
302
308
return directories ;
303
309
}
310
+
311
+ private Log getLog () {
312
+ return logger ;
313
+ }
304
314
}
0 commit comments