This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch mvn4
in repository https://gitbox.apache.org/repos/asf/maven-clean-plugin.git


The following commit(s) were added to refs/heads/mvn4 by this push:
     new 997706d  Switch a few core plugins to the new api
997706d is described below

commit 997706d828eef37238c3c435084b739ffb6a168b
Author: Guillaume Nodet <[email protected]>
AuthorDate: Thu Feb 24 17:00:56 2022 +0100

    Switch a few core plugins to the new api
---
 pom.xml                                            |  11 +-
 .../org/apache/maven/plugins/clean/CleanMojo.java  |  34 +++---
 .../org/apache/maven/plugins/clean/Cleaner.java    | 131 +++++++--------------
 .../apache/maven/plugins/clean/CleanMojoTest.java  |   8 +-
 4 files changed, 68 insertions(+), 116 deletions(-)

diff --git a/pom.xml b/pom.xml
index 743efd0..0b596ba 100644
--- a/pom.xml
+++ b/pom.xml
@@ -68,14 +68,14 @@ under the License.
     <mavenVersion>4.0.0-alpha-1-SNAPSHOT</mavenVersion>
     <javaVersion>8</javaVersion>
     <surefire.version>2.22.2</surefire.version>
-    <mavenPluginToolsVersion>3.6.2</mavenPluginToolsVersion>
+    <mavenPluginToolsVersion>3.6.5-SNAPSHOT</mavenPluginToolsVersion>
     
<project.build.outputTimestamp>2020-04-07T21:04:00Z</project.build.outputTimestamp>
   </properties>
 
   <dependencies>
     <dependency>
       <groupId>org.apache.maven</groupId>
-      <artifactId>maven-plugin-api</artifactId>
+      <artifactId>maven-core-api</artifactId>
       <version>${mavenVersion}</version>
       <scope>provided</scope>
     </dependency>
@@ -85,13 +85,6 @@ under the License.
       <version>4.0.0-SNAPSHOT</version>
     </dependency>
 
-    <!-- dependencies to annotations -->
-    <dependency>
-      <groupId>org.apache.maven.plugin-tools</groupId>
-      <artifactId>maven-plugin-annotations</artifactId>
-      <scope>provided</scope>
-    </dependency>
-
     <!-- Test -->
     <dependency>
       <groupId>org.apache.maven.plugin-testing</groupId>
diff --git a/src/main/java/org/apache/maven/plugins/clean/CleanMojo.java 
b/src/main/java/org/apache/maven/plugins/clean/CleanMojo.java
index 4b5ba86..114a5f0 100644
--- a/src/main/java/org/apache/maven/plugins/clean/CleanMojo.java
+++ b/src/main/java/org/apache/maven/plugins/clean/CleanMojo.java
@@ -19,11 +19,12 @@ package org.apache.maven.plugins.clean;
  * under the License.
  */
 
-import org.apache.maven.execution.MavenSession;
-import org.apache.maven.plugin.AbstractMojo;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugins.annotations.Mojo;
-import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.api.Session;
+import org.apache.maven.api.plugin.MojoException;
+import org.apache.maven.api.plugin.annotations.Mojo;
+import org.apache.maven.api.plugin.annotations.Parameter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.File;
 import java.io.IOException;
@@ -44,9 +45,8 @@ import java.io.IOException;
  * @see org.apache.maven.plugins.clean.Fileset
  * @since 2.0
  */
-@Mojo( name = "clean", threadSafe = true )
+@Mojo( name = "clean" )
 public class CleanMojo
