You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by vs...@apache.org on 2009/04/13 12:52:01 UTC

svn commit: r764400 - /maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntCleanMojo.java

Author: vsiveton
Date: Mon Apr 13 10:52:00 2009
New Revision: 764400

URL: http://svn.apache.org/viewvc?rev=764400&view=rev
Log:
MANT-47: ant:clean should not delete build.xml

o Add a user input handler and a force flag

Modified:
    maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntCleanMojo.java

Modified: maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntCleanMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntCleanMojo.java?rev=764400&r1=764399&r2=764400&view=diff
==============================================================================
--- maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntCleanMojo.java (original)
+++ maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntCleanMojo.java Mon Apr 13 10:52:00 2009
@@ -20,10 +20,14 @@
  */
 
 import java.io.File;
+import java.io.IOException;
+import java.util.Locale;
 
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.settings.Settings;
+import org.codehaus.plexus.components.interactivity.InputHandler;
 
 /**
  * Clean all Ant build files.
@@ -36,6 +40,24 @@
     extends AbstractMojo
 {
     /**
+     * The current user system settings for use in Maven.
+     *
+     * @parameter expression="${settings}"
+     * @required
+     * @readonly
+     * @since 2.1.1
+     */
+    private Settings settings;
+
+    /**
+     * Input handler, needed for command line handling.
+     *
+     * @component
+     * @since 2.1.1
+     */
+    private InputHandler inputHandler;
+
+    /**
      * The working project.
      *
      * @parameter default-value="${project}"
@@ -45,16 +67,20 @@
     private MavenProject project;
 
     /**
+     * Forcing the deletion of the custom <code>build.xml</code>.
+     *
+     * @parameter expression="${force}"
+     * @since 2.1.1
+     */
+    private boolean force;
+
+    /**
      * @see org.apache.maven.plugin.Mojo#execute()
      */
     public void execute()
         throws MojoExecutionException
     {
-        File buildXml = new File( project.getBasedir(), AntBuildWriter.DEFAULT_BUILD_FILENAME );
-        if ( buildXml.exists() && !buildXml.delete() )
-        {
-            throw new MojoExecutionException( "Cannot delete " + buildXml.getAbsolutePath() );
-        }
+        deleteCustomBuild();
 
         File mavenBuildXml = new File( project.getBasedir(), AntBuildWriter.DEFAULT_MAVEN_BUILD_FILENAME );
         if ( mavenBuildXml.exists() && !mavenBuildXml.delete() )
@@ -62,7 +88,8 @@
             throw new MojoExecutionException( "Cannot delete " + mavenBuildXml.getAbsolutePath() );
         }
 
-        File mavenBuildProperties = new File( project.getBasedir(), AntBuildWriter.DEFAULT_MAVEN_PROPERTIES_FILENAME );
+        File mavenBuildProperties =
+            new File( project.getBasedir(), AntBuildWriter.DEFAULT_MAVEN_PROPERTIES_FILENAME );
         if ( mavenBuildProperties.exists() && !mavenBuildProperties.delete() )
         {
             throw new MojoExecutionException( "Cannot delete " + mavenBuildProperties.getAbsolutePath() );
@@ -72,4 +99,95 @@
                        "Deleted Ant project for " + project.getArtifactId() + " in "
                            + project.getBasedir().getAbsolutePath() );
     }
+
+    /**
+     * Deleting the <code>build.xml</code> depending the user interaction.
+     *
+     * @throws MojoExecutionException if any
+     */
+    private void deleteCustomBuild()
+        throws MojoExecutionException
+    {
+        // add warranty msg
+        if ( !preCheck() )
+        {
+            return;
+        }
+
+        File buildXml = new File( project.getBasedir(), AntBuildWriter.DEFAULT_BUILD_FILENAME );
+        if ( buildXml.exists() && !buildXml.delete() )
+        {
+            throw new MojoExecutionException( "Cannot delete " + buildXml.getAbsolutePath() );
+        }
+    }
+
+    /**
+     * @return <code>true</code> if the user wants to proceed, <code>false</code> otherwise.
+     * @throws MojoExecutionException if any
+     */
+    private boolean preCheck()
+        throws MojoExecutionException
+    {
+        if ( force )
+        {
+            return true;
+        }
+
+        if ( !settings.isInteractiveMode() )
+        {
+            if ( getLog().isErrorEnabled() )
+            {
+                getLog().error(
+                                "Maven is not attempt to interact with the user for input. "
+                                    + "Verify the <interactiveMode/> configuration in your settings." );
+            }
+            return false;
+        }
+
+        if ( getLog().isWarnEnabled() )
+        {
+            getLog().warn( "" );
+            getLog().warn( "    WARRANTY DISCLAIMER" );
+            getLog().warn( "" );
+            getLog().warn( "This Maven goal will delete your build.xml." );
+            getLog().warn( "" );
+        }
+
+        while ( true )
+        {
+            if ( getLog().isInfoEnabled() )
+            {
+                getLog().info( "Are you sure to proceed? [Y]es [N]o" );
+            }
+
+            try
+            {
+                String userExpression = inputHandler.readLine();
+                if ( userExpression == null || userExpression.toLowerCase( Locale.ENGLISH ).equalsIgnoreCase( "Y" )
+                    || userExpression.toLowerCase( Locale.ENGLISH ).equalsIgnoreCase( "Yes" ) )
+                {
+                    if ( getLog().isInfoEnabled() )
+                    {
+                        getLog().info( "OK, let's proceed..." );
+                    }
+                    break;
+                }
+                if ( userExpression == null || userExpression.toLowerCase( Locale.ENGLISH ).equalsIgnoreCase( "N" )
+                    || userExpression.toLowerCase( Locale.ENGLISH ).equalsIgnoreCase( "No" ) )
+                {
+                    if ( getLog().isInfoEnabled() )
+                    {
+                        getLog().info( "No changes on the build.xml occur." );
+                    }
+                    return false;
+                }
+            }
+            catch ( IOException e )
+            {
+                throw new MojoExecutionException( "Unable to read from standard input.", e );
+            }
+        }
+
+        return true;
+    }
 }