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

gnodet pushed a commit to branch maven-3.10.x
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/maven-3.10.x by this push:
     new 8c795977a8 fix: add JAnsi compatibility shim to maven-jline and narrow 
jansi exports in extension.xml
8c795977a8 is described below

commit 8c795977a82919b95ed5e8a688842b11b0a75c11
Author: Arturo Bernal <[email protected]>
AuthorDate: Wed Nov 4 09:05:49 2026 +0100

    fix: add JAnsi compatibility shim to maven-jline and narrow jansi exports 
in extension.xml
    
    Plugins calling AnsiConsole.systemInstall() raced Maven's own JLine 
terminal for System.out/System.err ownership. maven-jline now provides 
AnsiConsole, AnsiPrintStream and AnsiMode forwarding shims so a plugin's own 
jansi dependency resolves to these instead of the real AnsiConsole. The 
wildcard org.fusesource.jansi.* exportedPackage is replaced with four named 
exports (the only classes maven-jline actually provides); the jansi artifact 
exclusion is kept and explained.
    
    Fixes #13726
---
 .../main/resources/META-INF/maven/extension.xml    | 30 ++++++--
 .../project/ExtensionDescriptorBuilderTest.java    | 43 +++++++++++
 .../src/main/java/org/fusesource/jansi/Ansi.java   |  2 +
 .../java/org/fusesource/jansi/AnsiConsole.java     | 56 ++++++++++++++
 .../main/java/org/fusesource/jansi/AnsiMode.java   | 33 ++++++++
 .../java/org/fusesource/jansi/AnsiPrintStream.java | 68 +++++++++++++++++
 .../java/org/fusesource/jansi/AnsiConsoleTest.java | 88 ++++++++++++++++++++++
 7 files changed, 315 insertions(+), 5 deletions(-)

diff --git a/maven-core/src/main/resources/META-INF/maven/extension.xml 
b/maven-core/src/main/resources/META-INF/maven/extension.xml
index aa333bcabe..bb6f52745a 100644
--- a/maven-core/src/main/resources/META-INF/maven/extension.xml
+++ b/maven-core/src/main/resources/META-INF/maven/extension.xml
@@ -107,7 +107,7 @@ under the License.
     <exportedPackage>javax.annotation.*</exportedPackage>
     <exportedPackage>javax.annotation.security.*</exportedPackage>
 
-    <!-- 
+    <!--
       | We may potentially want to export these, but right now I'm not sure 
that anything Guice specific needs
       | to be made available to plugin authors. If we find people are getting 
fancy and want to take advantage of
       | Guice specifics we can expose that later. Really some testing needs to 
be done to see full hiding
@@ -127,8 +127,20 @@ under the License.
     <exportedPackage>org.slf4j.helpers.*</exportedPackage>
     <exportedPackage>org.slf4j.event.*</exportedPackage>
 