-    extends AbstractMojo
 {
 
     public static final String FAST_MODE_BACKGROUND = "background";
@@ -55,6 +55,8 @@ public class CleanMojo
 
     public static final String FAST_MODE_DEFER = "defer";
 
+    private static final Logger LOGGER = LoggerFactory.getLogger( 
CleanMojo.class );
+
     /**
      * This is where build results go.
      */
@@ -209,21 +211,21 @@ public class CleanMojo
     private String fastMode;
 
     @Parameter( defaultValue = "${session}", readonly = true )
-    private MavenSession session;
+    private Session session;
 
     /**
      * Deletes file-sets in the following project build directory order: 
(source) directory, output directory, test
      * directory, report directory, and then the additional file-sets.
      *
-     * @throws MojoExecutionException When a directory failed to get deleted.
+     * @throws MojoException When a directory failed to get deleted.
      * @see org.apache.maven.plugin.Mojo#execute()
      */
     public void execute()
-        throws MojoExecutionException
+        throws MojoException
     {
         if ( skip )
         {
-            getLog().info( "Clean is skipped." );
+            LOGGER.info( "Clean is skipped." );
             return;
         }
 
@@ -243,7 +245,7 @@ public class CleanMojo
             fastDir = null;
             if ( fast )
             {
-                getLog().warn( "Fast clean requires maven 3.3.1 or newer, "
+                LOGGER.warn( "Fast clean requires maven 3.3.1 or newer, "
                         + "or an explicit directory to be specified with the 
'fastDir' configuration of "
                         + "this plugin, or the 'maven.clean.fastDir' user 
property to be set." );
             }
@@ -256,7 +258,7 @@ public class CleanMojo
                     + FAST_MODE_BACKGROUND + "', '" + FAST_MODE_AT_END + "' 
and '" + FAST_MODE_DEFER + "'." );
         }
 
-        Cleaner cleaner = new Cleaner( session, getLog(), isVerbose(), 
fastDir, fastMode );
+        Cleaner cleaner = new Cleaner( session, isVerbose(), fastDir, fastMode 
);
 
         try
         {
@@ -274,7 +276,7 @@ public class CleanMojo
                 {
                     if ( fileset.getDirectory() == null )
                     {
-                        throw new MojoExecutionException( "Missing base 
directory for " + fileset );
+                        throw new MojoException( "Missing base directory for " 
+ fileset );
                     }
                     GlobSelector selector = new GlobSelector( 
fileset.getIncludes(), fileset.getExcludes(),
                                                               
fileset.isUseDefaultExcludes() );
@@ -285,7 +287,7 @@ public class CleanMojo
         }
         catch ( IOException e )
         {
-            throw new MojoExecutionException( "Failed to clean project: " + 
e.getMessage(), e );
+            throw new MojoException( "Failed to clean project: " + 
e.getMessage(), e );
         }
     }
 
@@ -296,7 +298,7 @@ public class CleanMojo
      */
     private boolean isVerbose()
     {
-        return ( verbose != null ) ? verbose : getLog().isDebugEnabled();
+        return ( verbose != null ) ? verbose : LOGGER.isDebugEnabled();
     }
 
     /**
diff --git a/src/main/java/org/apache/maven/plugins/clean/Cleaner.java 
b/src/main/java/org/apache/maven/plugins/clean/Cleaner.java
index 4e6e9d2..cc77de7 100644
--- a/src/main/java/org/apache/maven/plugins/clean/Cleaner.java
+++ b/src/main/java/org/apache/maven/plugins/clean/Cleaner.java
@@ -19,22 +19,24 @@ package org.apache.maven.plugins.clean;
  * under the License.
  */
 
+import javax.annotation.Nonnull;
+
 import java.io.File;
 import java.io.IOException;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.StandardCopyOption;
 import java.util.ArrayDeque;
 import java.util.Deque;
+import java.util.function.Consumer;
 
-import org.apache.maven.execution.ExecutionListener;
-import org.apache.maven.execution.MavenSession;
-import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.api.Event;
+import org.apache.maven.api.Listener;
+import org.apache.maven.api.Session;
+import org.apache.maven.api.SessionData;
 import org.apache.maven.shared.utils.Os;
-import org.eclipse.aether.SessionData;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import static org.apache.maven.plugins.clean.CleanMojo.FAST_MODE_BACKGROUND;
 import static org.apache.maven.plugins.clean.CleanMojo.FAST_MODE_DEFER;
@@ -47,6 +49,8 @@ import static 
org.apache.maven.plugins.clean.CleanMojo.FAST_MODE_DEFER;
 class Cleaner
 {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger( 
Cleaner.class );
+
     private static final boolean ON_WINDOWS = Os.isFamily( Os.FAMILY_WINDOWS );
 
     private static final String LAST_DIRECTORY_TO_DELETE = 
Cleaner.class.getName() + ".lastDirectoryToDelete";
@@ -54,15 +58,15 @@ class Cleaner
     /**
      * The maven session.  This is typically non-null in a real run, but it 
can be during unit tests.
      */
-    private final MavenSession session;
+    private final Session session;
 
-    private final Logger logDebug;
+    private final Consumer<String> logDebug;
 
-    private final Logger logInfo;
+    private final Consumer<String> logInfo;
 
-    private final Logger logVerbose;
+    private final Consumer<String> logVerbose;
 
-    private final Logger logWarn;
+    private final Consumer<String> logWarn;
 
     private final File fastDir;
 
@@ -70,17 +74,16 @@ class Cleaner
 
     /**
      * Creates a new cleaner.
-     * @param log The logger to use, may be <code>null</code> to disable 
logging.
      * @param verbose Whether to perform verbose logging.
      * @param fastMode The fast deletion mode
      */
-    Cleaner( MavenSession session, final Log log, boolean verbose, File 
fastDir, String fastMode )
+    Cleaner( Session session, boolean verbose, File fastDir, String fastMode )
     {
-        logDebug = ( log == null || !log.isDebugEnabled() ) ? null : 
log::debug;
+        logDebug = LOGGER::debug;
 
-        logInfo = ( log == null || !log.isInfoEnabled() ) ? null : log::info;
+        logInfo = LOGGER::info;
 
-        logWarn = ( log == null || !log.isWarnEnabled() ) ? null : log::warn;
+        logWarn = LOGGER::warn;
 
         logVerbose = verbose ? logInfo : logDebug;
 
@@ -109,19 +112,13 @@ class Cleaner
         {
             if ( !basedir.exists() )
             {
-                if ( logDebug != null )
-                {
-                    logDebug.log( "Skipping non-existing directory " + basedir 
);
-                }
+                logDebug.accept( "Skipping non-existing directory " + basedir 
);
                 return;
             }
             throw new IOException( "Invalid base directory " + basedir );
         }
 
-        if ( logInfo != null )
-        {
-            logInfo.log( "Deleting " + basedir + ( selector != null ? " (" + 
selector + ")" : "" ) );
-        }
+        logInfo.accept( "Deleting " + basedir + ( selector != null ? " (" + 
selector + ")" : "" ) );
 
         File file = followSymlinks ? basedir : basedir.getCanonicalFile();
 
@@ -153,7 +150,7 @@ class Cleaner
                     Files.move( baseDir, tmpDir, 
StandardCopyOption.REPLACE_EXISTING );
                     if ( session != null )
                     {
-                        session.getRepositorySession().getData().set( 
LAST_DIRECTORY_TO_DELETE, baseDir.toFile() );
+                        session.getData().set( LAST_DIRECTORY_TO_DELETE, 
baseDir.toFile() );
                     }
                     baseDir = tmpDir;
                 }
@@ -165,11 +162,7 @@ class Cleaner
             }
             catch ( IOException e )
             {
-                if ( logDebug != null )
-                {
-                    // TODO: this Logger interface cannot log exceptions and 
needs refactoring
-                    logDebug.log( "Unable to fast delete directory: " + e );
-                }
+                logDebug.accept( "Unable to fast delete directory: " + e );
                 return false;
             }
         }
@@ -183,12 +176,9 @@ class Cleaner
         }
         catch ( IOException e )
         {
-            if ( logDebug != null )
-            {
-                // TODO: this Logger interface cannot log exceptions and needs 
refactoring
-                logDebug.log( "Unable to fast delete directory as the path "
-                        + fastDir + " does not point to a directory or cannot 
be created: " + e );
-            }
+            // TODO: this Logger interface cannot log exceptions and needs 
refactoring
+            logDebug.accept( "Unable to fast delete directory as the path "
+                    + fastDir + " does not point to a directory or cannot be 
created: " + e );
             return false;
         }
 
@@ -206,11 +196,8 @@ class Cleaner
         }
         catch ( IOException e )
         {
-            if ( logDebug != null )
-            {
-                // TODO: this Logger interface cannot log exceptions and needs 
refactoring
-                logDebug.log( "Unable to fast delete directory: " + e );
-            }
+            // TODO: this Logger interface cannot log exceptions and needs 
refactoring
+            logDebug.accept( "Unable to fast delete directory: " + e );
             return false;
         }
     }
@@ -259,14 +246,14 @@ class Cleaner
                         }
                     }
                 }
