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 7d17e187d2..9c7c4d2537 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 @@ -166,6 +166,8 @@ public class MavenCli private Map configurationProcessors; + private CLIManager cliManager; + public MavenCli() { this( null ); @@ -278,6 +280,7 @@ public class MavenCli cli( cliRequest ); properties( cliRequest ); logging( cliRequest ); + informativeCommands( cliRequest ); version( cliRequest ); localContainer = container( cliRequest ); commands( cliRequest ); @@ -367,7 +370,7 @@ public class MavenCli // slf4jLogger = new Slf4jStdoutLogger(); - CLIManager cliManager = new CLIManager(); + cliManager = new CLIManager(); List args = new ArrayList<>(); CommandLine mavenConfig = null; @@ -417,7 +420,10 @@ public class MavenCli cliManager.displayHelp( System.out ); throw e; } + } + private void informativeCommands( CliRequest cliRequest ) throws ExitException + { if ( cliRequest.commandLine.hasOption( CLIManager.HELP ) ) { cliManager.displayHelp( System.out ); 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 5f4c5b69a7..9889fbcb0f 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 @@ -29,7 +29,12 @@ import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.List; import org.apache.commons.cli.ParseException; import org.apache.maven.Maven; @@ -331,4 +336,36 @@ public class MavenCliTest orderdEventSpyDispatcherMock.verify(eventSpyDispatcherMock, times(1)).onEvent(any(ToolchainsBuildingResult.class)); } + /** + * MNG-7032: Disable colours for {@code --version} if {@code --batch-mode} is also given. + * @throws Exception cli invocation. + */ + @Test + public void testVersionStringWithoutAnsi() throws Exception + { + // given + // - request with version and batch mode + CliRequest cliRequest = new CliRequest( new String[] { + "--version", + "--batch-mode" + }, null ); + ByteArrayOutputStream systemOut = new ByteArrayOutputStream(); + PrintStream oldOut = System.out; + System.setOut( new PrintStream( systemOut ) ); + + // when + try { + cli.cli( cliRequest ); + } catch ( MavenCli.ExitException exitException ) { + // expected + } finally { + // restore sysout + System.setOut( oldOut ); + } + String versionOut = new String( systemOut.toByteArray(), StandardCharsets.UTF_8 ); + + // then + assertEquals( MessageUtils.stripAnsiCodes( versionOut ), versionOut ); + } + }