From 92dd40cce14ddf4e8d201848c6ad5361845476a3 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 15 Nov 2024 14:15:28 +0100 Subject: [PATCH] Fix IT related to raw streams --- .../apache/maven/cli/DaemonMavenInvoker.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/daemon/src/main/java/org/apache/maven/cli/DaemonMavenInvoker.java b/daemon/src/main/java/org/apache/maven/cli/DaemonMavenInvoker.java index 3df60c9d9..3d49dd4a0 100644 --- a/daemon/src/main/java/org/apache/maven/cli/DaemonMavenInvoker.java +++ b/daemon/src/main/java/org/apache/maven/cli/DaemonMavenInvoker.java @@ -18,6 +18,11 @@ */ package org.apache.maven.cli; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.util.Optional; + import org.apache.maven.api.cli.InvokerException; import org.apache.maven.api.cli.InvokerRequest; import org.apache.maven.api.cli.Options; @@ -40,11 +45,17 @@ public DaemonMavenInvoker(ProtoLookup protoLookup) { @Override protected Terminal createTerminal(ResidentMavenContext context) { + InputStream in = context.invokerRequest.in().orElseThrow(); + OutputStream out = context.invokerRequest.out().orElseThrow(); + OutputStream err = context.invokerRequest.err().orElseThrow(); + Optional rawStreams = context.invokerRequest.options().rawStreams(); + if (rawStreams.isPresent() && rawStreams.get()) { + System.setOut(printStream(out)); + System.setErr(printStream(err)); + } MessageUtils.systemInstall( builder -> { - builder.streams( - context.invokerRequest.in().get(), - context.invokerRequest.out().get()); + builder.streams(in, out); builder.systemOutput(TerminalBuilder.SystemOutput.ForcedSysOut); builder.provider(TerminalBuilder.PROP_PROVIDER_EXEC); if (context.coloredOutput != null) { @@ -55,6 +66,16 @@ protected Terminal createTerminal(ResidentMavenContext context) { return MessageUtils.getTerminal(); } + private PrintStream printStream(OutputStream outputStream) { + if (outputStream instanceof LoggingOutputStream los) { + return los.printStream(); + } else if (outputStream instanceof PrintStream ps) { + return ps; + } else { + return new PrintStream(outputStream); + } + } + @Override protected org.apache.maven.logging.BuildEventListener doDetermineBuildEventListener(ResidentMavenContext context) { return context.invokerRequest.lookup().lookup(BuildEventListener.class);