maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hbout...@apache.org
Subject [maven-ant-plugin] 05/18: MANT-47: ant:clean should not delete build.xml
Date Wed, 20 Dec 2017 09:28:46 GMT
This is an automated email from the ASF dual-hosted git repository.

hboutemy pushed a commit to annotated tag maven-ant-plugin-2.2
in repository https://gitbox.apache.org/repos/asf/maven-ant-plugin.git

commit c51d57051d4a4330834bac17e4d9b7e0b7147022
Author: Vincent Siveton <vsiveton@apache.org>
AuthorDate: Mon Apr 13 10:52:00 2009 +0000

    MANT-47: ant:clean should not delete build.xml
    
    o Add a user input handler and a force flag
    
    git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-ant-plugin@764400
13f79535-47bb-0310-9956-ffa450edef68
---
 .../org/apache/maven/plugin/ant/AntCleanMojo.java  | 130 ++++++++++++++++++++-
 1 file changed, 124 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugin/ant/AntCleanMojo.java b/src/main/java/org/apache/maven/plugin/ant/AntCleanMojo.java
index f61ba0a..ab4a9ac 100644
--- a/src/main/java/org/apache/maven/plugin/ant/AntCleanMojo.java
+++ b/src/main/java/org/apache/maven/plugin/ant/AntCleanMojo.java
@@ -20,10 +20,14 @@ package org.apache.maven.plugin.ant;
  */
 
 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 @@ public class AntCleanMojo
     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 @@ public class AntCleanMojo
     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 @@ public class AntCleanMojo
             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 @@ public class AntCleanMojo
                        "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;
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@maven.apache.org" <commits@maven.apache.org>.

Mime
View raw message