Skip to content

Commit 8cc4249

Browse files
gnodetclaude
andcommitted
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 <noreply@anthropic.com>
1 parent ed79e94 commit 8cc4249

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ protected void doCreateTerminal(C context, TerminalBuilder builder) {
342342
context.coloredOutput = context.coloredOutput != null ? context.coloredOutput : false;
343343
context.closeables.add(out::flush);
344344
} else {
345-
builder.systemOutput(TerminalBuilder.SystemOutput.ForcedSysOut);
345+
builder.systemOutput(TerminalBuilder.SystemOutput.SysOut);
346346
}
347347
if (context.coloredOutput != null) {
348348
builder.color(context.coloredOutput);
@@ -354,12 +354,10 @@ protected void doCreateTerminal(C context, TerminalBuilder builder) {
354354
*/
355355
protected final void doConfigureWithTerminal(C context, Terminal terminal) {
356356
context.terminal = terminal;
357-
// tricky thing: align what JLine3 detected and Maven thinks:
357+
// Align Maven's color setting with JLine's terminal detection:
358358
// if embedded, we default to context.coloredOutput=false unless overridden (see above)
359-
// if not embedded, JLine3 may detect redirection and will create dumb terminal.
359+
// if not embedded, JLine detects redirection via SysOut and will create dumb terminal.
360360
// To align Maven with outcomes, we set here color enabled based on these premises.
361-
// Note: Maven3 suffers from similar thing: if you do `mvn3 foo > log.txt`, the output will
362-
// not be not colored (good), but Maven will print out "Message scheme: color".
363361
MessageUtils.setColorEnabled(
364362
context.coloredOutput != null ? context.coloredOutput : !Terminal.TYPE_DUMB.equals(terminal.getType()));
365363

0 commit comments

Comments
 (0)