Search code examples
javaconfigurationquarkusapplication.properties

Quarkus does not use profile-related variables


These two links:

specify how to use different profiles in Quarkus applications. I tried both variants for my testing environment and I basically failed at both of them.

1st variant (custom profiles)

I have the main configuration file src/main/resources/application.properties which contains all main config variables but each of them has an env variable assigned as a value.

I have my test config variables also in src/main/resources/application.properties. Their names are the same as for the base config variables just prefixed with %test..

When I do not have any environment variable set related to any config variable, then mvn -Dquarkus.test.profile=test clean verify works as expected - the values from the config variables prefixed with %test. are used during the tests.

However, as soon as I specify any env variable which is used as a value for some config property, the test value for this config property is not used anymore but the value from the variable. This breaks the expected behavior also in my CI/CD pipelines.

2nd variant (profile aware files)

I have a main configuration file src/main/resources/application.properties and also I have a test profile file src/main/resources/application-test.properties. I have the same scenario with variables as in the 1st variant. Meaning that in the main config file I have for example my.property=${MY_PROPERTY} and in the test profile config I have my.property=dummyValue.

If I do not set env variable MY_PROPERTY, then the dummyValue value is used for my.property during runtime. But when I set the env variable MY_PROPERTY, then the dummyValue is completely ignored when I run mvn -Dquarkus.test.profile=test clean verify. Therefore this breaks my testing environment again.


Solution

  • Exactly as @JavierToja mentioned in his comment - the environment variables have higher priority than properties.

    Therefore for testing purposes I did not create a test profile via properties like %test.my.property=myValue nor by creating application-test.properties as the values from env variables in the non-test profile get preferred and I would not achieve the desired behavior this way.

    In the end I solved my problem with preferring test profile over the environment variables in the "main" profile (application.properties) by implementing QuarkusTestProfile class [1]. I overridden the getConfigOverrides method in such way that it returns a map of key value pairs where the key is the property name that I want to override and the value is the value for this property that I want to prefer to a value from an environment variable.

    [1] https://quarkus.io/guides/getting-started-testing#testing_different_profiles