From 59c5466c4093a94c9c32d09509bba738fa51d489 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 21 Jan 2021 18:22:18 +0100 Subject: [PATCH] [MNG-7080] Add a --color option to simplify color support --- .../src/main/java/org/apache/maven/cli/CLIManager.java | 3 +++ .../src/main/java/org/apache/maven/cli/MavenCli.java | 2 ++ .../org/apache/maven/cli/CLIManagerDocumentationTest.java | 4 +++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java b/maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java index a63f36643e..a374615ae9 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/CLIManager.java @@ -108,6 +108,8 @@ public class CLIManager public static final String NO_TRANSFER_PROGRESS = "ntp"; + public static final String COLOR = "color"; + protected Options options; @SuppressWarnings( "checkstyle:linelength" ) @@ -150,6 +152,7 @@ public class CLIManager options.addOption( Option.builder( LEGACY_LOCAL_REPOSITORY ).longOpt( "legacy-local-repository" ).desc( "Use Maven 2 Legacy Local Repository behaviour, ie no use of _remote.repositories. Can also be activated by using -Dmaven.legacyLocalRepo=true" ).build() ); options.addOption( Option.builder( BUILDER ).longOpt( "builder" ).hasArg().desc( "The id of the build strategy to use" ).build() ); options.addOption( Option.builder( NO_TRANSFER_PROGRESS ).longOpt( "no-transfer-progress" ).desc( "Do not display transfer progress when downloading or uploading" ).build() ); + options.addOption( Option.builder().longOpt( COLOR ).hasArg().desc( "Defines the color mode of the output. Available options are auto/always/never" ).build() ); } public CommandLine parse( String[] args ) diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index 5bdb040539..595f3e7107 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -119,6 +119,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.util.Comparator.comparing; +import static org.apache.maven.cli.CLIManager.COLOR; import static org.apache.maven.cli.ResolveFile.resolveFile; import static org.apache.maven.shared.utils.logging.MessageUtils.buffer; @@ -504,6 +505,7 @@ public class MavenCli // LOG COLOR String styleColor = cliRequest.getUserProperties().getProperty( STYLE_COLOR_PROPERTY, "auto" ); + styleColor = cliRequest.commandLine.getOptionValue( COLOR, styleColor ); if ( "always".equals( styleColor ) ) { MessageUtils.setColorEnabled( true ); diff --git a/maven-embedder/src/test/java/org/apache/maven/cli/CLIManagerDocumentationTest.java b/maven-embedder/src/test/java/org/apache/maven/cli/CLIManagerDocumentationTest.java index bc945fd3d9..a652eda838 100644 --- a/maven-embedder/src/test/java/org/apache/maven/cli/CLIManagerDocumentationTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/cli/CLIManagerDocumentationTest.java @@ -44,7 +44,9 @@ public class CLIManagerDocumentationTest { public int compare( Option opt1, Option opt2 ) { - return opt1.getOpt().compareToIgnoreCase( opt2.getOpt() ); + String s1 = opt1.getOpt() != null ? opt1.getOpt() : opt1.getLongOpt(); + String s2 = opt2.getOpt() != null ? opt2.getOpt() : opt2.getLongOpt(); + return s1.compareToIgnoreCase( s2 ); } }