maven If mirror fails, fall back to original - maven

I have a sonatype-nexus configured in lan for maven and the is connected using the mirror configuration in settings.xml shown below.
<mirrors>
<mirror>
<mirrorOf>*</mirrorOf>
<name>nexus.sb.karmalab.net</name>
<url>http://<lan_repo>/remote-repos</url>
</mirror>
This setting creates an issue if the is down.
Is there some option to configure the mirror to connect to the remote repo id the local lookup fails.

Maven Jira has an issue for this feature, but it was closed years ago without being addressed.

I have a similiar setup, where I have a Nexus3 Repository Manager setup on my lan, and ran into the same problem when I turned off my Nexus3 Repository Manager.
Based on what I read the issue linked by Igor Dvorhak was closed and the topic seems to not have been brought up recently.
Anyhow, here is a workaround that worked for me.
You get two locations to place settings.xml
User Level ... normally provided in ${user.home}/.m2/settings.xml.
Global level ... normally provided in ${maven.conf}/settings.xml.
If a User Level settings.xml is defined it takes precedence over Global level but also you can override it by giving the maven command the -s flag with the path to your settings.xml.
You can learn more about settings.xml here: Maven - Settings Reference
So what I did is setup the mirror at the User Level, but when the repository the mirror is pointed at was down (i.e. our own setup lan repository), I instructed the mvn command to use the global settings.xml which is set to go to central by default.
I happened to have an environment variable called M2_HOME in my ${user.home}/.bashrc file when I was setting up apache maven, So I am going to reuse that environment variable to construct the path. Then there is a conf folder inside the apache maven installation folder that contains the global settings.xml.
Finally here is the command to workaround the offline mirror site.
mvn clean install -s $M2_HOME/conf/settings.xml
Note: Your paths may be different if your working on a Windows OS.

Related

Resolve maven dependencies from JFrog artifactory by configuration in pom.xml

I need to point Maven to JFrog artifactory ONLY to resolve dependencies and not to deploy. For this I'm not allowed to use settings.xml.
I found artifactory-maven-plugin]1 but it show only how to deploy. The main issue that I need to set username/password to access JFrog and also setup Virtual Repos to resolve artifacts and plugins.
And thoughts how to this without settings.xml via pom.xml? Creds I can add to env variables.
You can add a <repositories> section to your POM. It will not override your <distributionManagement> (for deployment). Whether it will actually work depends on the mirror settings in the settings.xml.
Actually, I find it strange that you do not want to change the settings.xml. If you are not responsible for the settings.xml, you can talk to the relevant manager. You could also ask if the relevant repository is added to the company repository as proxy (if you have such a thing).

Maven with user settings.xml and project-specific settings.xml

I have several projects which point to different internal repositories. Users on one project might not be able to see the repository on the other project, so I need a project specific settings.xml for each project.
Create project-specific Maven settings indicates that I can do this by creating a MVN_ROOT/.mvn/maven.config with
--settings ./.mvn/settings.xml
and then putting the repository information inside MVN_ROOT/.mvn/settings.xml.
That seems to work and I can add those files to git and other users can use the repository automatically.
However, the repositories require validation, which requires that settings.xml contain something like
...
<server>
<id>repo-proj-1</id>
<username>me</username>
<password>secret</password>
</server>
<server>
<id>repo-proj-2</id>
<username>my.name</username>
<password>secure</password>
</server>
...
Obviously, I do not want to put this information into the git repository. ~/.m2/settings.xml is the obvious place to put that information. However, it appears that I can only have one user settings file, as my login information is not picked up when I try to build.
Is there a way to use both settings files?
This needs to work on Mac, Linux and Windows, so something like <xs:include>~/.m2/settings.xml</xs:include> (XML include, probably not the right syntax) won't work as ~/.m2 is not usually the right location on Windows.
We are using Maven 3, and if a specific recent version is required, we can require users to upgrade to that version.
What about to treat the secrets as environment variables?
<server>
<id>repo-proj-2</id>
<username>${user}</username>
<password>${pass}</password>
</server>
You can set them then via your build tool:
mvn -s ./build/settings.xml -Duser=xxx -Dpass=xxx clean install
There should be a central repository which contains SNAPSHOT's and an other one which contains releases and a group in a repository manager which combines them for consuming. This should be configured in a settings.xml located either in Users home directory or in the configuration of your CI server.
Deployment to repositories should only being done by a CI Server never by a developer.
In the CI server you should correctly setup the credentials to be hosted by the CI Server (Jenkins: Credentials Store) etc.
If you need separate settings.xml by project it is a design problem in your setup's...and will cause many issues.
The separation of different projects is a basic thing in a Maven repository which is called coordinate which is a combination of groupId and artifactId plus version.
In general a settings.xml should be located only in the users home directory which contains users credentials if needed (as already mentioned combined with encryption).

