diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/BootRunTask.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/BootRunTask.java index 927b06ca759..e598f08d34f 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/BootRunTask.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/BootRunTask.java @@ -54,6 +54,10 @@ public class BootRunTask extends JavaExec { @Override public void exec() { + if(System.console() != null) { + // Record that the console is available here for AnsiOutput to detect later + this.getEnvironment().put("spring.output.ansi.console-available", true); + } addResourcesIfNecessary(); super.exec(); } diff --git a/spring-boot/src/main/java/org/springframework/boot/ansi/AnsiOutput.java b/spring-boot/src/main/java/org/springframework/boot/ansi/AnsiOutput.java index b83cec2d7e4..a38eb208a2f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/ansi/AnsiOutput.java +++ b/spring-boot/src/main/java/org/springframework/boot/ansi/AnsiOutput.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,8 @@ public abstract class AnsiOutput { private static Enabled enabled = Enabled.DETECT; + private static Boolean consoleAvailable; + private static final String OPERATING_SYSTEM_NAME = System.getProperty("os.name") .toLowerCase(); @@ -48,6 +50,15 @@ public abstract class AnsiOutput { AnsiOutput.enabled = enabled; } + /** + * Sets if the System.console() is know to be available. + * @param consoleAvailable if the console is known to be available or {@code null} to + * use standard detection logic. + */ + public static void setConsoleAvailable(Boolean consoleAvailable) { + AnsiOutput.consoleAvailable = consoleAvailable; + } + static Enabled getEnabled() { return AnsiOutput.enabled; } @@ -115,7 +126,10 @@ public abstract class AnsiOutput { private static boolean detectIfEnabled() { try { - if (System.console() == null) { + if (Boolean.FALSE.equals(consoleAvailable)) { + return false; + } + if ((consoleAvailable == null) && (System.console() == null)) { return false; } return !(OPERATING_SYSTEM_NAME.indexOf("win") >= 0); diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/AnsiOutputApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/config/AnsiOutputApplicationListener.java index be7ede154c7..09614f6aed7 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/AnsiOutputApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/AnsiOutputApplicationListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,11 @@ public class AnsiOutputApplicationListener implements String enabled = resolver.getProperty("enabled"); AnsiOutput.setEnabled(Enum.valueOf(Enabled.class, enabled.toUpperCase())); } + + if (resolver.containsProperty("console-available")) { + AnsiOutput.setConsoleAvailable(resolver.getProperty("console-available", + Boolean.class)); + } } @Override diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/config/AnsiOutputApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/config/AnsiOutputApplicationListenerTests.java index 8967379b925..1fb0962c37c 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/config/AnsiOutputApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/config/AnsiOutputApplicationListenerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -71,7 +71,7 @@ public class AnsiOutputApplicationListenerTests { } @Test - public void disabledViaApplcationProperties() throws Exception { + public void disabledViaApplicationProperties() throws Exception { ConfigurableEnvironment environment = new StandardEnvironment(); EnvironmentTestUtils.addEnvironment(environment, "spring.config.name:ansi"); SpringApplication application = new SpringApplication(Config.class);