Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

maven-2

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</url>
</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>
like image 564
Alceu Costa Avatar asked Sep 01 '09 16:09

Alceu Costa


People also ask

Does POM xml override settings xml?

These elements configure the build system as a whole, instead of any particular project. It's important to note that values from an active profile in settings. xml will override any equivalent profile values in a pom. xml or profiles.

Do I need to restart Maven after changing settings xml?

The solution is to restart your computer. No, seriously. After restart maven is guaranteed to read settings. xml file again and use whatever changes you made.

What is difference between POM xml and settings xml?

settings. xml contains system and/or user configuration, while the pom. xml contains project information. All build configuration ends up in the pom.

How do you check which settings xml Maven is using?

Use the Maven debug option, ie mvn -X : Apache Maven 3.0.


1 Answers

I had a slightly different experience here from Rich's answer, so I think it's worth mentioning in a different one.

Variable substitution does indeed work inside a profile, but it seems to fail if you need them replaced before parent pom resolution happens.

I had the following configuration here:

<profiles>
  <profile>
    <id>company-dev</id>
    <properties>
      <company.repo.deployment.endpoint>https://companyartifacts.jfrog.io/companyartifacts</company.repo.deployment.endpoint>
      <company.repo.download.endpoint>https://companyartifacts.jfrog.io/companyartifacts</company.repo.download.endpoint>
    </properties>
    <repositories>
      ...
      <repository>
        <id>company-repo-snap</id>
        <name>libs-snapshots</name>
        <url>${company.repo.download.endpoint}/libs-snapshots</url>
        <snapshots>
          <enabled>true</enabled>
        </snapshots>
        <releases>
          <enabled>false</enabled>
        </releases>
      </repository>
    </repositories>
    </pluginRepositories>
  </profile>
</profiles>

It's been like that for years now, every place where I use company.repo.download.endpoint var seems to work great until I needed it to resolve a parent pom like this:

<project ...>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.company.jcompany</groupId>
        <artifactId>persistence</artifactId>
        <version>5-SNAPSHOT</version>
    </parent>
    <artifactId>persistence-api</artifactId>
    <version>5.0.0-SNAPSHOT</version>
    ...
</project>

Then, I started having this exception, even running something as simple as mvn -X help:effective-pom only:

(notice variables are not replaced in URL)

[ERROR]   The project com.company.jcompany:persistence-api:5.0.0-SNAPSHOT (/var/lib/jenkins/jobs/jcompany_persistence_api-trunk/workspace/pom.xml) has 1 error
[ERROR]     Non-resolvable parent POM: Could not transfer artifact com.company.jcompany:persistence:pom:5-SNAPSHOT from/to company-repo-snap (${company.repo.download.endpoint}/libs-snapshots): No connector available to access repository company-repo-snap (${company.repo.download.endpoint}/libs-snapshots) of type default using the available factories WagonRepositoryConnectorFactory and 'parent.relativePath' points at wrong local POM @ line 5, column 10 -> [Help 2]
org.apache.maven.model.resolution.UnresolvableModelException: Could not transfer artifact com.company.jcompany:persistence:pom:5-SNAPSHOT from/to company-repo-snap (${company.repo.download.endpoint}/libs-snapshots): No connector available to access repository company-repo-snap (${company.repo.download.endpoint}/libs-snapshots) of type default using the available factories WagonRepositoryConnectorFactory
    at org.apache.maven.project.ProjectModelResolver.resolveModel(ProjectModelResolver.java:159)
    at org.apache.maven.model.building.DefaultModelBuilder.readParentExternally(DefaultModelBuilder.java:813)
    at org.apache.maven.model.building.DefaultModelBuilder.readParent(DefaultModelBuilder.java:664)
    at org.apache.maven.model.building.DefaultModelBuilder.build(DefaultModelBuilder.java:310)
    at org.apache.maven.model.building.DefaultModelBuilder.build(DefaultModelBuilder.java:232)

The only reasonable explanation I find for this problem is that parent pom resolution happens so early in the process that variables are not replaced yet. When I don't have to fetch snapshots for parent poms, then all variables are replaced successfully, as expected. Not sure if I should report this as a bug.

Using Maven 3.6.3 at time of answer, if that matters.

like image 87
BrunoJCM Avatar answered Oct 04 '22 02:10

BrunoJCM