-                else if ( logDebug != null )
+                else
                 {
-                    logDebug.log( "Not recursing into symlink " + file );
+                    logDebug.accept( "Not recursing into symlink " + file );
                 }
             }
-            else if ( logDebug != null )
+            else
             {
-                logDebug.log( "Not recursing into directory without included 
files " + file );
+                logDebug.accept( "Not recursing into directory without 
included files " + file );
             }
         }
 
@@ -276,15 +263,15 @@ class Cleaner
             {
                 if ( isDirectory )
                 {
-                    logVerbose.log( "Deleting directory " + file );
+                    logVerbose.accept( "Deleting directory " + file );
                 }
                 else if ( file.exists() )
                 {
-                    logVerbose.log( "Deleting file " + file );
+                    logVerbose.accept( "Deleting file " + file );
                 }
                 else
                 {
-                    logVerbose.log( "Deleting dangling symlink " + file );
+                    logVerbose.accept( "Deleting dangling symlink " + file );
                 }
             }
             result.failures += delete( file, failOnError, retryOnError );
@@ -349,10 +336,7 @@ class Cleaner
                 }
                 else
                 {
-                    if ( logWarn != null )
-                    {
-                        logWarn.log( "Failed to delete " + file );
-                    }
+                    logWarn.accept( "Failed to delete " + file );
                     return 1;
                 }
             }
