Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNG-6220 add color CLI option #114

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public class CLIManager

public static final String BUILDER = "b";

public static final String COLOR = "co";

protected Options options;

@SuppressWarnings( { "static-access", "checkstyle:linelength" } )
Expand Down Expand Up @@ -140,6 +142,7 @@ public CLIManager()
options.addOption( OptionBuilder.withLongOpt( "threads" ).hasArg().withDescription( "Thread count, for instance 2.0C where C is core multiplied" ).create( THREADS ) );
options.addOption( OptionBuilder.withLongOpt( "legacy-local-repository" ).withDescription( "Use Maven 2 Legacy Local Repository behaviour, ie no use of _remote.repositories. Can also be activated by using -Dmaven.legacyLocalRepo=true" ).create( LEGACY_LOCAL_REPOSITORY ) );
options.addOption( OptionBuilder.withLongOpt( "builder" ).hasArg().withDescription( "The id of the build strategy to use" ).create( BUILDER ) );
options.addOption( OptionBuilder.withLongOpt( "color" ).hasArg().withDescription( "Color output using ANSI escape codes (auto|always|never)" ).hasArg().create( COLOR ) );

// Adding this back in for compatibility with the verifier that hard codes this option.
options.addOption( OptionBuilder.withLongOpt( "no-plugin-registry" ).withDescription( "Ineffective, only kept for backward compatibility" ).create( "npr" ) );
Expand Down
51 changes: 45 additions & 6 deletions maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ public static int main( String[] args, ClassWorld classWorld )
{
MavenCli cli = new MavenCli();

System.setProperty( "jansi.force", "true" );

prepareJansiNative();
MessageUtils.systemInstall();
int result = cli.doMain( new CliRequest( args, classWorld ) );
Expand Down Expand Up @@ -532,6 +534,7 @@ private CommandLine cliMerge( CommandLine mavenArgs, CommandLine mavenConfig )
* configure logging
*/
private void logging( CliRequest cliRequest )
throws Exception
{
cliRequest.debug = cliRequest.commandLine.hasOption( CLIManager.DEBUG );
cliRequest.quiet = !cliRequest.debug && cliRequest.commandLine.hasOption( CLIManager.QUIET );
Expand All @@ -553,18 +556,13 @@ else if ( cliRequest.quiet )
// else fall back to default log level specified in conf
// see https://issues.apache.org/jira/browse/MNG-2570

if ( cliRequest.commandLine.hasOption( CLIManager.BATCH_MODE ) )
{
MessageUtils.setColorEnabled( false );
}
configureColor( cliRequest );

if ( cliRequest.commandLine.hasOption( CLIManager.LOG_FILE ) )
{
File logFile = new File( cliRequest.commandLine.getOptionValue( CLIManager.LOG_FILE ) );
logFile = resolveFile( logFile, cliRequest.workingDirectory );

MessageUtils.setColorEnabled( false );

// redirect stdout and stderr to file
try
{
Expand All @@ -586,6 +584,47 @@ else if ( cliRequest.quiet )
slf4jLogger = slf4jLoggerFactory.getLogger( this.getClass().getName() );
}

private void configureColor( CliRequest cliRequest )
throws Exception
{
if ( cliRequest.commandLine.hasOption( CLIManager.COLOR ) )
{
String color = cliRequest.commandLine.getOptionValue( CLIManager.COLOR );

if ( "always".equals( color ) )
{
return;
}

if ( "never".equals( color ) )
{
MessageUtils.setColorEnabled( false );
return;
}

if ( !"auto".equals( color ) )
{
throw new Exception( "Invalid color configuration option [" + color
+ "]. Supported values are (auto|always|never)." );
}

}

if ( cliRequest.commandLine.hasOption( CLIManager.BATCH_MODE ) )
{
MessageUtils.setColorEnabled( false );
return;
}

if ( cliRequest.commandLine.hasOption( CLIManager.LOG_FILE ) )
{
MessageUtils.setColorEnabled( false );
return;
}

MessageUtils.autoDetectColorSupport();
}

private void version( CliRequest cliRequest )
{
if ( cliRequest.debug || cliRequest.commandLine.hasOption( CLIManager.SHOW_VERSION ) )
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ under the License.
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-shared-utils</artifactId>
<version>3.1.0</version>
<version>3.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
Expand Down