diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index 0828e3461e4..ab359e9585f 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -698,6 +698,21 @@ If you want to support your own locations, see the `ConfigDataLocationResolver` +==== Importing Extensionless Files +Some cloud platforms cannot add a file extension to volume mounted files. +To import these extensionless files, you need to give Spring Boot a hint so that it knows how to load them. +You can do this by putting an extension hint in square brackets. + +For example, suppose you have a `/etc/config/myconfig` file that you wish to import as yaml. +You can import it from your `application.properties` using the following: + +[source,properties,indent=0] +---- + spring.config.import=file:/etc/config/myconfig[.yaml] +---- + + + [[boot-features-external-config-files-configtree]] ==== Using Configuration Trees When running applications on a cloud platform (such as Kubernetes) you often need to read config values that the platform supplies. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ResourceConfigDataLocationResolver.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ResourceConfigDataLocationResolver.java index 7d84050d49b..763dcee10e7 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ResourceConfigDataLocationResolver.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ResourceConfigDataLocationResolver.java @@ -25,6 +25,7 @@ import java.util.Comparator; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.logging.Log; @@ -63,6 +64,8 @@ class ResourceConfigDataLocationResolver implements ConfigDataLocationResolver getResolvablesForFile(String fileLocation, boolean optional, String profile) { + Matcher extensionHintMatcher = EXTENSION_HINT_PATTERN.matcher(fileLocation); + boolean extensionHintLocation = extensionHintMatcher.matches(); + if (extensionHintLocation) { + fileLocation = extensionHintMatcher.group(1) + extensionHintMatcher.group(2); + } for (PropertySourceLoader loader : this.propertySourceLoaders) { String extension = getLoadableFileExtension(loader, fileLocation); if (extension != null) { String root = fileLocation.substring(0, fileLocation.length() - extension.length() - 1); - return Collections.singleton(new Resolvable(null, root, optional, profile, extension, loader)); + return Collections.singleton(new Resolvable(null, root, optional, profile, + (!extensionHintLocation) ? extension : null, loader)); } } throw new IllegalStateException("File extension is not known to any PropertySourceLoader. " @@ -343,7 +352,7 @@ class ResourceConfigDataLocationResolver implements ConfigDataLocationResolver assertThat(ex.getCause()).hasMessageStartingWith("File extension is not known")); } + @Test + void resolveWhenLocationUsesOptionalExtensionSyntaxResolves() throws Exception { + String location = "classpath:/application-props-no-extension[.properties]"; + List locations = this.resolver.resolve(this.context, location, true); + assertThat(locations.size()).isEqualTo(1); + ResourceConfigDataLocation resolved = locations.get(0); + assertThat(resolved.getResource().getFilename()).endsWith("application-props-no-extension"); + PropertySource propertySource = resolved.load().get(0); + assertThat(propertySource.getProperty("withnotext")).isEqualTo("test"); + } + @Test void resolveProfileSpecificReturnsProfileSpecificFiles() { String location = "classpath:/configdata/properties/"; diff --git a/spring-boot-project/spring-boot/src/test/resources/application-props-no-extension b/spring-boot-project/spring-boot/src/test/resources/application-props-no-extension new file mode 100644 index 00000000000..b3efe414930 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/application-props-no-extension @@ -0,0 +1 @@ +withnotext=test \ No newline at end of file