@@ -376,13 +360,6 @@ class Cleaner
 
     }
 
-    private interface Logger
-    {
-
-        void log( CharSequence message );
-
-    }
-
     private static class BackgroundCleaner extends Thread
     {
 
@@ -473,7 +450,7 @@ class Cleaner
             {
                 if ( cleaner.session != null )
                 {
-                    SessionData data = 
cleaner.session.getRepositorySession().getData();
+                    SessionData data = cleaner.session.getData();
                     File lastDir = ( File ) data.get( LAST_DIRECTORY_TO_DELETE 
);
                     if ( lastDir != null )
                     {
@@ -513,16 +490,9 @@ class Cleaner
          */
         private void wrapExecutionListener()
         {
-            ExecutionListener executionListener = 
cleaner.session.getRequest().getExecutionListener();
-            if ( executionListener == null
-                    || !Proxy.isProxyClass( executionListener.getClass() )
-                    || !( Proxy.getInvocationHandler( executionListener ) 
instanceof SpyInvocationHandler ) )
+            if ( cleaner.session.getListeners().stream().noneMatch( l -> l 
instanceof CleanerListener ) )
             {
-                ExecutionListener listener = ( ExecutionListener ) 
Proxy.newProxyInstance(
-                        ExecutionListener.class.getClassLoader(),
-                        new Class[] { ExecutionListener.class },
-                        new SpyInvocationHandler( executionListener ) );
-                cleaner.session.getRequest().setExecutionListener( listener );
+                cleaner.session.registerListener( new CleanerListener() );
             }
         }
 
@@ -538,7 +508,7 @@ class Cleaner
                 {
                     try
                     {
-                        cleaner.logInfo.log( "Waiting for background file 
deletion" );
+                        cleaner.logInfo.accept( "Waiting for background file 
deletion" );
                         while ( status != STOPPED )
                         {
                             wait();
@@ -554,29 +524,16 @@ class Cleaner
 
     }
 
-    static class SpyInvocationHandler implements InvocationHandler
+    static class CleanerListener implements Listener
     {
-        private final ExecutionListener delegate;
-
-        SpyInvocationHandler( ExecutionListener delegate )
-        {
-            this.delegate = delegate;
-        }
-
         @Override
-        public Object invoke( Object proxy, Method method, Object[] args ) 
throws Throwable
+        public void onEvent( @Nonnull Event event )
         {
-            if ( "sessionEnded".equals( method.getName() ) )
+            if ( event.getType() == Event.Type.SessionEnded )
             {
                 BackgroundCleaner.sessionEnd();
             }
-            if ( delegate != null )
-            {
-                return method.invoke( delegate, args );
-            }
-            return null;
         }
-
     }
 
 }
diff --git a/src/test/java/org/apache/maven/plugins/clean/CleanMojoTest.java 
b/src/test/java/org/apache/maven/plugins/clean/CleanMojoTest.java
index 84b638f..30d7f3a 100644
--- a/src/test/java/org/apache/maven/plugins/clean/CleanMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/clean/CleanMojoTest.java
@@ -19,7 +19,7 @@ package org.apache.maven.plugins.clean;
  * under the License.
  */
 
-import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.api.plugin.MojoException;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 
 import java.io.File;
@@ -176,7 +176,7 @@ public class CleanMojoTest
 
             fail( "Should fail to delete a file treated as a directory" );
         }
-        catch ( MojoExecutionException expected )
+        catch ( MojoException expected )
         {
             assertTrue( true );
         }
@@ -237,7 +237,7 @@ public class CleanMojoTest
             mojo.execute();
             fail( "Should fail to delete a file that is locked" );
         }
-        catch ( MojoExecutionException expected )
+        catch ( MojoException expected )
         {
             assertTrue( true );
         }
@@ -277,7 +277,7 @@ public class CleanMojoTest
             mojo.execute();
             assertTrue( true );
         }
-        catch ( MojoExecutionException expected )
+        catch ( MojoException expected )
         {
             fail( "Should display a warning when deleting a file that is 
locked" );
         }

Reply via email to