Search code examples
javamicronaut

How do I assign a property source to a certain environment in micronaut?


I am adding property values through my Application class like so

public class Application {

    @Loggable
    public static void main(String[] args) {

        SecretManager secretManager = new SecretManager();

        Micronaut.build(null)
                .mainClass(Application.class)
                .propertySources(PropertySource.of(
                        "name",
                        mapOf(
                                "datasources.default.username", secretManager.getValue(
                                        "DATASOURCES_DEFAULT_USERNAME")
                                ))).start();
    }
}

I would like to be able to change the value for datasources.default.username according to the environment by doing something like this. The following code does not work but is there a way to do something like this?

public class Application {

    @Loggable
    public static void main(String[] args) {

        SecretManager secretManager = new SecretManager();

        if(environment == "Dev") {
            Micronaut.build(null)
                    .mainClass(Application.class)
                    .propertySources(PropertySource.of(
                            "name",
                            mapOf(
                                    "datasources.default.username", secretManager.getValue(
                                            "DATASOURCES_DEFAULT_USERNAME")
                            ))).start();
        } else {
            Micronaut.build(null)
                    .mainClass(Application.class)
                    .propertySources(PropertySource.of(
                            "name",
                            mapOf(
                                    "datasources.default.username", secretManager.getValue(
                                            "DATASOURCES_CUSTOM_USERNAME")
                            ))).start();
        }
    }
}


Is there any way to get this done?


Solution

  • You can create an application-dev.yml that assigns the property from the env var

    datasources.default.username: ${SOME_ENV} for example