Search code examples
mavenenvironment-variablesmaven-plugin

Maven and env vars from file


I switched to maven and have a question. I use env file with vars for development, which is loaded to intellij idea via envfile. For deployment I use docker with another env file for container deployment. I switch these files via maven profiles. But the problem is, that I cannot load these settings to maven, mostly for testing, for example for local build or some automated deployment. My question - is there a way, like plugin or something, to load env file from maven? I made a makefile, but don't think it's a good idea really. I found apache surefire but looks like it cannot do what i need, or i simply didn't realise how to configure.

Thank you


Solution

  • You can use the properties-maven-plugin.

    With this plugin you can define a default config file and multiple override files that you can choose from. For example its configuration could be

    <configuration>
        <quiet>true</quiet>
        <files>
            <file>env.properties</file>
            <file>env_${user.name}.properties</file>
        </files>
    </configuration>
    

    In this way the overrride files could be, for example:

    1. env_myUserName.properties
    2. env_production.properties
    3. env_jboss.properties

    The first one, if present, will be used automatically assuming that myUserName is your OS user (per-developer configuration), the other ones can be enabled from the command line with the well known -Duser.name= parameter. For example:

    mvn clean install -Duser.name=production