tags:

views:

772

answers:

3

I have defined a local mirror for all repositories in the settings.xml file:

<mirror>
  <id>myMirror</id>
  <mirrorOf>*</mirrorOf>
  <url>file://${mypath}/maven/.m2/repository</url>
</mirror>

I want that my mirror to point to a local path, in this case the path is:

file://${mypath}/maven/.m2/repository

Where ${mypath} is a variable that I pass when I invoke Maven:

 mvn -Dmypath="/D:/test" package

The problem is that Maven does not replace the variable when it is invoked. I can see that this error is happening by inspection of the build log. For example, Maven reports that it is downloading a file from file://${mypath}/maven/.m2/repository when the correct would be file:///D:/test/maven/.m2/repository.

I have also noted that Maven replaces correctly my variable when it is inserted in the url child tag of the repository tag:

<repository>
        <id>central</id>
        <url>http://${mypath}/maven/.m2/repository&lt;/url&gt;
</repository>

The build works correctly when I replace the variable in my settings.xml by the complete URL like in the example below:

<mirror>
        <id>myMirror</id>
        <mirrorOf>*</mirrorOf>
        <url>file:///D:test/maven/.m2/repository</url>
</mirror>
A: 

It is probably a bug - unfortunately property replacement does not seem to be consistent across Maven plugins. I have encountered a bug myself around specifying more than two properties in a configuration item in another plugin.

Nosrama
A: 

The settings.xml is not interpolated like the pom is, so the property can't be used like shown above.

Brian Fox
Yes it can. As I have said in the question I was able to insert the variable in the url tag nested in the repository tag. Note that this repository tag that I have mentioned is inside the 'settings.xml' file and not inside a pom file.
Alceu Costa
Sorry I misread. The profile portion is interpolated but not all of the "base" settings are, for example it doesn't work out well if you try to use a property for your local Repository either.
Brian Fox
This is correct, but its not clear from your answer why you can't set properties outside of the profiles element, I've attempted to elaborate with my answer
Rich Seller
+2  A: 

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.


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.

Rich Seller
Great answer. Totally correct.
Alceu Costa