Maven does not replace a variable in 'settings.xml' when it is invoked

Property substitution into settings.xml doesn't work as you would expect.

It will substitute properties inside the profiles element (as you've seen it substitutes into your repository url, which would be defined inside a profile), but not to elements outside of profiles (as you've seen happening in the mirrors section). This distinction is made because the profile element in the settings.xml is a truncated version of the pom.xml profile element. It is a mechanism to allow configuration to be set into your POM, so property substitution is allowed within the profiles elements as they are effectively part of the POM.

The parts of the settings outside of the profiles element represent the platform configuration, these aren't supposed to be affected by individual builds, so are not substituted for command-line properties. This makes sense but isn't really made clear anywhere.

EDIT: in the settings page of mavens documentation, in the last sentence of the quick overview section (quite hidden) it states:

Note that properties defined in profiles within the settings.xml cannot be used for interpolation.


There is a workaround though, you can substitute environment variables into the settings.xml. If you set the environment variable:

set M2_MIRROR=D:\test

and configure the repository url as follows:

<url>file://${M2_MIRROR}/maven/.m2/repository</url>

Then invoke Maven as normal, the environment variable is substituted and your build should work as required.


This is an old question now, but as of Maven 3, and probably before, you can refer to environment vars, if you prefix with 'env'

I do so like this:

  <localRepository>${env.M2_LOCAL_REPO}</localRepository>

Then each developer sets M2_LOCAL_REPO to an appropriate location.

Tags:

Maven 2