From 785bad693c60ad60d7b307af8fab9e9234ff57bd Mon Sep 17 00:00:00 2001 From: rfscholte Date: Tue, 15 Aug 2017 21:48:57 +0200 Subject: [PATCH] [MNG-6220] Add CLI options to control color output Introduce -Dstyle.color=[always|never|auto] --- maven-embedder/pom.xml | 5 ++ .../java/org/apache/maven/cli/MavenCli.java | 28 ++++-- .../org/apache/maven/cli/MavenCliTest.java | 86 +++++++++++++++++-- 3 files changed, 107 insertions(+), 12 deletions(-) diff --git a/maven-embedder/pom.xml b/maven-embedder/pom.xml index 7bd265066c..4b3d097ec4 100644 --- a/maven-embedder/pom.xml +++ b/maven-embedder/pom.xml @@ -138,6 +138,11 @@ under the License. org.apache.commons commons-lang3 + + org.fusesource.jansi + jansi + test + 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 641a2a9b8b..3474fabbd5 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 @@ -144,6 +144,8 @@ public class MavenCli private static final String MVN_MAVEN_CONFIG = ".mvn/maven.config"; + public static final String STYLE_COLOR_PROPERTY = "style.color"; + private ClassWorld classWorld; private LoggerManager plexusLoggerManager; @@ -472,8 +474,9 @@ public class MavenCli /** * configure logging */ - private void logging( CliRequest cliRequest ) + void logging( CliRequest cliRequest ) { + // LOG LEVEL cliRequest.debug = cliRequest.commandLine.hasOption( CLIManager.DEBUG ); cliRequest.quiet = !cliRequest.debug && cliRequest.commandLine.hasOption( CLIManager.QUIET ); cliRequest.showErrors = cliRequest.debug || cliRequest.commandLine.hasOption( CLIManager.ERRORS ); @@ -494,18 +497,33 @@ public class MavenCli // 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 ) ) + // LOG COLOR + String styleColor = cliRequest.getUserProperties().getProperty( STYLE_COLOR_PROPERTY, "auto" ); + if ( "always".equals( styleColor ) ) + { + MessageUtils.setColorEnabled( true ); + } + else if ( "never".equals( styleColor ) ) { MessageUtils.setColorEnabled( false ); } - + else if ( !"auto".equals( styleColor ) ) + { + throw new IllegalArgumentException( "Invalid color configuration option [" + styleColor + + "]. Supported values are (auto|always|never)." ); + } + else if ( cliRequest.commandLine.hasOption( CLIManager.BATCH_MODE ) + || cliRequest.commandLine.hasOption( CLIManager.LOG_FILE ) ) + { + MessageUtils.setColorEnabled( false ); + } + + // LOG STREAMS 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 { diff --git a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java index 9b480ea651..433c949962 100644 --- a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java +++ b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java @@ -19,26 +19,35 @@ package org.apache.maven.cli; * under the License. */ -import junit.framework.TestCase; -import org.apache.commons.cli.ParseException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.junit.Assume.assumeTrue; import java.io.File; +import org.apache.commons.cli.ParseException; +import org.apache.maven.shared.utils.logging.MessageUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + public class MavenCliTest - extends TestCase { private MavenCli cli; private String origBasedir; - protected void setUp() + @Before + public void setUp() { cli = new MavenCli(); origBasedir = System.getProperty( MavenCli.MULTIMODULE_PROJECT_DIRECTORY ); } - @Override - protected void tearDown() + @After + public void tearDown() throws Exception { if ( origBasedir != null ) @@ -49,9 +58,9 @@ public class MavenCliTest { System.getProperties().remove( MavenCli.MULTIMODULE_PROJECT_DIRECTORY ); } - super.tearDown(); } + @Test public void testCalculateDegreeOfConcurrencyWithCoreMultiplier() { int cores = Runtime.getRuntime().availableProcessors(); @@ -71,6 +80,7 @@ public class MavenCliTest } } + @Test public void testMavenConfig() throws Exception { @@ -90,6 +100,7 @@ public class MavenCliTest assertEquals( "foobar", request.commandLine.getOptionValue( "builder" ) ); } + @Test public void testMavenConfigInvalid() throws Exception { @@ -120,6 +131,7 @@ public class MavenCliTest * * @throws Exception in case of failure. */ + @Test public void testMVNConfigurationThreadCanBeOverwrittenViaCommandLine() throws Exception { @@ -145,6 +157,7 @@ public class MavenCliTest * * @throws Exception */ + @Test public void testMVNConfigurationDefinedPropertiesCanBeOverwrittenViaCommandLine() throws Exception { @@ -172,6 +185,7 @@ public class MavenCliTest * * @throws Exception */ + @Test public void testMVNConfigurationCLIRepeatedPropertiesLastWins() throws Exception { @@ -199,6 +213,7 @@ public class MavenCliTest * * @throws Exception */ + @Test public void testMVNConfigurationFunkyArguments() throws Exception { @@ -221,4 +236,61 @@ public class MavenCliTest assertEquals( "-Dpom.xml", request.getCommandLine().getOptionValue( CLIManager.ALTERNATE_POM_FILE ) ); } + + @Test + public void testStyleColors() + throws Exception + { + assumeTrue( "ANSI not supported", MessageUtils.isColorEnabled() ); + CliRequest request; + + MessageUtils.setColorEnabled( true ); + request = new CliRequest( new String[] { "-B" }, null ); + cli.cli( request ); + cli.properties( request ); + cli.logging( request ); + assertFalse( MessageUtils.isColorEnabled() ); + + MessageUtils.setColorEnabled( true ); + request = new CliRequest( new String[] { "-l", "target/temp/mvn.log" }, null ); + cli.cli( request ); + cli.properties( request ); + cli.logging( request ); + assertFalse( MessageUtils.isColorEnabled() ); + + MessageUtils.setColorEnabled( false ); + request = new CliRequest( new String[] { "-Dstyle.color=always" }, null ); + cli.cli( request ); + cli.properties( request ); + cli.logging( request ); + assertTrue( MessageUtils.isColorEnabled() ); + + MessageUtils.setColorEnabled( true ); + request = new CliRequest( new String[] { "-Dstyle.color=never" }, null ); + cli.cli( request ); + cli.properties( request ); + cli.logging( request ); + assertFalse( MessageUtils.isColorEnabled() ); + + MessageUtils.setColorEnabled( false ); + request = new CliRequest( new String[] { "-Dstyle.color=always", "-B", "-l", "target/temp/mvn.log" }, null ); + cli.cli( request ); + cli.properties( request ); + cli.logging( request ); + assertTrue( MessageUtils.isColorEnabled() ); + + try + { + MessageUtils.setColorEnabled( false ); + request = new CliRequest( new String[] { "-Dstyle.color=maybe", "-B", "-l", "target/temp/mvn.log" }, null ); + cli.cli( request ); + cli.properties( request ); + cli.logging( request ); + fail( "maybe is not a valid option" ); + } + catch ( IllegalArgumentException e ) + { + // noop + } + } }