Fix deprecation warning about JAnsi Terminal (#1017)

And use JNI (and FFM) instead.

Changes:
* use DefaultClient from master (thanks @gnodet !)
* drop jansi (as DefaultClient should not depend on it)
* introduce two new jline3 terminal backends: jni and ffm (used on Java 22+)
* tested/verified (thanks @wendigo )
This commit is contained in:
Tamas Cservenak
2024-06-13 21:48:27 +02:00
committed by GitHub
parent 359cfeff10
commit f32d00003a
6 changed files with 61 additions and 27 deletions

View File

@@ -98,6 +98,7 @@
<enforceBytecodeVersion>
<maxJdkVersion>${maven.compiler.release}</maxJdkVersion>
<excludes>
<exclude>org.jline:jline-terminal-ffm</exclude>
<exclude>org.graalvm.nativeimage:svm</exclude>
</excludes>
</enforceBytecodeVersion>

View File

@@ -41,9 +41,10 @@ import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.internal.CLibrary;
import org.jline.terminal.spi.SystemStream;
import org.jline.terminal.spi.TerminalExt;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.mvndaemon.mvnd.common.DaemonException;
import org.mvndaemon.mvnd.common.DaemonInfo;
@@ -61,7 +62,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;
import org.slf4j.impl.MvndLoggerFactory;
import org.slf4j.impl.StaticLoggerBinder;
import static org.mvndaemon.mvnd.client.DaemonParameters.LOG_EXTENSION;
@@ -94,19 +94,6 @@ public class DefaultClient implements Client {
final boolean batchMode = Environment.MAVEN_BATCH_MODE.hasCommandLineOption(args)
|| Environment.COMPLETION.hasCommandLineOption(args);
// Color
Color styleColor =
Color.of(Environment.MAVEN_COLOR.removeCommandLineOption(args)).orElse(Color.auto);
if (styleColor == Color.auto) {
/* Translate from auto to either always or never */
/* stdout is not a terminal e.g. when stdout is redirected to a file */
final boolean stdoutIsTerminal = CLibrary.isatty(1) != 0;
styleColor = (batchMode || logFile != null || !stdoutIsTerminal) ? Color.never : Color.always;
}
/* We cannot use Environment.addCommandLineOption() because that one would pass --color to the daemon
* and --color is not supported there yet. */
args.add("-D" + Environment.MAVEN_COLOR.getProperty() + "=" + styleColor.name());
String userJdkJavaOpts = System.getenv(Environment.JDK_JAVA_OPTIONS.getEnvironmentVariable());
if (userJdkJavaOpts != null) {
Environment.JDK_JAVA_OPTIONS.addCommandLineOption(args, userJdkJavaOpts);
@@ -115,8 +102,8 @@ public class DefaultClient implements Client {
// System properties
setSystemPropertiesFromCommandLine(args);
if (StaticLoggerBinder.getSingleton().getLoggerFactory() instanceof MvndLoggerFactory) {
((MvndLoggerFactory) StaticLoggerBinder.getSingleton().getLoggerFactory()).reconfigure();
if (LoggerFactory.getILoggerFactory() instanceof MvndLoggerFactory) {
((MvndLoggerFactory) LoggerFactory.getILoggerFactory()).reconfigure();
}
DaemonParameters parameters = new DaemonParameters();
@@ -158,6 +145,21 @@ public class DefaultClient implements Client {
boolean noBuffering = batchMode || parameters.noBuffering();
try (TerminalOutput output = new TerminalOutput(noBuffering, parameters.rollingWindowSize(), logFile)) {
try {
// Color
// We need to defer this part until the terminal is created
Color styleColor = Color.of(Environment.MAVEN_COLOR.removeCommandLineOption(args))
.orElse(Color.auto);
if (styleColor == Color.auto) {
/* Translate from auto to either always or never */
/* stdout is not a terminal e.g. when stdout is redirected to a file */
final boolean stdoutIsTerminal =
((TerminalExt) output.getTerminal()).getProvider().isSystemStream(SystemStream.Output);
styleColor = (batchMode || logFile != null || !stdoutIsTerminal) ? Color.never : Color.always;
}
/* We cannot use Environment.addCommandLineOption() because that one would pass --color to the daemon
* and --color is not supported there yet. */
args.add("-D" + Environment.MAVEN_COLOR.getProperty() + "=" + styleColor.name());
final ExecutionResult result = new DefaultClient(parameters).execute(output, args);
exitCode = result.getExitCode();
} catch (DaemonException.InterruptedException e) {
@@ -251,8 +253,12 @@ public class DefaultClient implements Client {
+ " (" + buildProperties.getRevision() + ")";
boolean isColored = !"never".equals(Environment.MAVEN_COLOR.getCommandLineOption(args));
final String v =
isColored ? Ansi.ansi().bold().a(mvndVersionString).reset().toString() : mvndVersionString;
final String v = isColored
? new AttributedStringBuilder()
.style(AttributedStyle.BOLD)
.append(mvndVersionString)
.toAnsi()
: mvndVersionString;
output.accept(Message.log(v));
// Print terminal information
output.describeTerminal();

View File

@@ -43,7 +43,11 @@
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jansi</artifactId>
<artifactId>jline-terminal-jni</artifactId>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-ffm</artifactId>
</dependency>
<dependency>

View File

@@ -498,6 +498,10 @@ public class TerminalOutput implements ClientOutput {
return terminal.getWidth();
}
public Terminal getTerminal() {
return terminal;
}
void readInputLoop() {
try {
while (!closing) {

View File

@@ -61,7 +61,10 @@
<artifact id="org.jline:jline-terminal">
<exclusion id="*:*"/>
</artifact>
<artifact id="org.jline:jline-terminal-jansi">
<artifact id="org.jline:jline-terminal-ffm">
<exclusion id="*:*"/>
</artifact>
<artifact id="org.jline:jline-terminal-jni">
<exclusion id="*:*"/>
</artifact>
</artifactSet>

26
pom.xml
View File

@@ -82,7 +82,6 @@
<graalvm.plugin.version>0.10.2</graalvm.plugin.version>
<groovy.version>4.0.21</groovy.version>
<jakarta.inject.version>1.0</jakarta.inject.version>
<jansi.version>2.4.1</jansi.version>
<jline.version>3.26.1</jline.version>
<maven.version>3.9.8</maven.version>
<!-- Keep in sync with Maven -->
@@ -288,13 +287,13 @@
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-jansi</artifactId>
<artifactId>jline-terminal-jni</artifactId>
<version>${jline.version}</version>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>${jansi.version}</version>
<groupId>org.jline</groupId>
<artifactId>jline-terminal-ffm</artifactId>
<version>${jline.version}</version>
</dependency>
<dependency>
@@ -527,6 +526,23 @@
<fail>true</fail>
</configuration>
</execution>
<execution>
<id>enforce-bytecode-version</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<enforceBytecodeVersion>
<maxJdkVersion>${maven.compiler.target}</maxJdkVersion>
<excludes>
<exclude>org.jline:jline-terminal-ffm</exclude>
</excludes>
</enforceBytecodeVersion>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
<plugin>