Maven does not replace the variable in 'settings.xml' when it is called

I 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 my mirror to point to a local path, in this case the path:

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

Where $ {mypath} is the variable that I pass when I call Maven:

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

The problem is that Maven does not replace the variable when it is called. I see that this error occurs by checking the build log. For example, Maven reports that it loads a file from a file: // $ {mypath} /maven/.m2/repository when the file: /// D: /test/maven/.m2/repository is correct.

I also noted that Maven correctly replaces my variable when it is inserted into the tag of the child element of the repository tag:

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

The line works correctly when I replace the variable in my settings.xml with the full URL, as in the example below:

 <mirror> <id>myMirror</id> <mirrorOf>*</mirrorOf> <url>file:///D:test/maven/.m2/repository</url> </mirror> 
+7
maven-2
source share
4 answers

Substitution of properties in settings.xml does not work as you expected.

It will replace the properties inside the profile element (as you saw, it will replace your repository URL, which will be defined inside the profile), but not the elements outside the profiles (as you saw, the section happening in the mirrors). This distinction is made because the profile element in settings.xml is a truncated version of the pom.xml profile element. This is a mechanism that allows you to configure the configuration in POM, so property substitution is allowed in the profile elements, since they are actually part of the POM.

Parts of the settings outside the profile element represent the configuration of the platform, they should not be affected by individual assemblies, so they do not replace the properties of the command line. It makes sense, but nothing is really visible.

EDIT: in the mavens documentation settings page , in the last sentence of the quick look section (rather hidden), he states:

Please note that the properties defined in profiles in settings.xml cannot be used for interpolation.


However, there is a workaround, you can replace the environment variables with 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 call Maven as usual, replace the environment variable, and your build should work as needed.

+11
source share

This is an old question now, but, as is the case with Maven 3, and perhaps earlier, you can refer to environmental vars if you prefix ' env '

I do like this:

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

Each developer then sets M2_LOCAL_REPO to the appropriate location.

+2
source share

Settings.xml is not interpolated like pom, so the property cannot be used as shown above.

+1
source share

This is probably a mistake. Unfortunately, replacing properties does not seem to be consistent with Maven plugins. I myself encountered an error specifying more than two properties in the configuration item in another plugin.

0
source share

All Articles