Skip to content

Commit

Permalink
Switch to the new maven 4 api
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jul 25, 2022
1 parent 6560418 commit 260a4ba
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 193 deletions.
54 changes: 28 additions & 26 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ under the License.
<parent>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugins</artifactId>
<version>35</version>
<version>36</version>
<relativePath />
</parent>

<artifactId>maven-clean-plugin</artifactId>
<version>3.2.1-SNAPSHOT</version>
<version>4.0.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Apache Maven Clean Plugin</name>
Expand Down Expand Up @@ -65,44 +65,31 @@ under the License.
</distributionManagement>

<properties>
<mavenVersion>3.2.5</mavenVersion>
<mavenVersion>4.0.0-alpha-1-SNAPSHOT</mavenVersion>
<javaVersion>8</javaVersion>
<surefire.version>2.22.2</surefire.version>
<mavenPluginToolsVersion>3.6.4</mavenPluginToolsVersion>
<mavenPluginToolsVersion>4.0.0-SNAPSHOT</mavenPluginToolsVersion>
<mavenPluginTestingVersion>4.0.0-SNAPSHOT</mavenPluginTestingVersion>
<project.build.outputTimestamp>2022-04-01T21:20:42Z</project.build.outputTimestamp>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<artifactId>maven-api-core</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-shared-utils</artifactId>
<version>3.3.4</version>
</dependency>

<!-- dependencies to annotations -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<scope>provided</scope>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>${mavenVersion}</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>${mavenVersion}</version>
<version>${mavenPluginTestingVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -112,13 +99,28 @@ under the License.
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>4.0.0-SNAPSHOT</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>run-its</id>
Expand Down
36 changes: 19 additions & 17 deletions src/main/java/org/apache/maven/plugins/clean/CleanMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
* 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;
Expand All @@ -44,9 +45,9 @@
* @see org.apache.maven.plugins.clean.Fileset
* @since 2.0
*/
@Mojo( name = "clean", threadSafe = true )
@Mojo( name = "clean" )
public class CleanMojo
extends AbstractMojo
implements org.apache.maven.api.plugin.Mojo
{

public static final String FAST_MODE_BACKGROUND = "background";
Expand All @@ -55,6 +56,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.
*/
Expand Down Expand Up @@ -209,21 +212,20 @@ 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.
* @see org.apache.maven.plugin.Mojo#execute()
* @throws MojoException When a directory failed to get deleted.
* @see org.apache.maven.api.plugin.Mojo#execute()
*/
public void execute()
throws MojoExecutionException
{
if ( skip )
{
getLog().info( "Clean is skipped." );
LOGGER.info( "Clean is skipped." );
return;
}

Expand All @@ -243,7 +245,7 @@ else if ( fast && multiModuleProjectDirectory != null )
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." );
}
Expand All @@ -256,7 +258,7 @@ else if ( fast && multiModuleProjectDirectory != null )
+ 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, LOGGER, isVerbose(), fastDir, fastMode );

try
{
Expand All @@ -274,7 +276,7 @@ else if ( fast && multiModuleProjectDirectory != null )
{
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() );
Expand All @@ -285,7 +287,7 @@ else if ( fast && multiModuleProjectDirectory != null )
}
catch ( IOException e )
{
throw new MojoExecutionException( "Failed to clean project: " + e.getMessage(), e );
throw new MojoException( "Failed to clean project: " + e.getMessage(), e );
}
}

Expand All @@ -296,7 +298,7 @@ else if ( fast && multiModuleProjectDirectory != null )
*/
private boolean isVerbose()
{
return ( verbose != null ) ? verbose : getLog().isDebugEnabled();
return ( verbose != null ) ? verbose : LOGGER.isDebugEnabled();
}

/**
Expand Down
57 changes: 19 additions & 38 deletions src/main/java/org/apache/maven/plugins/clean/Cleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@

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 org.apache.maven.execution.ExecutionListener;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.shared.utils.Os;
import org.eclipse.aether.SessionData;
import org.apache.maven.api.Event;
import org.apache.maven.api.EventType;
import org.apache.maven.api.Listener;
import org.apache.maven.api.Session;
import org.apache.maven.api.SessionData;
import org.codehaus.plexus.util.Os;

import static org.apache.maven.plugins.clean.CleanMojo.FAST_MODE_BACKGROUND;
import static org.apache.maven.plugins.clean.CleanMojo.FAST_MODE_DEFER;
Expand All @@ -54,7 +52,7 @@ 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;

Expand All @@ -74,7 +72,7 @@ class Cleaner
* @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, final org.slf4j.Logger log, boolean verbose, File fastDir, String fastMode )
{
logDebug = ( log == null || !log.isDebugEnabled() ) ? null : log::debug;

Expand Down Expand Up @@ -153,7 +151,7 @@ private boolean fastDelete( File baseDirFile )
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;
}
Expand Down Expand Up @@ -379,7 +377,7 @@ public void update( Result result )
private interface Logger
{

void log( CharSequence message );
void log( String message );

}

Expand Down Expand Up @@ -473,7 +471,7 @@ synchronized File pollNext()
{
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 )
{
Expand Down Expand Up @@ -513,16 +511,12 @@ synchronized boolean doDelete( File dir )
*/
private void wrapExecutionListener()
{
ExecutionListener executionListener = cleaner.session.getRequest().getExecutionListener();
if ( executionListener == null
|| !Proxy.isProxyClass( executionListener.getClass() )
|| !( Proxy.getInvocationHandler( executionListener ) instanceof SpyInvocationHandler ) )
synchronized ( CleanerListener.class )
{
ExecutionListener listener = ( ExecutionListener ) Proxy.newProxyInstance(
ExecutionListener.class.getClassLoader(),
new Class[] { ExecutionListener.class },
new SpyInvocationHandler( executionListener ) );
cleaner.session.getRequest().setExecutionListener( listener );
if ( cleaner.session.getListeners().stream().noneMatch( l -> l instanceof CleanerListener ) )
{
cleaner.session.registerListener( new CleanerListener() );
}
}
}

Expand Down Expand Up @@ -554,29 +548,16 @@ synchronized void doSessionEnd()

}

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( Event event )
{
if ( "sessionEnded".equals( method.getName() ) )
if ( event.getType() == EventType.SessionEnded )
{
BackgroundCleaner.sessionEnd();
}
if ( delegate != null )
{
return method.invoke( delegate, args );
}
return null;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
* under the License.
*/

import org.apache.maven.shared.utils.io.DirectoryScanner;
import org.apache.maven.shared.utils.io.SelectorUtils;

import java.io.File;
import java.util.Arrays;

import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.SelectorUtils;


/**
* Selects paths based on Ant-like glob patterns.
Expand Down
Loading

0 comments on commit 260a4ba

Please sign in to comment.