Author: rl...@jb... Date: 2005-12-06 13:58:06 -0500 (Tue, 06 Dec 2005) New Revision: 1712 Added: trunk/labs/jbossbuild/projects/ trunk/labs/jbossbuild/projects/maven-plugins/ trunk/labs/jbossbuild/projects/maven-plugins/pom.xml trunk/labs/jbossbuild/projects/maven-plugins/src/ trunk/labs/jbossbuild/projects/maven-plugins/src/main/ trunk/labs/jbossbuild/projects/maven-plugins/src/main/java/ trunk/labs/jbossbuild/projects/maven-plugins/src/main/java/org/ trunk/labs/jbossbuild/projects/maven-plugins/src/main/java/org/apache/ trunk/labs/jbossbuild/projects/maven-plugins/src/main/java/org/apache/maven/ trunk/labs/jbossbuild/projects/maven-plugins/src/main/java/org/apache/maven/plugin/ trunk/labs/jbossbuild/projects/maven-plugins/src/main/java/org/apache/maven/plugin/clean/ trunk/labs/jbossbuild/projects/maven-plugins/src/main/java/org/apache/maven/plugin/clean/CleanMojo.java Log: Initial entry of maven clean plugin Added: trunk/labs/jbossbuild/projects/maven-plugins/pom.xml =================================================================== --- trunk/labs/jbossbuild/projects/maven-plugins/pom.xml 2005-12-06 17:19:16 UTC (rev 1711) +++ trunk/labs/jbossbuild/projects/maven-plugins/pom.xml 2005-12-06 18:58:06 UTC (rev 1712) @@ -0,0 +1,13 @@ +<project> + <parent> + <artifactId>maven-plugin-parent</artifactId> + <groupId>org.apache.maven.plugins</groupId> + <version>2.0</version> + </parent> + <modelVersion>4.0.0</modelVersion> + <artifactId>maven-clean-plugin</artifactId> + <packaging>maven-plugin</packaging> + <name>Maven Clean Plugin</name> + <version>2.0.1-SNAPSHOT</version> + <inceptionYear>2001</inceptionYear> +</project> \ No newline at end of file Added: trunk/labs/jbossbuild/projects/maven-plugins/src/main/java/org/apache/maven/plugin/clean/CleanMojo.java =================================================================== --- trunk/labs/jbossbuild/projects/maven-plugins/src/main/java/org/apache/maven/plugin/clean/CleanMojo.java 2005-12-06 17:19:16 UTC (rev 1711) +++ trunk/labs/jbossbuild/projects/maven-plugins/src/main/java/org/apache/maven/plugin/clean/CleanMojo.java 2005-12-06 18:58:06 UTC (rev 1712) @@ -0,0 +1,163 @@ +package org.apache.maven.plugin.clean; + +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; + +import java.io.File; + +/** + * Goal which cleans the build. + + * @goal clean + * @author <a href="mailto:eve...@ma...">Emmanuel Venisse</a> + * @version $Id: CleanMojo.java 307230 2005-10-08 01:19:09Z brett $ + */ +public class CleanMojo + extends AbstractMojo +{ + private static final int DELETE_RETRY_SLEEP_MILLIS = 10; + + /** + * This is where build results go. + * + * @parameter expression="${project.build.directory}" + * @required + * @readonly + */ + private File directory; + + /** + * This is where compiled classes go. + * + * @parameter expression="${project.build.outputDirectory}" + * @required + * @readonly + */ + private File outputDirectory; + + /** + * This is where compiled test classes go. + * + * @parameter expression="${project.build.testOutputDirectory}" + * @required + * @readonly + */ + private File testOutputDirectory; + + public void execute() + throws MojoExecutionException + { + removeDirectory( directory ); + removeDirectory( outputDirectory ); + removeDirectory( testOutputDirectory ); + } + + private void removeDirectory( File dir ) + throws MojoExecutionException + { + if ( dir != null ) + { + if ( dir.exists() && dir.isDirectory() ) + { + getLog().info( "Deleting directory " + dir.getAbsolutePath() ); + removeDir( dir ); + } + } + } + + /** + * Accommodate Windows bug encountered in both Sun and IBM JDKs. + * Others possible. If the delete does not work, call System.gc(), + * wait a little and try again. + */ + private boolean delete( File f ) + { + if ( !f.delete() ) + { + if ( System.getProperty( "os.name" ).toLowerCase().indexOf( "windows" ) > -1 ) + { + System.gc(); + } + try + { + Thread.sleep( DELETE_RETRY_SLEEP_MILLIS ); + return f.delete(); + } + catch ( InterruptedException ex ) + { + return f.delete(); + } + } + return true; + } + + /** + * Delete a directory + * + * @param d the directory to delete + */ + protected void removeDir( File d ) + throws MojoExecutionException + { + String[] list = d.list(); + if ( list == null ) + { + list = new String[0]; + } + for ( int i = 0; i < list.length; i++ ) + { + String s = list[i]; + File f = new File( d, s ); + if ( f.isDirectory() ) + { + removeDir( f ); + } + else + { + if ( !delete( f ) ) + { + String message = "Unable to delete file " + f.getAbsolutePath(); +// TODO:... +// if ( failOnError ) +// { + throw new MojoExecutionException( message ); +// } +// else +// { +// getLog().info( message ); +// } + } + } + } + + if ( !delete( d ) ) + { + String message = "Unable to delete directory " + d.getAbsolutePath(); +// TODO:... +// if ( failOnError ) +// { + throw new MojoExecutionException( message ); +// } +// else +// { +// getLog().info( message ); +// } + } + } +} |