diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java index f1a5a4fc8a..0e7374063d 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -90,6 +90,7 @@ public class SpringCli { * @return a return status code (non boot is used to indicate an error) */ public int runAndHandleErrors(String... args) { + System.setProperty("java.awt.headless", Boolean.toString(true)); String[] argsWithoutDebugFlags = removeDebugFlags(args); boolean debug = argsWithoutDebugFlags.length != args.length; try { diff --git a/spring-boot-parent/pom.xml b/spring-boot-parent/pom.xml index ef6f3f6969..18f6e40b86 100644 --- a/spring-boot-parent/pom.xml +++ b/spring-boot-parent/pom.xml @@ -359,6 +359,7 @@ file:/dev/./urandom + true -Xmx1024m -XX:MaxPermSize=256m diff --git a/spring-boot-samples/pom.xml b/spring-boot-samples/pom.xml index a10fd482fe..5c7fd07c4d 100644 --- a/spring-boot-samples/pom.xml +++ b/spring-boot-samples/pom.xml @@ -72,6 +72,21 @@ + + maven-surefire-plugin + + + **/*Tests.java + + + **/Abstract*.java + + + file:/dev/./urandom + true + + + diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 42e3b3c8b7..6e6c285d50 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -177,6 +177,8 @@ public class SpringApplication { private boolean webEnvironment; + private boolean headless = true; + private Set> initializers; private Set> listeners; @@ -305,6 +307,8 @@ public class SpringApplication { stopWatch.start(); ConfigurableApplicationContext context = null; + System.setProperty("java.awt.headless", Boolean.toString(this.headless)); + ApplicationEventMulticaster multicaster = createApplicationEventMulticaster(); try { Set sources = getSources(); @@ -673,6 +677,15 @@ public class SpringApplication { this.webEnvironment = webEnvironment; } + /** + * Sets if the application is headless and should not instantiate AWT. Defaults to + * {@code true} to prevent java icons appearing. + * @param headless if the application is headless + */ + public void setHeadless(boolean headless) { + this.headless = headless; + } + /** * Sets if the Spring banner should be displayed when the application runs. Defaults * to {@code true}. diff --git a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java index dbd43be376..52417ccdb3 100644 --- a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java +++ b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.boot.builder; import java.util.Arrays; @@ -59,7 +60,6 @@ import org.springframework.core.io.ResourceLoader; * SpringApplication instead. * * @author Dave Syer - * */ public class SpringApplicationBuilder { @@ -307,6 +307,17 @@ public class SpringApplicationBuilder { return this; } + /** + * Sets if the application is headless and should not instantiate AWT. Defaults to + * {@code true} to prevent java icons appearing. + * @param headless if the application is headless + * @return the current builder + */ + public SpringApplicationBuilder headless(boolean headless) { + this.application.setHeadless(headless); + return this; + } + /** * Fixes the main application class that is used to anchor the startup messages. * diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 0805c09138..56562aa019 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -398,6 +398,23 @@ public class SpringApplicationTests { verify(applicationContext.getApplicationContext()).registerShutdownHook(); } + @Test + public void headless() throws Exception { + TestSpringApplication application = new TestSpringApplication(ExampleConfig.class); + application.setWebEnvironment(false); + application.run(); + assertThat(System.getProperty("java.awt.headless"), equalTo("true")); + } + + @Test + public void headlessFalse() throws Exception { + TestSpringApplication application = new TestSpringApplication(ExampleConfig.class); + application.setWebEnvironment(false); + application.setHeadless(false); + application.run(); + assertThat(System.getProperty("java.awt.headless"), equalTo("false")); + } + private boolean hasPropertySource(ConfigurableEnvironment environment, Class propertySourceClass, String name) { for (PropertySource source : environment.getPropertySources()) {