Search code examples
javaconfigurationmicronaut

Micronaut PropertySource for multiple configuration files


I have a micronaut project where I want to have an unnversioned configuration file for private data (like database connections and so on)

This information have to be loaded through @Property annotation, but since there will be more than one .yml (there will also be at least an application.yml) y want to be able to provide file's path to @Properties to be able to differentiate where to look for property.

Since it's my first micronaut project I'm a bit lost with this stuff but taking springboot as an example, what I want to do is something like:

@PropertySource("classpath:configprops.properties")

But after reading micronaut documentation(https://docs.micronaut.io/latest/guide/index.html#configurationProperties) I found myself unable to do this (except from something like just reading the plain file which I guess would not be micronaut compliant)


Solution

  • I do it by passing jvm arguments.

    For example, If I am running it on my local machine using gradle:run, I add following to build.grade

    run.jvmArgs('-Dmicronaut.environments=dev', "-Dmicronaut.config.files=${System.getProperty("user.home")}/auth-config.groovy")
    

    For my jar deployment, I have made a deploy.sh file as follows :

    #!/bin/bash
    
    fuser -k 8181/tcp
    
    nohup java -Xmx512m -Dmicronaut.environments=staging -Dmicronaut.config.files=<path-to-config>/config.groovy -jar application-0.1-all.jar > application.log 2>&1 &
    

    Also note that I am passing different environment names, this helps you to include development environment config directly in code if you want.

    Like

    • application-[environment_name].groovy
    • application-[environment_name].yml
    • application-[environment_name].properties

    This will help new contributors on your project to speedup the process project setup, I generally also include note in my application-dev.groovy file

    DEVELOPER NOTE:
    ***** DO NOT COMMIT ANY CHANGE IN THIS FILE IF YOU MAKE ANY
    *******************************************************
    ***** CREATE <config.groovy> file in your <HOME> folder and copy paste content of this file
    ***** Override properties as required
    *******************************************************