From 76dbe50057405d15188ad58db22aafe9d7895196 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 24 Jun 2024 16:35:29 +0200 Subject: [PATCH] Cleanup --- pom.xml | 52 ++++++------------- .../apache/maven/plugins/clean/Cleaner.java | 50 +++++++++++------- 2 files changed, 49 insertions(+), 53 deletions(-) diff --git a/pom.xml b/pom.xml index 37d6f3c..a7ef0bb 100644 --- a/pom.xml +++ b/pom.xml @@ -62,21 +62,13 @@ under the License. 4.0.0-beta-3 - 2023-06-14T18:50:52Z 17 + 3.7.0 + 4.0.0-alpha-3-SNAPSHOT 4.0.0-SNAPSHOT + 2024-06-16T10:25:11Z - - - - org.apache.maven - maven-di - ${mavenVersion} - - - - org.apache.maven @@ -84,30 +76,34 @@ under the License. ${mavenVersion} provided + + org.apache.maven + maven-api-di + ${mavenVersion} + provided + + + org.apache.maven + maven-api-meta + ${mavenVersion} + provided + org.codehaus.plexus plexus-utils - 4.0.0 org.apache.maven.plugin-testing maven-plugin-testing-harness - 4.0.0-alpha-3-SNAPSHOT - test - - - org.codehaus.plexus - plexus-xml - 4.0.2 + ${version.maven-plugin-testing} test org.junit.jupiter junit-jupiter-api - 5.10.1 test @@ -132,28 +128,14 @@ under the License. - - org.apache.maven.plugins - maven-compiler-plugin - 3.13.0 - - - - org.apache.maven - maven-di - - - - org.apache.maven.plugins maven-plugin-plugin - 4.0.0-SNAPSHOT + ${version.maven-plugin-tools} org.apache.maven.plugins maven-surefire-plugin - 3.2.5 true 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 ab5f1dd..533a224 100644 --- a/src/main/java/org/apache/maven/plugins/clean/Cleaner.java +++ b/src/main/java/org/apache/maven/plugins/clean/Cleaner.java @@ -27,6 +27,8 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayDeque; import java.util.Deque; +import java.util.function.BiConsumer; +import java.util.function.Consumer; import java.util.stream.Stream; import org.apache.maven.api.Event; @@ -79,11 +81,11 @@ class Cleaner { * @param fastMode The fast deletion mode. */ Cleaner(Session session, Log log, boolean verbose, Path fastDir, String fastMode) { - logDebug = (log == null || !log.isDebugEnabled()) ? null : log::debug; + logDebug = (log == null || !log.isDebugEnabled()) ? null : logger(log::debug, log::debug); - logInfo = (log == null || !log.isInfoEnabled()) ? null : log::info; + logInfo = (log == null || !log.isInfoEnabled()) ? null : logger(log::info, log::info); - logWarn = (log == null || !log.isWarnEnabled()) ? null : log::warn; + logWarn = (log == null || !log.isWarnEnabled()) ? null : logger(log::warn, log::warn); logVerbose = verbose ? logInfo : logDebug; @@ -92,6 +94,20 @@ class Cleaner { this.fastMode = fastMode; } + private Logger logger(Consumer l1, BiConsumer l2) { + return new Logger() { + @Override + public void log(CharSequence message) { + l1.accept(message); + } + + @Override + public void log(CharSequence message, Throwable t) { + l2.accept(message, t); + } + }; + } + /** * Deletes the specified directories and its contents. * @@ -152,8 +168,7 @@ private boolean fastDelete(Path baseDir) { } } 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.log("Unable to fast delete directory", e); } return false; } @@ -165,9 +180,10 @@ private boolean fastDelete(Path baseDir) { } } 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); + logDebug.log( + "Unable to fast delete directory as the path " + fastDir + + " does not point to a directory or cannot be created", + e); } return false; } @@ -184,8 +200,7 @@ private boolean fastDelete(Path baseDir) { return true; } 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.log("Unable to fast delete directory", e); } return false; } @@ -281,7 +296,6 @@ private int delete(Path file, boolean failOnError, boolean retryOnError) throws Files.deleteIfExists(file); return 0; } catch (IOException e) { - boolean deleted = false; IOException exception = new IOException("Failed to delete " + file); exception.addSuppressed(e); @@ -292,9 +306,9 @@ private int delete(Path file, boolean failOnError, boolean retryOnError) throws } final int[] delays = {50, 250, 750}; - for (int i = 0; !deleted && i < delays.length; i++) { + for (int delay : delays) { try { - Thread.sleep(delays[i]); + Thread.sleep(delay); } catch (InterruptedException e2) { exception.addSuppressed(e2); } @@ -305,16 +319,14 @@ private int delete(Path file, boolean failOnError, boolean retryOnError) throws exception.addSuppressed(e2); } } - } else { - deleted = !Files.exists(file); } - if (!deleted) { + if (Files.exists(file)) { if (failOnError) { - throw new IOException("Failed to delete " + file); + throw new IOException("Failed to delete " + file, exception); } else { if (logWarn != null) { - logWarn.log("Failed to delete " + file); + logWarn.log("Failed to delete " + file, exception); } return 1; } @@ -339,6 +351,8 @@ public void update(Result result) { private interface Logger { void log(CharSequence message); + + void log(CharSequence message, Throwable t); } private static class BackgroundCleaner extends Thread {