diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java index 2cbf0faa85c4..45a659c1bab1 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -348,7 +348,9 @@ private void checkMandatoryLocations(ConfigDataEnvironmentContributors contribut ConfigDataActivationContext activationContext) { Set mandatoryLocations = new LinkedHashSet<>(); for (ConfigDataEnvironmentContributor contributor : contributors) { - mandatoryLocations.addAll(getMandatoryImports(contributor)); + if (contributor.isActive(activationContext)) { + mandatoryLocations.addAll(getMandatoryImports(contributor)); + } } for (ConfigDataEnvironmentContributor contributor : contributors) { if (contributor.getLocation() != null) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java index 966e90ee7e58..b27a920fc3d2 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -186,6 +186,21 @@ void runWhenOneCustomLocationDoesNotExistLoadsOthers() { assertThat(property).isEqualTo("frompropertiesfile"); } + @Test + void runWhenProfileSpecificMandatoryLocationDoesNotExistShouldNotFail() { + ConfigurableApplicationContext context = this.application.run("--spring.config.name=testprofiles", + "--spring.config.location=classpath:configdata/profiles/"); + String property = context.getEnvironment().getProperty("my.property"); + assertThat(property).isEqualTo("fromyamlfile"); + } + + @Test + void runWhenProfileSpecificMandatoryLocationDoesNotExistShouldFailWhenProfileActive() { + this.application.setAdditionalProfiles("prod"); + assertThatExceptionOfType(ConfigDataResourceNotFoundException.class).isThrownBy(() -> this.application + .run("--spring.config.name=testprofiles", "--spring.config.location=classpath:configdata/profiles/")); + } + @Test void runWhenHasActiveProfilesFromMultipleLocationsActivatesProfileFromOneLocation() { ConfigurableApplicationContext context = this.application diff --git a/spring-boot-project/spring-boot/src/test/resources/configdata/profiles/testprofiles.yml b/spring-boot-project/spring-boot/src/test/resources/configdata/profiles/testprofiles.yml index 22850cf3aecc..98a993ef7659 100644 --- a/spring-boot-project/spring-boot/src/test/resources/configdata/profiles/testprofiles.yml +++ b/spring-boot-project/spring-boot/src/test/resources/configdata/profiles/testprofiles.yml @@ -12,4 +12,7 @@ dev: spring.config.activate.on-profile: other my: property: fromotherprofile +--- +spring.config.activate.on-profile: prod +spring.config.import: file:./non-existent.yml