diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java index 4dc237a4b51..f37f6bd1b41 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java @@ -18,6 +18,9 @@ package org.springframework.boot.autoconfigure.groovy.template; import groovy.text.markup.MarkupTemplateEngine; +import java.security.CodeSource; +import java.security.ProtectionDomain; + import javax.annotation.PostConstruct; import javax.servlet.Servlet; @@ -75,7 +78,7 @@ public class GroovyTemplateAutoConfiguration { @PostConstruct public void checkTemplateLocationExists() { - if (this.properties.isCheckTemplateLocation()) { + if (this.properties.isCheckTemplateLocation() && !isUsingGroovyAllJar()) { Resource resource = this.resourceLoader.getResource(this.properties .getPrefix()); Assert.state(resource.exists(), "Cannot find template location: " @@ -85,6 +88,28 @@ public class GroovyTemplateAutoConfiguration { } } + /** + * MarkupTemplateEngine could be loaded from groovy-templates or groovy-all. + * Unfortunately it's quite common for people to use groovy-all and not actually + * need templating support. This method checks attempts to check the source jar so + * that we can skip the {@code /template} folder check for such cases. + * @return true if the groovy-all jar is used + */ + private boolean isUsingGroovyAllJar() { + try { + ProtectionDomain domain = MarkupTemplateEngine.class + .getProtectionDomain(); + CodeSource codeSource = domain.getCodeSource(); + if (codeSource != null + && codeSource.getLocation().toString().contains("-all")) { + return true; + } + } + catch (Exception ex) { + } + return false; + } + @Bean @ConditionalOnMissingBean(GroovyMarkupConfig.class) @ConfigurationProperties(prefix = "spring.groovy.template.configuration")