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

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).

Related

maven If mirror fails, fall back to original

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.

Which folder does the Maven settings.xml file belong in?

Okay so I have been searching for the answer here in SE and came across this comment and several others like it
When Maven tries to build a project, it will look in your local repository (by default ~/.m2/repository but you can configure it by changing the value in your ~/.m2/settings.xml) to find any dependency, plugin or report defined in your pom.xml. If the adequate artifact is not found in your local repository, it will look in all external repositories configured, starting with the default one, http://repo1.maven.org.`
So my question is where is the settings.xml file suppose to reside?
I was told by the previous developers on the project that it is in the maven\conf\settings.xml which is where the only settings.xml file we have is located. This is contradictory to the above comment of .m2/settings.xml.
Please clarify where this file is to reside and if it makes a difference where it is at.
Both answers are correct.
Basic config file for maven is inside mvn/conf/settings and you could add some configuration (or override basic config) with your personal maven settings file located in ~.m2/settings.xml
settings.xml file may be found in 2 places:
The Maven install: ${maven.home}/conf/settings.xml
A user’s install: ${user.home}/.m2/settings.xml
If both files exists, their contents gets merged, with the user-specific settings.xml being dominant.
Ref : https://maven.apache.org/settings.html

How to specify different local maven repositories for different projects in their pom files?

I have one .m2 repository. I have 2 projects. JavaProject and HibernateProject. I want them to reference different .m2 repositories. How is this done?
You can't specify the local repository location in your POM file. If you were able to, it would break Maven: the whole point is to be able to distribute your POM file to other people and have the code just work. If the POM file contains a reference to ~seanmc/localrepos/hibernateproject then it's not going to work on my machine.
The place where you specify the local repository is in your settings.xml. Typically you have one global settings and one user settings. You can pass the -s flag to Maven to specify a settings file to use. So you could make a settings file for each project. Inside the settings file (reference) you'll want a <localRepository> tag with the path to the local Maven repository you want to use.
Be aware, though, that doing this is contrary to Maven's design. Probably, you have a bug in your architecture or your thinking.

jenkins maven repository configuration

I am new to maven and Jenkins, I am trying to build a maven 3 project.
I have maven in c drive and I moved the repository to d drive.
But when I configure a build in Jenkins, it is creating its own repository(I think so)
Now How to configure jenkis, so that it can use my repository in D drive.
Hope I made sense.
you need to adjust the settings.xml to point out which repository to use.
<localRepository>C:\\USER_LOCAL_DIR\.m2\m2repository</localRepository>
where USER_LOCAL_DIR is your user directory (e.g. Users\your_user_name).
Further more you should make sure which settings.xml the maven is using, if you dont have any specific user settings.xml the maven will start using the settings.xml which is at maven configuration directory, which will be M2_HOME/conf/settings.xml (M2_HOME being the Maven home directory). The hierarchy for settings.xml is first the user .m2 directory and if it could not find that, it will try to find the one in M2_HOME/conf/settings.xml.
Hope this helps.
You may need to check for settings.xml file under
..\maven-xx\conf\settings.xml file
C:\Users\Username.m2\settings.xml file
You may need to remember precedence in the order mentioned. In case if you need to point to a private maven repository consider to check both the files and in case if it's not pointing to intended location then update the settings file accordingly.

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