Should the mirror id be unique in maven - maven

What will happen if i have two mirrors with same id in the settings.xml file?
Which one will be taken into account?

It won't do anything more than 1 repo.
You can even use it to have different mirrors with the same credentials.
But consider that you could did it by yourself ... This is a trivial question which take 1 minutes + build time to test by yourself.
Nevertheless :
<mirrors>
<mirror>
<id>mirror.repo.myorg.fr</id>
<mirrorOf>!tiers-providers-public-snapshots,*</mirrorOf>
<name>Repository Artifactory for snapshots.</name>
<url>http://mirror.repo.myorg.fr/artifactory/repo-snapsthot</url>
</mirror>
<mirror>
<id>mirror.repo.myorg.fr</id>
<mirrorOf>!tiers-providers-public,*</mirrorOf>
<name>Repository Artifactory for realeases</name>
<url>http://mirror.repo.myorg.fr/artifactory/repo</url>
</mirror>
</mirrors>
This is the same machine, but two mirrors for differents purposes. I'm not sure this case would happen, but it is not so far of my own configuration.
I tried it, and it's obviously working.

Related

Maven - How to use two mirrors

I have to use two mirrors.
In .m2/settings I have
<mirrors>
<mirror>
<id>nexus_xyz</id>
<mirrorOf>*</mirrorOf>
<url>https://../</url>
</mirror>
<mirror>
<id>nexus_wso2</id>
<mirrorOf>*</mirrorOf>
<url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
</mirror>
</mirrors>
Then in the pom.xml of my project I don't know how to say to download something from the first mirror and something from the second mirror. Thanks
The bad news: You cannot determine for different dependencies where they should be downloaded from.
But the good news: You don't need such a thing. Mirroring usually makes only sense if you want to send (nearly) every request to the same repository. You usually do this if you have a company Nexus/Artifactory and use it to proxy all external repositories you need. If you don't have a company Nexus/Artifactory, you should list the repositories you need unter the <repositories> tag in the settings.xml. In this case, you don't define a mirror, or you only mirror all requests that you do not handle otherwise (by setting <mirrorOf> to something like *,!rep1,!rep2). Maven will then go through all the defined repositories until it finds the required artifact.
If the package isn't in the first mirror, it should fallback to the second one.
In Maven, it's not possible to tell it "download this dependency from this mirror, or repository".

Project repository is ignored if mirrors are configured in the settings.xml

I've created a project repository using this guide: https://devcenter.heroku.com/articles/local-maven-dependencies
This works fine if I comment out the mirrors definition in the settings.xml file in the .m2 folder.
If the mirrors are defined, the project repository is not considered. Do I have to add it as a mirror as well? It would be great if this can be somehow handled inside the pom.xml.
pom.xml
<repositories>
<repository>
<id>repo</id>
<url>file://${project.basedir}/repo</url>
</repository>
</repositories>
Settings.xml
<mirrors>
<mirror>
<id>de.companyname.repository.release</id>
<mirrorOf>de.companyname.repository</mirrorOf>
<url>https://repository.companyname.de/content/repositories/releases</url>
</mirror>
<mirror>
<id>de.companyname.repository</id>
<mirrorOf>de.companyname.repository</mirrorOf>
<url>https://repository.companyname.de/content/repositories/snapshots</url>
</mirror>
<mirror>
<id>nexus-else</id>
<mirrorOf>*</mirrorOf>
<url>http://nexus.companyname.de:8081/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>nexus</id>
<mirrorOf>central</mirrorOf>
<url>http://nexus.companyname.de:8081/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>nexus-snapshots</id>
<mirrorOf>central-snapshots</mirrorOf>
<url>http://nexus.companyname.de:8081/nexus/content/groups/public-snapshots</url>
</mirror>
</mirrors>
This is normal, and is actually the wanted use-case of mirrors. They are used in order to have Maven download dependencies from another location that the one defined in <repository> element. More info on how it does that in this related answer.
In your case, you have this mirror configuration:
<mirror>
<id>nexus-else</id>
<mirrorOf>*</mirrorOf>
<url>http://nexus.companyname.de:8081/nexus/content/groups/public</url>
</mirror>
which means that this <mirror> will mirror *, i.e. all repositories. So your repo declaration inside your POM is not being taken into account, because this mirror is configured to mirror it. As such, every request that Maven would have made at file://${project.basedir}/repo is in fact redirected to your mirror URL. You have two solutions here:
Don't tell nexus-else to mirror local file based repository. You can do that with
<mirror>
<id>nexus-else</id>
<mirrorOf>external:*</mirrorOf>
<url>http://nexus.companyname.de:8081/nexus/content/groups/public</url>
</mirror>
external:* matches all repositories except those using localhost or file based repositories. This is used in conjunction with a repository manager when you want to exclude redirecting repositories that are defined for Integration Testing.
Since your repo declaration is a file repository poiting to your localhost, it won't be mirrored by nexus-else. This also makes sure that any other file based repository on your localhost that you add in the future won't be mirrored as well.
Exclude repo from the mirror configuration with:
<mirror>
<id>nexus-else</id>
<mirrorOf>*,!repo</mirrorOf>
<url>http://nexus.companyname.de:8081/nexus/content/groups/public</url>
</mirror>
*,!repo1 = everything except repo1
This solution might be more fragile than the one above, because you need to hard-code the id of the repository.