-    <!-- JAnsi -->
-    <exportedPackage>org.fusesource.jansi.*</exportedPackage>
+    <!-- JAnsi: maven-jline ships Ansi, AnsiConsole, AnsiPrintStream and 
AnsiMode, forwarding to the
+         already-installed JLine terminal, for backward compatibility with 
code (e.g. maven-shared-utils)
+         still calling the JAnsi API. The jansi artifact itself stays excluded 
below, so a plugin that
+         declares org.fusesource.jansi:jansi gets these forwarding classes 
instead of its own copy: that
+         is what keeps AnsiConsole.systemInstall()/systemUninstall() calls 
routed through the single
+         console Maven owns, instead of a second, independent install racing 
it for ownership of
+         System.out/System.err. Each class is named exactly, not as 
org.fusesource.jansi.*, since these
+         four are the only ones maven-jline provides; any other class in that 
package a plugin needs
+         falls back to its own classpath (see ClassRealm#loadClassFromImport). 
See the discussion on
+         #13727. -->
+    <exportedPackage>org.fusesource.jansi.Ansi</exportedPackage>
+    <exportedPackage>org.fusesource.jansi.AnsiConsole</exportedPackage>
+    <exportedPackage>org.fusesource.jansi.AnsiPrintStream</exportedPackage>
+    <exportedPackage>org.fusesource.jansi.AnsiMode</exportedPackage>
   </exportedPackages>
 
   <exportedArtifacts>
@@ -175,10 +187,18 @@ under the License.
     <exportedArtifact>javax.inject:javax.inject</exportedArtifact>
     <exportedArtifact>javax.annotation:javax.annotation-api</exportedArtifact>
     <exportedArtifact>org.slf4j:slf4j-api</exportedArtifact>
+
+    <!--
+      | JAnsi is no longer a maven-core dependency (replaced by JLine, see 
#11874), but stays excluded here:
+      | maven-jline exports forwarding classes for the JAnsi API (see 
exportedPackages above), and excluding
+      | the real jansi artifact is what makes a plugin's own 
org.fusesource.jansi:jansi dependency resolve to
+      | those forwarding classes instead of shadowing them with the real 
AnsiConsole, which would install its
+      | own System.out/System.err independently of the terminal Maven already 
installed. See #13726, #13727.
+     -->
     <exportedArtifact>org.fusesource.jansi:jansi</exportedArtifact>
 
     <!--
-      | We must also filter out the old Aether or NoClassDefFoundErrors will 
surface  
+      | We must also filter out the old Aether or NoClassDefFoundErrors will 
surface
      -->
     <exportedArtifact>org.sonatype.aether:aether-api</exportedArtifact>
     <exportedArtifact>org.sonatype.aether:aether-spi</exportedArtifact>
@@ -193,7 +213,7 @@ under the License.
       | NOTE: Don't exclude the wagons or any of their dependencies (apart 
from the wagon API). This would otherwise
       | provoke linkage errors for wagons contributed by build extensions. We 
also don't need to exclude the wagons
       | from plugins. Plugins that use wagons directly and declare the 
corresponding dependency will simply use a
-      | wagon from their plugin realm. 
+      | wagon from their plugin realm.
      -->
   </exportedArtifacts>
 </extension>
diff --git 
a/maven-core/src/test/java/org/apache/maven/project/ExtensionDescriptorBuilderTest.java
 
b/maven-core/src/test/java/org/apache/maven/project/ExtensionDescriptorBuilderTest.java
index 79e535f649..d94e84b855 100644
--- 
a/maven-core/src/test/java/org/apache/maven/project/ExtensionDescriptorBuilderTest.java
+++ 
b/maven-core/src/test/java/org/apache/maven/project/ExtensionDescriptorBuilderTest.java
@@ -22,6 +22,7 @@
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
+import java.util.stream.Collectors;
 
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -76,4 +77,46 @@ public void testCompleteDescriptor() throws Exception {
         assertEquals(Arrays.asList("a", "b", "c"), ed.getExportedPackages());
         assertEquals(Arrays.asList("x", "y", "z"), ed.getExportedArtifacts());
     }
+
+    /**
+     * maven-core's own {@code META-INF/maven/extension.xml} must keep 
excluding {@code org.fusesource.jansi:jansi}
+     * as an {@code exportedArtifact} even though jansi is no longer a 
maven-core dependency (replaced by JLine, see
+     * #11874): maven-jline exports forwarding classes for the JAnsi API (see
+     * {@code org.apache.maven.jline}{@code .*} and {@code 
org.fusesource.jansi.*} in maven-jline), and it is the
+     * {@code exportedArtifact} exclusion that makes a plugin's own {@code 
org.fusesource.jansi:jansi} dependency
+     * resolve to those forwarding classes instead of shadowing them with the 
real {@code AnsiConsole}, which would
+     * install its own {@code System.out}/{@code System.err} independently of 
the terminal Maven already installed
+     * (see #13726, #13727).
+     * <p>
+     * The four {@code exportedPackage} entries are named exactly, not as the 
{@code org.fusesource.jansi.*}
+     * wildcard, since {@code Ansi}, {@code AnsiConsole}, {@code 
AnsiPrintStream} and {@code AnsiMode} are the only
+     * classes maven-jline provides; any other class in that package a plugin 
needs falls back to its own classpath
+     * (see {@code ClassRealm#loadClassFromImport}).
+     */
+    @Test
+    public void testCoreExtensionDescriptorHasNoStaleArtifactExports() throws 
Exception {
+        try (InputStream in =
+                
getClass().getClassLoader().getResourceAsStream(builder.getExtensionDescriptorLocation()))
 {
+            assertNotNull(in, "maven-core's own extension descriptor must be 
on the test classpath");
+
+            ExtensionDescriptor ed = builder.build(in);
+
+            assertTrue(
+                    
ed.getExportedArtifacts().contains("org.fusesource.jansi:jansi"),
+                    "jansi must stay excluded so a plugin's own jansi 
dependency resolves to maven-jline's"
+                            + " forwarding classes instead of shadowing them: 
" + ed.getExportedArtifacts());
+            assertEquals(
+                    Arrays.asList(
+                            "org.fusesource.jansi.Ansi",
+                            "org.fusesource.jansi.AnsiConsole",
+                            "org.fusesource.jansi.AnsiPrintStream",
+                            "org.fusesource.jansi.AnsiMode"),
+                    ed.getExportedPackages().stream()
+                            .filter(p -> p.startsWith("org.fusesource.jansi"))
+                            .collect(Collectors.toList()),
+                    "the JAnsi compatibility shim exports must stay, named 
exactly (not as the"
+                            + " org.fusesource.jansi.* package, which 
maven-jline does not fully provide): "
+                            + ed.getExportedPackages());
+        }
+    }
 }
diff --git a/maven-jline/src/main/java/org/fusesource/jansi/Ansi.java 
b/maven-jline/src/main/java/org/fusesource/jansi/Ansi.java
index f861da0098..820556d9c4 100644
--- a/maven-jline/src/main/java/org/fusesource/jansi/Ansi.java
+++ b/maven-jline/src/main/java/org/fusesource/jansi/Ansi.java
@@ -27,6 +27,8 @@
  * This class comes from Jansi and is provided for backward compatibility
  * with maven-shared-utils, while Maven has migrated to JLine (into which 
Jansi has been merged
  * since JLine 3.25.0).
+ *
+ * @see AnsiConsole
  */
 @Deprecated
 @SuppressWarnings("unused")
diff --git a/maven-jline/src/main/java/org/fusesource/jansi/AnsiConsole.java 
b/maven-jline/src/main/java/org/fusesource/jansi/AnsiConsole.java
new file mode 100644
index 0000000000..f0b59a40b7
--- /dev/null
+++ b/maven-jline/src/main/java/org/fusesource/jansi/AnsiConsole.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.fusesource.jansi;
+
+/**
+ * Forwards to {@link org.jline.jansi.AnsiConsole} instead of performing its 
own install, so that code
+ * calling the JAnsi API, such as maven-shared-utils, shares the single 
console Maven itself installs
+ * through {@code org.apache.maven.jline.MessageUtils} rather than racing it 
for ownership of
+ * {@code System.out}/{@code System.err}.
+ *
+ * This class comes from Jansi and is provided for backward compatibility with 
maven-shared-utils,
+ * while Maven has migrated to JLine (into which Jansi has been merged since 
JLine 3.25.0).
+ */
+@Deprecated
+public class AnsiConsole {
+
+    private static final AnsiPrintStream OUT = new AnsiPrintStream();
+
+    private AnsiConsole() {}
+
+    public static synchronized void systemInstall() {
+        org.jline.jansi.AnsiConsole.systemInstall();
+    }
+
+    public static synchronized boolean isInstalled() {
+        return org.jline.jansi.AnsiConsole.isInstalled();
+    }
+
+    public static synchronized void systemUninstall() {
+        org.jline.jansi.AnsiConsole.systemUninstall();
+    }
+
+    public static AnsiPrintStream out() {
+        return OUT;
+    }
+
+    public static int getTerminalWidth() {
+        return org.jline.jansi.AnsiConsole.getTerminalWidth();
+    }
+}
diff --git a/maven-jline/src/main/java/org/fusesource/jansi/AnsiMode.java 
b/maven-jline/src/main/java/org/fusesource/jansi/AnsiMode.java
new file mode 100644
index 0000000000..c295e4731c
--- /dev/null
+++ b/maven-jline/src/main/java/org/fusesource/jansi/AnsiMode.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.fusesource.jansi;
+
+/**
+ * Mirrors the real JAnsi {@code AnsiMode} enum so code compiled against 
JAnsi, such as
+ * maven-shared-utils, keeps resolving against this type at runtime.
+ *
+ * This class comes from Jansi and is provided for backward compatibility with 
maven-shared-utils,
+ * while Maven has migrated to JLine (into which Jansi has been merged since 
JLine 3.25.0).
+ */
+@Deprecated
+public enum AnsiMode {
+    Strip,
+    Default,
+    Force
+}
diff --git 
a/maven-jline/src/main/java/org/fusesource/jansi/AnsiPrintStream.java 
b/maven-jline/src/main/java/org/fusesource/jansi/AnsiPrintStream.java
new file mode 100644
index 0000000000..d5e148bf4d
--- /dev/null
+++ b/maven-jline/src/main/java/org/fusesource/jansi/AnsiPrintStream.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.fusesource.jansi;
+
+import java.io.OutputStream;
+import java.io.PrintStream;
+
+/**
+ * Forwards everything written to it to whatever {@code System.out} currently 
is, which is the stream
+ * JLine installed. Unlike real JAnsi, this stream does not decide for itself 
whether to strip, force or
+ * pass through ANSI escapes - that decision belongs to the terminal Maven 
already set up - so
+ * {@link #setMode} is a no-op.
+ *
+ * This class comes from Jansi and is provided for backward compatibility with 
maven-shared-utils,
+ * while Maven has migrated to JLine (into which Jansi has been merged since 
JLine 3.25.0).
+ */
+@Deprecated
+public class AnsiPrintStream extends PrintStream {
+
+    AnsiPrintStream() {
+        super(
+                new OutputStream() {
+                    @Override
+                    public void write(int b) {
+                        System.out.write(b);
+                    }
+
+                    @Override
+                    public void write(byte[] b, int off, int len) {
+                        System.out.write(b, off, len);
+                    }
+
+                    @Override
+                    public void flush() {
+                        System.out.flush();
+                    }
+                },
+                true);
+    }
+
+    public AnsiMode getMode() {
+        return AnsiMode.Default;
+    }
+
+    public void setMode(AnsiMode mode) {
+        // no-op: the terminal Maven installed already decides whether ANSI is 
forced, stripped or kept
+    }
+
+    public int getTerminalWidth() {
+        return AnsiConsole.getTerminalWidth();
+    }
+}
diff --git 
a/maven-jline/src/test/java/org/fusesource/jansi/AnsiConsoleTest.java 
b/maven-jline/src/test/java/org/fusesource/jansi/AnsiConsoleTest.java
new file mode 100644
index 0000000000..bac1816a1f
--- /dev/null
+++ b/maven-jline/src/test/java/org/fusesource/jansi/AnsiConsoleTest.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.fusesource.jansi;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+/**
+ * Verifies that this JAnsi compatibility shim shares its lifecycle with 
{@link org.jline.jansi.AnsiConsole}
+ * instead of running an install of its own (see #13726, #13727).
+ */
+class AnsiConsoleTest {
+
+    @AfterEach
+    void uninstall() {
+        if (AnsiConsole.isInstalled()) {
+            AnsiConsole.systemUninstall();
+        }
+    }
+
+    @Test
+    void systemInstallAndUninstallDelegateToJLinesAnsiConsole() {
+        assertFalse(AnsiConsole.isInstalled());
+
+        AnsiConsole.systemInstall();
+        assertEquals(org.jline.jansi.AnsiConsole.isInstalled(), 
AnsiConsole.isInstalled());
+
+        AnsiConsole.systemUninstall();
+        assertFalse(AnsiConsole.isInstalled());
+        assertEquals(org.jline.jansi.AnsiConsole.isInstalled(), 
AnsiConsole.isInstalled());
+    }
+
+    @Test
+    void outWritesToWhicheverStreamIsCurrentlySystemOut() {
+        PrintStream original = System.out;
+        ByteArrayOutputStream captured = new ByteArrayOutputStream();
+        try {
+            System.setOut(new PrintStream(captured));
+            AnsiConsole.out().print("hello");
+            AnsiConsole.out().flush();
+        } finally {
+            System.setOut(original);
+        }
+
+        assertEquals("hello", captured.toString());
+    }
+
+    @Test
+    void outReturnsTheSameInstanceEveryCall() {
+        assertSame(AnsiConsole.out(), AnsiConsole.out());
+    }
+
+    @Test
+    void getTerminalWidthDelegatesToJLinesAnsiConsole() {
+        assertEquals(org.jline.jansi.AnsiConsole.getTerminalWidth(), 
AnsiConsole.getTerminalWidth());
+    }
+
+    @Test
+    void setModeOnTheSharedPrintStreamIsANoOp() {
+        AnsiPrintStream out = AnsiConsole.out();
+        assertDoesNotThrow(() -> out.setMode(AnsiMode.Force));
+        assertEquals(AnsiMode.Default, out.getMode());
+    }
+}

Reply via email to