How to configure Maven to not use proxy server

I am trying to configure Maven to not use a proxy server. This turns out to be a lot of work. Somewhere Maven has read some proxy information which it keeps on using.
I tried the following already.
in the terminal unset http_proxy and https_proxy
I did not have a settings.xml. I added one and added a proxy config to it with active is false
In settings.xml active = true and nonProxyHosts = *
I remove proxy settings from /etc/environment
I don't want to install a local proxy server just to get this working, is there another way to tell Maven not to use a proxy server.
The first step is to ask Maven to show you what configuration it sees/uses. Use the Help plugin for this: mvn help:effective-pom shows you the complete POM that Maven will use and help:effective-settings will do the same for the settings.xml.
Now you can see whether there are any proxy settings in those outputs, just to make sure the Maven universe is clean. My guess is that someone changes the global/default settings.xml from the conf folder of your Maven installation.
Afterwards, you need to check the proxy options which Java uses. Check your environment and the file $HOME/.mavenrc

Does maven maintain any setting cache?

I comment out my local repository section im my pom.xml, but when I ran mvn eclipse: eclipse, it still try to download plugin from local repository. I also check the pom.xml it depends on and make sure to comment out them either, but this problem still occurs.
I think I might neglect some setting?
You may have a settings.xml in the default maven repository location .m2. If you are running maven inside eclipse, check your maven configuration inside Eclipse preferences, there you should find the location of the settings.xml file.

How can I get Nexus to grab artifact files in proxy repositories?

My setup is that I have a local Maven repository on my machine, and one on my team's machine that's managed by Nexus. I added a new proxy repository to Nexus. When I select this new proxy repository and hit "Browse Remote", I see everything (all the JARs, POM files, etc.) in the repository, as it should be. But when I click "Browse Storage", I see almost nothing ... only a handful of POM files, and none of the JAR files.
My Maven build is failing, presumably because it can't get the JAR files from Nexus (the JARs don't appear in my local repository, either, but the POMs do). The other virtual repositories on my Nexus all have the JAR files in storage and in my local repository; how can I get Nexus to grab the JAR artifacts from the remote repository?
I'm sure this is something basic, but I've searched and didn't come up with anything I can use. Thank you!
Enable remote repository index download, add the repository to the public group, make sure that Maven hits Nexus and most importantly read the free book about Nexus.
Here is the chapter about configuring Maven to work with Nexus.
Oh and for proxy repositories it will only download remote artifacts if they are needed e.g. by a Maven build accessing Nexus..
I just ran into this issue as well and thought it might help to have another answer.
I have my repository location at: http://serverName.company.com/nexus-2.3 and I copied/pasted that URL into my settings.xml file. That nexus server points to another company's nexus server so that we only have to point to one to pull down our stuff as well as theirs. Unfortunately, I wasn't able to do that.
That is, until I found out that you have to use the following URL instead of the one above:
http://serverName.company.com/nexus-2.3/content/groups/public/
The snippet of my settings.xml looks like:
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://serverName.company.com/nexus-2.3/content/groups/public/</url>
</mirror>
I spent far too long figuring this out. I hope this helps someone in the future.

Resources