Commit e3b352e0 authored by Phillip Webb's avatar Phillip Webb

Support 'headless' applications

Update SpringApplication to run by default in 'headless' mode. This
prevents the AWT system from creating a Java icon (for example in the
OSX dock).

Also update builds to run tests in 'headless' mode.
parent 0e413c3b
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -90,6 +90,7 @@ public class SpringCli { ...@@ -90,6 +90,7 @@ public class SpringCli {
* @return a return status code (non boot is used to indicate an error) * @return a return status code (non boot is used to indicate an error)
*/ */
public int runAndHandleErrors(String... args) { public int runAndHandleErrors(String... args) {
System.setProperty("java.awt.headless", Boolean.toString(true));
String[] argsWithoutDebugFlags = removeDebugFlags(args); String[] argsWithoutDebugFlags = removeDebugFlags(args);
boolean debug = argsWithoutDebugFlags.length != args.length; boolean debug = argsWithoutDebugFlags.length != args.length;
try { try {
......
...@@ -359,6 +359,7 @@ ...@@ -359,6 +359,7 @@
</excludes> </excludes>
<systemPropertyVariables> <systemPropertyVariables>
<java.security.egd>file:/dev/./urandom</java.security.egd> <java.security.egd>file:/dev/./urandom</java.security.egd>
<java.awt.headless>true</java.awt.headless>
</systemPropertyVariables> </systemPropertyVariables>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine> <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
</configuration> </configuration>
......
...@@ -72,6 +72,21 @@ ...@@ -72,6 +72,21 @@
</additionalConfig> </additionalConfig>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
<systemPropertyVariables>
<java.security.egd>file:/dev/./urandom</java.security.egd>
<java.awt.headless>true</java.awt.headless>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
<repositories> <repositories>
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -177,6 +177,8 @@ public class SpringApplication { ...@@ -177,6 +177,8 @@ public class SpringApplication {
private boolean webEnvironment; private boolean webEnvironment;
private boolean headless = true;
private Set<ApplicationContextInitializer<?>> initializers; private Set<ApplicationContextInitializer<?>> initializers;
private Set<ApplicationListener<?>> listeners; private Set<ApplicationListener<?>> listeners;
...@@ -305,6 +307,8 @@ public class SpringApplication { ...@@ -305,6 +307,8 @@ public class SpringApplication {
stopWatch.start(); stopWatch.start();
ConfigurableApplicationContext context = null; ConfigurableApplicationContext context = null;
System.setProperty("java.awt.headless", Boolean.toString(this.headless));
ApplicationEventMulticaster multicaster = createApplicationEventMulticaster(); ApplicationEventMulticaster multicaster = createApplicationEventMulticaster();
try { try {
Set<Object> sources = getSources(); Set<Object> sources = getSources();
...@@ -673,6 +677,15 @@ public class SpringApplication { ...@@ -673,6 +677,15 @@ public class SpringApplication {
this.webEnvironment = webEnvironment; 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 * Sets if the Spring banner should be displayed when the application runs. Defaults
* to {@code true}. * to {@code true}.
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.builder; package org.springframework.boot.builder;
import java.util.Arrays; import java.util.Arrays;
...@@ -59,7 +60,6 @@ import org.springframework.core.io.ResourceLoader; ...@@ -59,7 +60,6 @@ import org.springframework.core.io.ResourceLoader;
* SpringApplication instead. * SpringApplication instead.
* *
* @author Dave Syer * @author Dave Syer
*
*/ */
public class SpringApplicationBuilder { public class SpringApplicationBuilder {
...@@ -307,6 +307,17 @@ public class SpringApplicationBuilder { ...@@ -307,6 +307,17 @@ public class SpringApplicationBuilder {
return this; 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. * Fixes the main application class that is used to anchor the startup messages.
* *
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -398,6 +398,23 @@ public class SpringApplicationTests { ...@@ -398,6 +398,23 @@ public class SpringApplicationTests {
verify(applicationContext.getApplicationContext()).registerShutdownHook(); 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, private boolean hasPropertySource(ConfigurableEnvironment environment,
Class<?> propertySourceClass, String name) { Class<?> propertySourceClass, String name) {
for (PropertySource<?> source : environment.getPropertySources()) { for (PropertySource<?> source : environment.getPropertySources()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment