[MNG-7080] Add a --color option to simplify color support

This commit is contained in:
Guillaume Nodet 2021-01-21 18:22:18 +01:00 committed by Martin Kanters
parent 5d6c6e2537
commit 59c5466c40
3 changed files with 8 additions and 1 deletions

View File

@ -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 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 )

View File

@ -119,6 +119,7 @@
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 @@ else if ( cliRequest.quiet )
// LOG COLOR
String styleColor = cliRequest.getUserProperties().getProperty( STYLE_COLOR_PROPERTY, "auto" );
styleColor = cliRequest.commandLine.getOptionValue( COLOR, styleColor );
if ( "always".equals( styleColor ) )
{
MessageUtils.setColorEnabled( true );

View File

@ -44,7 +44,9 @@ private static class OptionComparator
{
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 );
}
}