Skip to content

Commit 11c8c87

Browse files
authored
[MCLEAN-95] Add a fast clean option (#6)
1 parent de77b58 commit 11c8c87

File tree

3 files changed

+394
-4
lines changed

3 files changed

+394
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ under the License.
115115
<groupId>org.apache.maven</groupId>
116116
<artifactId>maven-core</artifactId>
117117
<version>${mavenVersion}</version>
118-
<scope>test</scope>
118+
<scope>provided</scope>
119119
</dependency>
120120
<dependency>
121121
<groupId>junit</groupId>

src/main/java/org/apache/maven/plugins/clean/CleanMojo.java

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* under the License.
2020
*/
2121

22+
import org.apache.maven.execution.MavenSession;
2223
import org.apache.maven.plugin.AbstractMojo;
2324
import org.apache.maven.plugin.MojoExecutionException;
2425
import org.apache.maven.plugins.annotations.Mojo;
@@ -48,6 +49,12 @@ public class CleanMojo
4849
extends AbstractMojo
4950
{
5051

52+
public static final String FAST_MODE_BACKGROUND = "background";
53+
54+
public static final String FAST_MODE_AT_END = "at-end";
55+
56+
public static final String FAST_MODE_DEFER = "defer";
57+
5158
/**
5259
* This is where build results go.
5360
*/
@@ -161,6 +168,49 @@ public class CleanMojo
161168
@Parameter( property = "maven.clean.excludeDefaultDirectories", defaultValue = "false" )
162169
private boolean excludeDefaultDirectories;
163170

171+
/**
172+
* Enables fast clean if possible. If set to <code>true</code>, when the plugin is executed, a directory to
173+
* be deleted will be atomically moved inside the <code>maven.clean.fastDir</code> directory and a thread will
174+
* be launched to delete the needed files in the background. When the build is completed, maven will wait
175+
* until all the files have been deleted. If any problem occurs during the atomic move of the directories,
176+
* the plugin will default to the traditional deletion mechanism.
177+
*
178+
* @since 3.2
179+
*/
180+
@Parameter( property = "maven.clean.fast", defaultValue = "false" )
181+
private boolean fast;
182+
183+
/**
184+
* When fast clean is specified, the <code>fastDir</code> property will be used as the location where directories
185+
* to be deleted will be moved prior to background deletion. If not specified, the
186+
* <code>${maven.multiModuleProjectDirectory}/target/.clean</code> directory will be used. If the
187+
* <code>${build.directory}</code> has been modified, you'll have to adjust this property explicitly.
188+
* In order for fast clean to work correctly, this directory and the various directories that will be deleted
189+
* should usually reside on the same volume. The exact conditions are system dependant though, but if an atomic
190+
* move is not supported, the standard deletion mechanism will be used.
191+
*
192+
* @since 3.2
193+
* @see #fast
194+
*/
195+
@Parameter( property = "maven.clean.fastDir" )
196+
private File fastDir;
197+
198+
/**
199+
* Mode to use when using fast clean. Values are: <code>background</code> to start deletion immediately and
200+
* waiting for all files to be deleted when the session ends, <code>at-end</code> to indicate that the actual
201+
* deletion should be performed synchronously when the session ends, or <code>defer</code> to specify that
202+
* the actual file deletion should be started in the background when the session ends (this should only be used
203+
* when maven is embedded in a long running process).
204+
*
205+
* @since 3.2
206+
* @see #fast
207+
*/
208+
@Parameter( property = "maven.clean.fastMode", defaultValue = FAST_MODE_BACKGROUND )
209+
private String fastMode;
210+
211+
@Parameter( defaultValue = "${session}", readonly = true )
212+
private MavenSession session;
213+
164214
/**
165215
* Deletes file-sets in the following project build directory order: (source) directory, output directory, test
166216
* directory, report directory, and then the additional file-sets.
@@ -177,7 +227,36 @@ public void execute()
177227
return;
178228
}
179229

180-
Cleaner cleaner = new Cleaner( getLog(), isVerbose() );
230+
String multiModuleProjectDirectory = session != null
231+
? session.getSystemProperties().getProperty( "maven.multiModuleProjectDirectory" ) : null;
232+
File fastDir;
233+
if ( fast && this.fastDir != null )
234+
{
235+
fastDir = this.fastDir;
236+
}
237+
else if ( fast && multiModuleProjectDirectory != null )
238+
{
239+
fastDir = new File( multiModuleProjectDirectory, "target/.clean" );
240+
}
241+
else
242+
{
243+
fastDir = null;
244+
if ( fast )
245+
{
246+
getLog().warn( "Fast clean requires maven 3.3.1 or newer, "
247+
+ "or an explicit directory to be specified with the 'fastDir' configuration of "
248+
+ "this plugin, or the 'maven.clean.fastDir' user property to be set." );
249+
}
250+
}
251+
if ( fast && !FAST_MODE_BACKGROUND.equals( fastMode )
252+
&& !FAST_MODE_AT_END.equals( fastMode )
253+
&& !FAST_MODE_DEFER.equals( fastMode ) )
254+
{
255+
throw new IllegalArgumentException( "Illegal value '" + fastMode + "' for fastMode. Allowed values are '"
256+
+ FAST_MODE_BACKGROUND + "', '" + FAST_MODE_AT_END + "' and '" + FAST_MODE_DEFER + "'." );
257+
}
258+
259+
Cleaner cleaner = new Cleaner( session, getLog(), isVerbose(), fastDir, fastMode );
181260

182261
try
183262
{

0 commit comments

Comments
 (0)