You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2026/04/11 22:49:21 UTC

(maven-script-interpreter) branch custom-outstream created (now 1425ffa)

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

sjaranowski pushed a change to branch custom-outstream
in repository https://gitbox.apache.org/repos/asf/maven-script-interpreter.git


      at 1425ffa  Refactor `FileLogger` to allow customizable output stream creation

This branch includes the following new commits:

     new 1425ffa  Refactor `FileLogger` to allow customizable output stream creation

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(maven-script-interpreter) 01/01: Refactor `FileLogger` to allow customizable output stream creation

Posted by sj...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch custom-outstream
in repository https://gitbox.apache.org/repos/asf/maven-script-interpreter.git

commit 1425ffa321389a9a2fcb42f4a7fb66ebc8f5b27c
Author: Slawomir Jaranowski <s....@gmail.com>
AuthorDate: Sun Apr 12 00:49:06 2026 +0200

    Refactor `FileLogger` to allow customizable output stream creation
---
 .../maven/shared/scriptinterpreter/FileLogger.java      | 17 +++++++++++++++--
 .../maven/shared/scriptinterpreter/FileLoggerTest.java  | 15 +++++----------
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java b/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java
index a9462b2..257a2b6 100644
--- a/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java
+++ b/src/main/java/org/apache/maven/shared/scriptinterpreter/FileLogger.java
@@ -23,6 +23,8 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintStream;
 import java.nio.file.Files;
+import java.nio.file.OpenOption;
+import java.nio.file.Path;
 
 /**
  * <p>FileLogger class.</p>
@@ -62,8 +64,9 @@ public class FileLogger implements ExecutionLogger, AutoCloseable {
         OutputStream outputStream;
 
         if (outputFile != null) {
-            outputFile.getParentFile().mkdirs();
-            outputStream = Files.newOutputStream(outputFile.toPath());
+            Path outputPath = outputFile.toPath();
+            Files.createDirectories(outputPath.getParent());
+            outputStream = createOutputStream(outputPath);
         } else {
             outputStream = new NullOutputStream();
         }
@@ -75,6 +78,16 @@ public class FileLogger implements ExecutionLogger, AutoCloseable {
         }
     }
 
+    /**
+     * <p>Override this method to create a custom output stream.
+     *
+     * <p>By default, stream is created with {@link Files#newOutputStream(Path, OpenOption...)},
+     * which truncate the existing file.
+     */
+    protected OutputStream createOutputStream(Path outputPath) throws IOException {
+        return Files.newOutputStream(outputPath);
+    }
+
     /**
      * Gets the path to the output file.
      *
diff --git a/src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java b/src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java
index dc17b96..47bcfda 100644
--- a/src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java
+++ b/src/test/java/org/apache/maven/shared/scriptinterpreter/FileLoggerTest.java
@@ -22,6 +22,7 @@ import java.io.File;
 import java.nio.file.Files;
 
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNull;
@@ -75,11 +76,8 @@ public class FileLoggerTest {
     }
 
     @Test
-    void outputFileNoMirror() throws Exception {
-        File outputFile = new File("target/test.log");
-        if (outputFile.exists()) {
-            outputFile.delete();
-        }
+    void outputFileNoMirror(@TempDir File tempDir) throws Exception {
+        File outputFile = new File(tempDir, "/target/test.log");
 
         try (FileLogger fileLogger = new FileLogger(outputFile)) {
             fileLogger.consumeLine("Test1");
@@ -94,11 +92,8 @@ public class FileLoggerTest {
     }
 
     @Test
-    void outputFileWithMirror() throws Exception {
-        File outputFile = new File("target/test.log");
-        if (outputFile.exists()) {
-            outputFile.delete();
-        }
+    void outputFileWithMirror(@TempDir File tempDir) throws Exception {
+        File outputFile = new File(tempDir, "target/test.log");
         TestMirrorHandler mirrorHandler = new TestMirrorHandler();
 
         try (FileLogger fileLogger = new FileLogger(outputFile, mirrorHandler)) {