From 8cc424970c07df95983027c74b70a76d31e4251f Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 3 Apr 2026 22:09:59 +0200 Subject: [PATCH] Fix #11885: Disable ANSI colors when stdout is piped or redirected on JDK 22+ Change ForcedSysOut to SysOut so that JLine properly checks whether stdout is a TTY before creating a system terminal. ForcedSysOut bypasses this check, which causes JLine to create a non-dumb terminal even when stdout is piped (since stdin is still a TTY), resulting in ANSI escape codes appearing in piped output on JDK 22+ where the FFM provider successfully creates a terminal from stdin alone. With SysOut, JLine detects that stdout is not a TTY and falls back to a dumb terminal, correctly disabling ANSI colors. Co-Authored-By: Claude Opus 4.6 --- .../org/apache/maven/cling/invoker/LookupInvoker.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java index 963d8b1706b7..5ec158321dbd 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java @@ -342,7 +342,7 @@ protected void doCreateTerminal(C context, TerminalBuilder builder) { context.coloredOutput = context.coloredOutput != null ? context.coloredOutput : false; context.closeables.add(out::flush); } else { - builder.systemOutput(TerminalBuilder.SystemOutput.ForcedSysOut); + builder.systemOutput(TerminalBuilder.SystemOutput.SysOut); } if (context.coloredOutput != null) { builder.color(context.coloredOutput); @@ -354,12 +354,10 @@ protected void doCreateTerminal(C context, TerminalBuilder builder) { */ protected final void doConfigureWithTerminal(C context, Terminal terminal) { context.terminal = terminal; - // tricky thing: align what JLine3 detected and Maven thinks: + // Align Maven's color setting with JLine's terminal detection: // if embedded, we default to context.coloredOutput=false unless overridden (see above) - // if not embedded, JLine3 may detect redirection and will create dumb terminal. + // if not embedded, JLine detects redirection via SysOut and will create dumb terminal. // To align Maven with outcomes, we set here color enabled based on these premises. - // Note: Maven3 suffers from similar thing: if you do `mvn3 foo > log.txt`, the output will - // not be not colored (good), but Maven will print out "Message scheme: color". MessageUtils.setColorEnabled( context.coloredOutput != null ? context.coloredOutput : !Terminal.TYPE_DUMB.equals(terminal.getType()));