How do you override a user Maven settings file when building a grails project?

I'm using Grails 2.4.1 to build a Grails plugin project via Jenkins & the Grails Release plugin (3.0.1) to publish that plugin to our Nexus repository.
The user that runs Jetbrains (jetbrains) on our build server already has a .m2/settings.xml file. That file defines some Mirrors that are causing havok when trying to publish plugins to our nexus repository. The mirror defined in the settings.xml file reroutes everything to the public 'group'.
What I would like to do is for specific jobs force maven to either ignore the settings.xml file all together or to specify my own.
I could put a file in the M2_HOME/conf directory but since the user directory already has a settings.xml file that contains mirrors it seems like anything I do in the M2_HOME/conf directory would be ignored.
I've seen the -s file-name-here solution, but I don't see any way to pass that through my grails publish-plugin command while building.
I'd also be open to solutions that redefine the mirror we have so that everything EXCEPT our internal repositories are rerouted.
The mirror block I have is:
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>Nexus Public Mirror</name>
<url>http://maven.internal.website/nexus/content/groups/public</url>
</mirror>
As per the Maven Mirror guide, I've tried:
<mirror>
<id>nexus</id>
<mirrorOf>external:*</mirrorOf>
<name>Nexus Public Mirror</name>
<url>http://maven.internal.website/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>nexus</id>
<mirrorOf>external:*,!snapshots,!releases</mirrorOf>
<name>Nexus Public Mirror</name>
<url>http://maven.internal.website/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>nexus</id>
<mirrorOf>*,!snapshots,!releases</mirrorOf>
<name>Nexus Public Mirror</name>
<url>http://maven.internal.website/nexus/content/groups/public</url>
</mirror>
IMMEDIATELY after posting my question I realized I hadn't tried one pattern... and it seems to work just fine:
<mirror>
<id>nexus</id>
<mirrorOf>!snapshots,!releases</mirrorOf>
<name>Nexus Public Mirror</name>
<url>http://maven.internal.website/nexus/content/groups/public</url>
</mirror>

maven - mirror depending on project

I have a mirror for maven central setup like this in settings.xml:
<mirrors>
<mirror>
<id>artifactory-other</id>
<mirrorOf>*</mirrorOf>
<url>http://some.internal.site/artifactory/repo</url>
<name>Artifactory</name>
</mirror>
</mirrors>
But this server is accessible only from the internal network. When I'm at home and tinker with some side projects I need to access the real repositories. For now I just comment this mirror out, but it's cumbersome.
How can I make it automatic? Is it possible to define a profile with separate mirrors and automatically activated based on project path? Is there some simple solution?
Unfortunately there is not an out-of-the-box solution with Maven. Usual i check in my settings.xml via git and having different branches for different networks like at work, home etc. The result is reduced to a simple
git checkout HOME
or
git checkout WORK
etc.
The solution is not using mirror-any, but using specific repository declarations with dependencies pom cleanups.
This way you'll be as protected from going to unauthorized repos as with mirror-any and you'll be able to declare profiles (work and home) with different servers in each of them.
With new maven (3.3.1+), use the project-settings-extension to load the project settings, and put project specific mirrors into ${basedir}/.mvn/settings.xml in each project.
in ${basedir}/.mvn/extensions.xml
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
<extension>
<groupId>com.github.gzm55.maven</groupId>
<artifactId>project-settings-extension</artifactId>
<version>0.0.1</version>
</extension>
</extensions>
in ${basedir}/.mvn/settings.xml
<settings>
<mirrors>
<mirror>
<id>id</id>
<url>https://internal-repo/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
</settings>
Then the project will be built out-of-box in the internal network with mvn test, and not pollute your personal .m2/settings.xml

How should I point to different repository mirrors in maven settings.xml

We have 2 separate products used by different teams and we would like to point to mirrorA for projectA and mirrorB for projectB. How can I achieve this in settings.xml and should I do this away from settings.xml and somehow reference in my project specific POM file.
<mirrors>
<mirror>
<id>internal</id>
<mirrorOf>*</mirrorOf>
<name>Internal Release Repository</name>
<url>http://192.168.1.4:7777/archiva/repository/internal</url>
</mirror>
</mirrors>
Note: This scenario happens when a developer contributes to both of these projects.
I think you might be confusing "mirrors" with "repositories". You should very probably not have a mirror configuration like that in your settings.xml. If different projects should deploy to or get their dependencies from different repositories, you just specify those repositories in the respective projects' poms. Setting up mirrors solves an entirely different problem than setting up repositories does.

Resources