Search code examples
mavenenvironment-variablesmaven-3maven-resources-plugin

Override a Maven filter value with an environment variable or command-line parameter


We use Maven to build our java projects, with different profiles (testing, staging, production etc). Each profile has a its own filter, declared like :

<filters>
    <filter>src/main/filters/filter-${env}.properties</filter>
</filters>

(with ${env} being set to the profile name). Each filter contains the values specific to the target environment (mostly configuration parameters).

It's been working great for many years. But, sometimes, we would like to override one of the values inside this .properties filter file, without modifying the file itself.

Why? Because it happens very late in the process. We already tagged a version on the SCM, carefully tested it in testing/staging environments, communicated on this version number, and are discovering a wrong parameter value in the production filter when installing in production. It's too late to create a new tag and test again, only for a configuration parameter.

I'd like to find a way to override the value at Maven runtime : either with an environment variable, or by passing some parameters on the Maven command-line.

I suppose it should be possible by extending the maven-resources-plugin ourself, but there might be an easier way?

NB : our build does not only generate a war, but a complete package including its configuration.

Technical environment : Maven 3.3.9, launched by a Jenkins job


Solution

  • when executing a maven command, you can override your property by providing it as a system-property.

    like mvn install "-Dthe.property=the overriden value"

    note the " are only needed if the value contains spaces.