diff --git a/pom.xml b/pom.xml
index 53e29db..ff4cf70 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@ under the License.
maven-clean-plugin
- 3.3.3-SNAPSHOT
+ 4.0.0-SNAPSHOT
maven-plugin
Apache Maven Clean Plugin
@@ -61,71 +61,48 @@ under the License.
- 3.5.4
+ 4.0.0-alpha-9-SNAPSHOT
2023-06-14T18:50:52Z
+ 8
org.apache.maven
- maven-plugin-api
+ maven-api-core
${mavenVersion}
provided
- org.apache.maven
- maven-core
- ${mavenVersion}
- provided
-
-
- org.apache.maven.resolver
- maven-resolver-api
- 1.1.1
+ jakarta.inject
+ jakarta.inject-api
+ 2.0.1
provided
+
org.codehaus.plexus
plexus-utils
4.0.0
-
+
- org.apache.maven.plugin-tools
- maven-plugin-annotations
- provided
+ org.codehaus.plexus
+ plexus-xml
+ 4.0.2
+ test
-
-
org.apache.maven.plugin-testing
maven-plugin-testing-harness
- 4.0.0-alpha-2
+ 4.0.0-alpha-3-SNAPSHOT
test
org.junit.jupiter
junit-jupiter-api
- 5.9.3
- test
-
-
- org.codehaus.plexus
- plexus-testing
- 1.1.0
- test
-
-
- org.eclipse.sisu
- org.eclipse.sisu.plexus
- test
-
-
- com.google.inject
- guice
- 4.2.0
- no_aop
+ 5.10.1
test
@@ -136,12 +113,27 @@ under the License.
org.apache.maven
- maven-resolver-provider
+ maven-core
${mavenVersion}
test
+
+
+
+ org.apache.maven.plugins
+ maven-plugin-plugin
+ 3.10.3-SNAPSHOT
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 3.2.2
+
+
+
+
run-its
diff --git a/src/it/settings.xml b/src/it/settings.xml
index c8f77f0..5617e4e 100644
--- a/src/it/settings.xml
+++ b/src/it/settings.xml
@@ -32,9 +32,11 @@ under the License.
@localRepositoryUrl@
true
+ ignore
true
+ ignore
@@ -44,9 +46,11 @@ under the License.
@localRepositoryUrl@
true
+ ignore
true
+ ignore
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 afc0df4..8ce0068 100644
--- a/src/main/java/org/apache/maven/plugins/clean/CleanMojo.java
+++ b/src/main/java/org/apache/maven/plugins/clean/CleanMojo.java
@@ -18,14 +18,16 @@
*/
package org.apache.maven.plugins.clean;
-import java.io.File;
import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
-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 jakarta.inject.Inject;
+import org.apache.maven.api.Session;
+import org.apache.maven.api.plugin.Log;
+import org.apache.maven.api.plugin.MojoException;
+import org.apache.maven.api.plugin.annotations.Mojo;
+import org.apache.maven.api.plugin.annotations.Parameter;
/**
* Goal which cleans the build.
@@ -43,8 +45,8 @@
* @see org.apache.maven.plugins.clean.Fileset
* @since 2.0
*/
-@Mojo(name = "clean", threadSafe = true)
-public class CleanMojo extends AbstractMojo {
+@Mojo(name = "clean")
+public class CleanMojo implements org.apache.maven.api.plugin.Mojo {
public static final String FAST_MODE_BACKGROUND = "background";
@@ -52,23 +54,26 @@ public class CleanMojo extends AbstractMojo {
public static final String FAST_MODE_DEFER = "defer";
+ @Inject
+ private Log logger;
+
/**
* This is where build results go.
*/
@Parameter(defaultValue = "${project.build.directory}", readonly = true, required = true)
- private File directory;
+ private Path directory;
/**
* This is where compiled classes go.
*/
@Parameter(defaultValue = "${project.build.outputDirectory}", readonly = true, required = true)
- private File outputDirectory;
+ private Path outputDirectory;
/**
* This is where compiled test classes go.
*/
@Parameter(defaultValue = "${project.build.testOutputDirectory}", readonly = true, required = true)
- private File testOutputDirectory;
+ private Path testOutputDirectory;
/**
* This is where the site plugin generates its pages.
@@ -76,7 +81,7 @@ public class CleanMojo extends AbstractMojo {
* @since 2.1.1
*/
@Parameter(defaultValue = "${project.build.outputDirectory}", readonly = true, required = true)
- private File reportDirectory;
+ private Path reportDirectory;
/**
* Sets whether the plugin runs in verbose mode. As of plugin version 2.3, the default value is derived from Maven's
@@ -190,7 +195,7 @@ public class CleanMojo extends AbstractMojo {
* @see #fast
*/
@Parameter(property = "maven.clean.fastDir")
- private File fastDir;
+ private Path fastDir;
/**
* Mode to use when using fast clean. Values are: background
to start deletion immediately and
@@ -206,32 +211,32 @@ public class CleanMojo extends AbstractMojo {
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 {
+ public void execute() {
if (skip) {
- getLog().info("Clean is skipped.");
+ logger.info("Clean is skipped.");
return;
}
String multiModuleProjectDirectory =
- session != null ? session.getSystemProperties().getProperty("maven.multiModuleProjectDirectory") : null;
- File fastDir;
+ session != null ? session.getSystemProperties().get("maven.multiModuleProjectDirectory") : null;
+ Path fastDir;
if (fast && this.fastDir != null) {
fastDir = this.fastDir;
} else if (fast && multiModuleProjectDirectory != null) {
- fastDir = new File(multiModuleProjectDirectory, "target/.clean");
+ fastDir = Paths.get(multiModuleProjectDirectory, "target/.clean");
} else {
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.");
}
@@ -244,10 +249,10 @@ public void execute() throws MojoExecutionException {
+ 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 {
- for (File directoryItem : getDirectories()) {
+ for (Path directoryItem : getDirectories()) {
if (directoryItem != null) {
cleaner.delete(directoryItem, null, followSymLinks, failOnError, retryOnError);
}
@@ -256,7 +261,7 @@ public void execute() throws MojoExecutionException {
if (filesets != null) {
for (Fileset fileset : filesets) {
if (fileset.getDirectory() == null) {
- throw new MojoExecutionException("Missing base directory for " + fileset);
+ throw new MojoException("Missing base directory for " + fileset);
}
final String[] includes = fileset.getIncludes();
final String[] excludes = fileset.getExcludes();
@@ -273,8 +278,9 @@ public void execute() throws MojoExecutionException {
fileset.getDirectory(), selector, fileset.isFollowSymlinks(), failOnError, retryOnError);
}
}
+
} catch (IOException e) {
- throw new MojoExecutionException("Failed to clean project: " + e.getMessage(), e);
+ throw new MojoException("Failed to clean project: " + e.getMessage(), e);
}
}
@@ -284,7 +290,7 @@ public void execute() throws MojoExecutionException {
* @return true
if verbose output is enabled, false
otherwise.
*/
private boolean isVerbose() {
- return (verbose != null) ? verbose : getLog().isDebugEnabled();
+ return (verbose != null) ? verbose : logger.isDebugEnabled();
}
/**
@@ -292,12 +298,12 @@ private boolean isVerbose() {
*
* @return The directories to clean or an empty array if none, never null
.
*/
- private File[] getDirectories() {
- File[] directories;
+ private Path[] getDirectories() {
+ Path[] directories;
if (excludeDefaultDirectories) {
- directories = new File[0];
+ directories = new Path[0];
} else {
- directories = new File[] {directory, outputDirectory, testOutputDirectory, reportDirectory};
+ directories = new Path[] {directory, outputDirectory, testOutputDirectory, reportDirectory};
}
return directories;
}
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 97ef594..4ade156 100644
--- a/src/main/java/org/apache/maven/plugins/clean/Cleaner.java
+++ b/src/main/java/org/apache/maven/plugins/clean/Cleaner.java
@@ -20,9 +20,6 @@
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.LinkOption;
import java.nio.file.Path;
@@ -30,12 +27,16 @@
import java.nio.file.attribute.BasicFileAttributes;
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 java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+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.apache.maven.api.plugin.Log;
import org.codehaus.plexus.util.Os;
-import org.eclipse.aether.SessionData;
import static org.apache.maven.plugins.clean.CleanMojo.FAST_MODE_BACKGROUND;
import static org.apache.maven.plugins.clean.CleanMojo.FAST_MODE_DEFER;
@@ -54,7 +55,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;
@@ -64,7 +65,7 @@ class Cleaner {
private final Logger logWarn;
- private final File fastDir;
+ private final Path fastDir;
private final String fastMode;
@@ -74,7 +75,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 Log log, boolean verbose, Path fastDir, String fastMode) {
logDebug = (log == null || !log.isDebugEnabled()) ? null : log::debug;
logInfo = (log == null || !log.isInfoEnabled()) ? null : log::info;
@@ -101,10 +102,10 @@ class Cleaner {
* @throws IOException If a file/directory could not be deleted and failOnError
is true
.
*/
public void delete(
- File basedir, Selector selector, boolean followSymlinks, boolean failOnError, boolean retryOnError)
+ Path basedir, Selector selector, boolean followSymlinks, boolean failOnError, boolean retryOnError)
throws IOException {
- if (!basedir.isDirectory()) {
- if (!basedir.exists()) {
+ if (!Files.isDirectory(basedir)) {
+ if (!Files.exists(basedir)) {
if (logDebug != null) {
logDebug.log("Skipping non-existing directory " + basedir);
}
@@ -117,7 +118,7 @@ public void delete(
logInfo.log("Deleting " + basedir + (selector != null ? " (" + selector + ")" : ""));
}
- File file = followSymlinks ? basedir : basedir.getCanonicalFile();
+ Path file = followSymlinks ? basedir : basedir.toAbsolutePath().normalize();
if (selector == null && !followSymlinks && fastDir != null && session != null) {
// If anything wrong happens, we'll just use the usual deletion mechanism
@@ -129,9 +130,8 @@ public void delete(
delete(file, "", selector, followSymlinks, failOnError, retryOnError);
}
- private boolean fastDelete(File baseDirFile) {
- Path baseDir = baseDirFile.toPath();
- Path fastDir = this.fastDir.toPath();
+ private boolean fastDelete(Path baseDir) {
+ Path fastDir = this.fastDir;
// Handle the case where we use ${maven.multiModuleProjectDirectory}/target/.clean for example
if (fastDir.toAbsolutePath().startsWith(baseDir.toAbsolutePath())) {
try {
@@ -140,7 +140,7 @@ private boolean fastDelete(File baseDirFile) {
try {
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;
} catch (IOException e) {
@@ -177,7 +177,7 @@ private boolean fastDelete(File baseDirFile) {
// or any other exception occurs, an exception will be thrown in which case
// the method will return false and the usual deletion will be performed.
Files.move(baseDir, dstDir, StandardCopyOption.ATOMIC_MOVE);
- BackgroundCleaner.delete(this, tmpDir.toFile(), fastMode);
+ BackgroundCleaner.delete(this, tmpDir, fastMode);
return true;
} catch (IOException e) {
if (logDebug != null) {
@@ -204,7 +204,7 @@ private boolean fastDelete(File baseDirFile) {
* @throws IOException If a file/directory could not be deleted and failOnError
is true
.
*/
private Result delete(
- File file,
+ Path file,
String pathname,
Selector selector,
boolean followSymlinks,
@@ -213,20 +213,23 @@ private Result delete(
throws IOException {
Result result = new Result();
- boolean isDirectory = file.isDirectory();
+ boolean isDirectory = Files.isDirectory(file);
if (isDirectory) {
if (selector == null || selector.couldHoldSelected(pathname)) {
- if (followSymlinks || !isSymbolicLink(file.toPath())) {
- File canonical = followSymlinks ? file : file.getCanonicalFile();
- String[] filenames = canonical.list();
- if (filenames != null) {
- String prefix = pathname.length() > 0 ? pathname + File.separatorChar : "";
- for (int i = filenames.length - 1; i >= 0; i--) {
- String filename = filenames[i];
- File child = new File(canonical, filename);
+ final boolean isSymlink = Files.isSymbolicLink(file);
+ Path canonical = followSymlinks ? file : file.toAbsolutePath().normalize();
+ if (followSymlinks || !isSymlink) {
+ String prefix = pathname.length() > 0 ? pathname + File.separatorChar : "";
+ try (Stream children = Files.list(canonical)) {
+ for (Path child : children.collect(Collectors.toList())) {
result.update(delete(
- child, prefix + filename, selector, followSymlinks, failOnError, retryOnError));
+ child,
+ prefix + child.getFileName(),
+ selector,
+ followSymlinks,
+ failOnError,
+ retryOnError));
}
}
} else if (logDebug != null) {
@@ -241,7 +244,7 @@ private Result delete(
if (logVerbose != null) {
if (isDirectory) {
logVerbose.log("Deleting directory " + file);
- } else if (file.exists()) {
+ } else if (Files.exists(file)) {
logVerbose.log("Deleting file " + file);
} else {
logVerbose.log("Deleting dangling symlink " + file);
@@ -262,19 +265,14 @@ private boolean isSymbolicLink(Path path) throws IOException {
|| (attrs.isDirectory() && attrs.isOther());
}
- /**
- * Deletes the specified file, directory. If the path denotes a symlink, only the link is removed, its target is
- * left untouched.
- *
- * @param file The file/directory to delete, must not be null
.
- * @param failOnError Whether to abort with an exception in case the file/directory could not be deleted.
- * @param retryOnError Whether to undertake additional delete attempts in case the first attempt failed.
- * @return 0
if the file was deleted, 1
otherwise.
- * @throws IOException If a file/directory could not be deleted and failOnError
is true
.
- */
- private int delete(File file, boolean failOnError, boolean retryOnError) throws IOException {
- if (!file.delete()) {
+ private int delete(Path file, boolean failOnError, boolean retryOnError) throws IOException {
+ try {
+ Files.deleteIfExists(file);
+ return 0;
+ } catch (IOException e) {
boolean deleted = false;
+ IOException exception = new IOException("Failed to delete " + file);
+ exception.addSuppressed(e);
if (retryOnError) {
if (ON_WINDOWS) {
@@ -286,13 +284,18 @@ private int delete(File file, boolean failOnError, boolean retryOnError) throws
for (int i = 0; !deleted && i < delays.length; i++) {
try {
Thread.sleep(delays[i]);
- } catch (InterruptedException e) {
- // ignore
+ } catch (InterruptedException e2) {
+ exception.addSuppressed(e2);
+ }
+ try {
+ Files.deleteIfExists(file);
+ return 0;
+ } catch (IOException e2) {
+ exception.addSuppressed(e2);
}
- deleted = file.delete() || !file.exists();
}
} else {
- deleted = !file.exists();
+ deleted = !Files.exists(file);
}
if (!deleted) {
@@ -331,7 +334,7 @@ private static class BackgroundCleaner extends Thread {
private static BackgroundCleaner instance;
- private final Deque filesToDelete = new ArrayDeque<>();
+ private final Deque filesToDelete = new ArrayDeque<>();
private final Cleaner cleaner;
@@ -343,7 +346,7 @@ private static class BackgroundCleaner extends Thread {
private int status = NEW;
- public static void delete(Cleaner cleaner, File dir, String fastMode) {
+ public static void delete(Cleaner cleaner, Path dir, String fastMode) {
synchronized (BackgroundCleaner.class) {
if (instance == null || !instance.doDelete(dir)) {
instance = new BackgroundCleaner(cleaner, dir, fastMode);
@@ -359,7 +362,7 @@ static void sessionEnd() {
}
}
- private BackgroundCleaner(Cleaner cleaner, File dir, String fastMode) {
+ private BackgroundCleaner(Cleaner cleaner, Path dir, String fastMode) {
super("mvn-background-cleaner");
this.cleaner = cleaner;
this.fastMode = fastMode;
@@ -368,7 +371,7 @@ private BackgroundCleaner(Cleaner cleaner, File dir, String fastMode) {
public void run() {
while (true) {
- File basedir = pollNext();
+ Path basedir = pollNext();
if (basedir == null) {
break;
}
@@ -380,24 +383,25 @@ public void run() {
}
}
- synchronized void init(File fastDir, File dir) {
- if (fastDir.isDirectory()) {
- File[] children = fastDir.listFiles();
- if (children != null && children.length > 0) {
- for (File child : children) {
- doDelete(child);
+ synchronized void init(Path fastDir, Path dir) {
+ if (Files.isDirectory(fastDir)) {
+ try {
+ try (Stream children = Files.list(fastDir)) {
+ children.forEach(this::doDelete);
}
+ } catch (IOException e) {
+ throw new RuntimeException(e);
}
}
doDelete(dir);
}
- synchronized File pollNext() {
- File basedir = filesToDelete.poll();
+ synchronized Path pollNext() {
+ Path basedir = filesToDelete.poll();
if (basedir == null) {
if (cleaner.session != null) {
- SessionData data = cleaner.session.getRepositorySession().getData();
- File lastDir = (File) data.get(LAST_DIRECTORY_TO_DELETE);
+ SessionData data = cleaner.session.getData();
+ Path lastDir = (Path) data.get(LAST_DIRECTORY_TO_DELETE);
if (lastDir != null) {
data.set(LAST_DIRECTORY_TO_DELETE, null);
return lastDir;
@@ -409,7 +413,7 @@ synchronized File pollNext() {
return basedir;
}
- synchronized boolean doDelete(File dir) {
+ synchronized boolean doDelete(Path dir) {
if (status == STOPPED) {
return false;
}
@@ -431,15 +435,10 @@ synchronized boolean doDelete(File dir) {
* to outlive its main execution.
*/
private void wrapExecutionListener() {
- ExecutionListener executionListener = cleaner.session.getRequest().getExecutionListener();
- if (executionListener == null
- || !Proxy.isProxyClass(executionListener.getClass())
- || !(Proxy.getInvocationHandler(executionListener) instanceof SpyInvocationHandler)) {
- ExecutionListener listener = (ExecutionListener) Proxy.newProxyInstance(
- ExecutionListener.class.getClassLoader(),
- new Class[] {ExecutionListener.class},
- new SpyInvocationHandler(executionListener));
- cleaner.session.getRequest().setExecutionListener(listener);
+ synchronized (CleanerListener.class) {
+ if (cleaner.session.getListeners().stream().noneMatch(l -> l instanceof CleanerListener)) {
+ cleaner.session.registerListener(new CleanerListener());
+ }
}
}
@@ -464,22 +463,12 @@ synchronized void doSessionEnd() {
}
}
- static class SpyInvocationHandler implements InvocationHandler {
- private final ExecutionListener delegate;
-
- SpyInvocationHandler(ExecutionListener delegate) {
- this.delegate = delegate;
- }
-
+ static class CleanerListener implements Listener {
@Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- if ("sessionEnded".equals(method.getName())) {
+ public void onEvent(Event event) {
+ if (event.getType() == EventType.SESSION_ENDED) {
BackgroundCleaner.sessionEnd();
}
- if (delegate != null) {
- return method.invoke(delegate, args);
- }
- return null;
}
}
}
diff --git a/src/main/java/org/apache/maven/plugins/clean/Fileset.java b/src/main/java/org/apache/maven/plugins/clean/Fileset.java
index ecd88b0..7508411 100644
--- a/src/main/java/org/apache/maven/plugins/clean/Fileset.java
+++ b/src/main/java/org/apache/maven/plugins/clean/Fileset.java
@@ -18,7 +18,7 @@
*/
package org.apache.maven.plugins.clean;
-import java.io.File;
+import java.nio.file.Path;
import java.util.Arrays;
/**
@@ -32,7 +32,7 @@
*/
public class Fileset {
- private File directory;
+ private Path directory;
private String[] includes;
@@ -45,7 +45,7 @@ public class Fileset {
/**
* @return {@link #directory}
*/
- public File getDirectory() {
+ public Path getDirectory() {
return directory;
}
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 8525d42..af5a42f 100644
--- a/src/test/java/org/apache/maven/plugins/clean/CleanMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/clean/CleanMojoTest.java
@@ -28,41 +28,40 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
+import java.util.Properties;
import com.google.inject.Provides;
import com.google.inject.Singleton;
-import org.apache.maven.execution.DefaultMavenExecutionRequest;
-import org.apache.maven.execution.DefaultMavenExecutionResult;
-import org.apache.maven.execution.MavenExecutionRequest;
-import org.apache.maven.execution.MavenExecutionResult;
-import org.apache.maven.execution.MavenSession;
-import org.apache.maven.plugin.MojoExecution;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.testing.junit5.InjectMojo;
-import org.apache.maven.plugin.testing.junit5.MojoTest;
-import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
-import org.codehaus.plexus.PlexusContainer;
+import org.apache.maven.api.MojoExecution;
+import org.apache.maven.api.plugin.MojoException;
+import org.apache.maven.api.plugin.testing.InjectMojo;
+import org.apache.maven.api.plugin.testing.MojoExtension;
+import org.apache.maven.api.plugin.testing.MojoTest;
+import org.apache.maven.api.plugin.testing.stubs.MojoExecutionStub;
+import org.apache.maven.api.plugin.testing.stubs.SessionStub;
+import org.apache.maven.internal.impl.InternalSession;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
-import static org.apache.maven.plugin.testing.junit5.MojoExtension.setVariableValueToObject;
-import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
+import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir;
+import static org.apache.maven.api.plugin.testing.MojoExtension.setVariableValueToObject;
import static org.codehaus.plexus.util.IOUtil.copy;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
+import static org.mockito.Mockito.doReturn;
/**
* Test the clean mojo.
- *
- * @author Vincent Siveton
*/
@MojoTest
public class CleanMojoTest {
+ private static final String LOCAL_REPO = "target/local-repo/";
+
/**
* Tests the simple removal of directories
*
@@ -154,7 +153,7 @@ public void testFilesetsClean(CleanMojo mojo) throws Exception {
@Test
@InjectMojo(goal = "clean", pom = "classpath:/unit/invalid-directory-test/plugin-pom.xml")
public void testCleanInvalidDirectory(CleanMojo mojo) throws Exception {
- assertThrows(MojoExecutionException.class, mojo::execute);
+ assertThrows(MojoException.class, mojo::execute, "Should fail to delete a file treated as a directory");
}
/**
@@ -187,7 +186,7 @@ public void testCleanLockedFile(CleanMojo mojo) throws Exception {
FileLock ignored = channel.lock()) {
mojo.execute();
fail("Should fail to delete a file that is locked");
- } catch (MojoExecutionException expected) {
+ } catch (MojoException expected) {
assertTrue(true);
}
}
@@ -212,7 +211,7 @@ public void testCleanLockedFileWithNoError(CleanMojo mojo) throws Exception {
FileLock ignored = channel.lock()) {
mojo.execute();
assertTrue(true);
- } catch (MojoExecutionException expected) {
+ } catch (MojoException expected) {
fail("Should display a warning when deleting a file that is locked");
}
}
@@ -274,7 +273,7 @@ private void testSymlink(LinkCreator linkCreator) throws Exception {
Files.write(file, Collections.singleton("Hello world"));
linkCreator.createLink(jctDir, orgDir);
// delete
- cleaner.delete(dirWithLnk.toFile(), null, false, true, false);
+ cleaner.delete(dirWithLnk, null, false, true, false);
// verify
assertTrue(Files.exists(file));
assertFalse(Files.exists(jctDir));
@@ -287,7 +286,7 @@ private void testSymlink(LinkCreator linkCreator) throws Exception {
Files.write(file, Collections.singleton("Hello world"));
linkCreator.createLink(jctDir, orgDir);
// delete
- cleaner.delete(dirWithLnk.toFile(), null, true, true, false);
+ cleaner.delete(dirWithLnk, null, true, true, false);
// verify
assertFalse(Files.exists(file));
assertFalse(Files.exists(jctDir));
@@ -298,10 +297,11 @@ private void testSymlink(LinkCreator linkCreator) throws Exception {
@Provides
@Singleton
@SuppressWarnings("unused")
- private MavenSession createSession(PlexusContainer container) {
- MavenExecutionRequest request = new DefaultMavenExecutionRequest();
- MavenExecutionResult result = new DefaultMavenExecutionResult();
- MavenSession session = new MavenSession(container, MavenRepositorySystemUtils.newSession(), request, result);
+ private InternalSession createSession() {
+ InternalSession session = SessionStub.getMockSession(LOCAL_REPO);
+ Properties props = new Properties();
+ props.put("basedir", MojoExtension.getBasedir());
+ doReturn(props).when(session).getSystemProperties();
return session;
}
@@ -309,7 +309,7 @@ private MavenSession createSession(PlexusContainer container) {
@Singleton
@SuppressWarnings("unused")
private MojoExecution createMojoExecution() {
- return new MojoExecution(null);
+ return new MojoExecutionStub("default-clean", "clean");
}
/**