Repository: quarkusio/quarkus
Issue: ENVs required in prod profile break dev profile but only when using the built artifact

**Describe the bug**

Quarkus tries to resolve ENVs for profiles which are not active. This breaks other profiles. This only happens with the built jar. It works fine with the `quarkus:dev` plugin.

Example:

Empty Quarkus project with `%prod.quarkus.http.port=${HTTP_PORT}` as the only configuration in `application.properties` breaks every other profile when running from jar if `HTTP_PORT` is not exported. 

**Expected behavior**

If `HTTP_PORT` is not set application should start fine with any profile but `prod`. `HTTP_PORT` is only required in `prod`.

**Actual behavior**

Quarkus tries to resolve `HTTP_PORT` regardless of the active profile and fails to start.

```
QUARKUS_PROFILE=foo java -jar target/quarkus-app/quarkus-run.jar
One or more configuration errors have prevented the application from starting. The errors are:
  - SRCFG00011: Could not expand value HTTP_PORT in property quarkus.http.port
```


**To Reproduce**

https://github.com/atamanroman/quarkus-profile-dev-prod-repro

Steps to reproduce here:

https://github.com/atamanroman/quarkus-profile-dev-prod-repro/blob/main/README.md

**Additional context**

This issue was opened after discussion in [this zulip thread](https://quarkusio.zulipchat.com/#narrow/stream/187030-users/topic/ENVs.20required.20in.20prod.20profile.20break.20dev).


Changed files:
core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java
extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/ConfigDefaultValues.java

diff --git a/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java b/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java
index 109885691ee..48572a88c31 100644
--- a/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java
+++ b/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java
@@ -296,7 +296,7 @@ ReadResult run() {
                 nameBuilder.setLength(len);
             }
             // sweep-up
-            for (String propertyName : config.getPropertyNames()) {
+            for (String propertyName : getAllProperties()) {
                 if (propertyName.equals(ConfigSource.CONFIG_ORDINAL)) {
                     continue;
                 }
@@ -718,6 +718,19 @@ private Converter<?> getConverter(SmallRyeConfig config, Field field, ConverterT
             convByType.put(valueType, converter);
             return converter;
         }
+
+        /**
+         * We collect all properties from ConfigSources, because Config#getPropertyNames exclude the active profiled
+         * properties, meaning that the property is written in the default config source unprofiled. This may cause
+         * issues if we run with a different profile and fallback to defaults.
+         */
+        private Set<String> getAllProperties() {
+            Set<String> properties = new HashSet<>();
+            for (ConfigSource configSource : config.getConfigSources()) {
+                properties.addAll(configSource.getPropertyNames());
+            }
+            return properties;
+        }
     }
 
     public static final class ReadResult {
diff --git a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/ConfigDefaultValues.java b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/ConfigDefaultValuesTest.java
similarity index 63%
rename from extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/ConfigDefaultValues.java
rename to extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/ConfigDefaultValuesTest.java
index 9de4efe466e..b37b7083c23 100644
--- a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/ConfigDefaultValues.java
+++ b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/config/ConfigDefaultValuesTest.java
@@ -15,12 +15,17 @@
 
 import io.quarkus.test.QuarkusUnitTest;
 
-public class ConfigDefaultValues {
+public class ConfigDefaultValuesTest {
     @RegisterExtension
     static final QuarkusUnitTest TEST = new QuarkusUnitTest()
             .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
-                    .addAsResource(new StringAsset("config_ordinal=1000\\n" +
-                            "my.prop=1234\\n"), "application.properties"));
+                    .addAsResource(new StringAsset(
+                            "config_ordinal=1000\\n" +
+                                    "my.prop=1234\\n" +
+                                    "%prod.my.prop=1234\\n" +
+                                    "%dev.my.prop=5678\\n" +
+                                    "%test.my.prop=1234"),
+                            "application.properties"));
     @Inject
     Config config;
 
@@ -38,6 +43,17 @@ void configDefaultValues() {
         assertEquals("1234", applicationProperties.getValue("my.prop"));
     }
 
+    @Test
+    void profileDefaultValues() {
+        ConfigSource defaultValues = getConfigSourceByName("PropertiesConfigSource[source=Specified default values]");
+        assertNotNull(defaultValues);
+        assertEquals("1234", defaultValues.getValue("my.prop"));
+        assertEquals("1234", defaultValues.getValue("%prod.my.prop"));
+        assertEquals("5678", defaultValues.getValue("%dev.my.prop"));
+        assertEquals("1234", defaultValues.getValue("%test.my.prop"));
+        assertEquals("1234", config.getValue("my.prop", String.class));
+    }
+
     private ConfigSource getConfigSourceByName(String name) {
         for (ConfigSource configSource : config.getConfigSources()) {
             if (configSource.getName().contains(name)) {