Search code examples
mavenenvironment-variablesdefault-valuesetter

How to set default values for missing environment variables in maven pom


Is it possible to have maven provide default values for missing environment variables? If so, how?


Context:

I have properties files with placeholders to environment variables, like this: my.property=${env:ENVIRONMENT_PROPERTY}

Running maven (edit: not like this mvn ... -DENVIRONMENT_PROPERTY=some_value) if the environment variable is set in the OS makes it resolve the property placeholder with the given value. But if the environment variable does not exist, the value is blank.

Problem:

I would like the environment variables to have a default value. If the environment variable does not exist it should be given a default value that would be specified in the pom or some properties file or whatever.

Important:

The property placeholders have to point to an environment variable.


Solution

  • I used the felix fileinstall plugin (org.apache.felix.fileinstall) This allows the following notation:

    ${env:ENVIRONMENT_PROPERTY:-default value}

    This will take the value from the ENVIRONMENT_PROPERTY and if that is not set, it would take "default value".


    EDIT:
    If you want to take the default values from maven properties, you need to use the ${dollar} hack:

    ${dollar}{env:ENVIRONMENT_PROPERTY:-${default.value}}

    where dollar is a maven property that is resolved to a dollar symbol (<dollar>$</dollar>) which turns into a delimiter when combined with the { bracket.