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>
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.
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.
settings. xml contains system and/or user configuration, while the pom. xml contains project information. All build configuration ends up in the pom.
Use the Maven debug option, ie mvn -X : Apache Maven 3.0.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With