slachiewicz opened a new pull request, #14009:
URL: https://github.com/apache/maven/pull/14009

   Since #13888 removed JLine, nothing switches on virtual terminal processing, 
so in the classic Windows console (conhost, which runs `cmd.exe` outside 
Windows Terminal) `--color=auto` stays plain, and `--color=always` prints the 
escape codes as text. This restores colors there on Java 22 and later without 
bundling native code.
   
   - `WindowsConsole` calls kernel32 `SetConsoleMode` through 
`java.lang.foreign`, by reflection because `maven-jline` compiles for Java 8. 
It runs only on Java 22+ and only when native access is enabled for it, so the 
JDK never prints its restricted method warning. A shutdown hook restores the 
original console mode.
   - The call happens only after the CLI has decided on color: auto mode in the 
classic console, or `--color=always`. Embedded use, `-B`, `-l` and 
`--color=never` never touch the console.
   - `mvn.cmd` passes `--enable-native-access=ALL-UNNAMED` again, which 
a95c0160d5 removed, but only when the JDK's `release` file reports Java 22 or 
later. The file is read directly, so no extra JVM or child process starts. The 
`mvn` shell script is unchanged.
   
   Results in new conhost windows on Windows Server 2022, with the window 
buffer read back after `mvn validate`:
   
   | JDK | `--color=auto` | `--color=always` |
   |---|---|---|
   | 8, 17, 21 | plain (unchanged) | escape codes printed as text (unchanged) |
   | 22, 25 | colored | colored |
   
   Java under `C:\Program Files (x86)` and `-B` behave as expected, and the 
console mode is the same before and after each run.
   
   Limits:
   - Switching on the mode costs 170 to 182 ms at startup on a 2-vCPU VM, 
measured in process; most of it is the JDK's one-time setup of the first 
downcall handle. Building the handles on a background thread during startup 
would hide it; this PR does not do that.
   - No colors when `java.exe` comes from a `PATH` shim without a `release` 
file next to it (Oracle `javapath`, Scoop), or when Maven starts without 
`mvn.cmd` (the jar Maven Wrapper, IDE runners, Git Bash).
   
   Verified: `mvn verify -pl maven-jline,maven-embedder -am` (JDK 17) → 
`maven-core` 430 tests, `maven-embedder` 56, `maven-jline` 9; `maven-jline` 
tests on Windows with JDK 22 and 25, where the kernel32 test runs. Not run on 
Windows 10 or 11 desktops, Windows on ARM, or JDK 23 and 24.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to