Maven ignores settings.xml file - maven

I have added proxy configuration in settigns.xml file, but it is not used by Maven, i confirmed this by making the settings.xml file invalid. I ran the maven install command to update settings and global-settings to point to the correct file, still no luck. I am using maven3.0.4.

Try running Maven with the -X option. It should print as part of the debug output which settings file is being used.
Since you already tried it with an invalid file, I bet that something is wrong with the location of your file.

It's almost to stupid to tell, but it might save some time for somebody else: If you're using a new computer, make sure file extensions are displayed. Otherwise your "settings.xml" file probably is a "settings.xml.txt" file in fact...

Make sure it is in the right directory (HOME/.m2/settings.xml)
You can find the relevant paths and a proxy example here: Maven proxy settings not working
And of course the reference is always useful: https://maven.apache.org/settings.html

have you tried with these options: from the command line to specify the settings file?
mvn -o –Dmaven.repo.local=$HOME/.my_m2path/repository clean install --settings $HOME/.my_m2path/settings.xml Dcheckstyle.skip=true –DskipTests
Some options that might not be necessary
-o is for offline (unless you have all your repos in your m2, its suggested to skip this option)
skip tests is for skipping tests while building
–Dmaven.repo.local - repo path - if you are having own repo path, then use this option
--settings $HOME/.my_m2path/settings.xml (remember there is space between settings and the path)

Since there is no accepted answer, and I encountered that problem today and other answers proved unhlepful as the file path was correct:
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.

For those using Linux
In the Ubuntu package repository (and probably other disto's too), there are two maven packages: maven and maven2. For some reason, I had maven2 installed, which seems to ignore settings.xml in ~/.m2.
As a solution, I removed it using
sudo apt-get purge maven2
and installed the other one with
sudo apt-get install maven
What's going on?
I couldn't find a reliable source, but apparently, maven2 is an older version (2.x), as the latest maven has version 3.x and is served with maven.

Please make sure you are using the settings.xml which you modified.
for example: your using IDE's embedded maven which using different settings.xml in you operation system.

You can set the path of settings file in Eclipse* as :
In the menubar goto Window -> Preferences
In Preferences Dialog, Goto Maven Section(On the left) and Expand it.
Click on UserSettings.
Add the path of settings file using Browse. The default for most users will be (C:\Users\.m2\settings.xml). It will be shown in grey but you need to actually enter the location.
Click on Update Settings !
You can also enable debug logs and stack traces for debugging by clicking on the Maven Section in the Preferences dialog and Checking the box against the label "Debug Output".
P.S. I am currently using Eclipse(Neon) on Windows 10 x64.

I have also been facing the same issue. I removed the file and folder, but still maven was still picking the settings.
For me, restarting the system solved the issue.

What I found was that even if I set my own maven setting file using the --settings or -s command such as:
mvn -s $MAVEN_HOME/libexec/conf/my-settings.xml clean
Maven was still reading the default global setting.xml
Two ways that one can slove this:
Rename the setting.xml to something like setting.xml.bak in the $MAVEN_HOME/conf dicrectory.
The preferred approach add global to the command -gs hence the final command becomes:
mvn -gs $MAVEN_HOME/libexec/conf/my-settings.xml clean

Related

Vscode with custom settings.xml Maven file per project in a workspace

I have a Vscode workspace with a bunch of Maven projects, and which one has its own Maven settings.xml file.
As each project has its own settings file with the same name but in its own root folder I got success configuring both the workspace as each of the projects with such an option: {"maven.executable.options": "-s my_settings.xml"}.
Although, I can run successfully any of Maven commands for all projects, Vscode reports problems in the pom.xml files about missing artifacts and the same errors are reported when I run tests from the "Testing" in the Side Bar.
When I copy one of the project's settings file to the ~/.m2/settings.xml that given project works fine, no errors at all.
So, I conclude Vscode doesn't respect the "maven.executable.options" for all of project operations inside IDE, like tests and maybe others operations but only for directly launched Maven commands.
I don't want to merge all those settings.xml files into a big one in my local ~/.m2/settgins.xml each time I have to open a given set of projects.
Is there a way to teach Vscode to use different settings.xml for each project and make it work properly in all Vscode operations (not only Maven commands)?
VSCode setting ID: java.configuration.maven.userSettings
In VSCode, go to Preferences > Settings > Workspace
Search 'maven'
Set 'Java > Configuration > Maven: User Settings' to ./settings.xml to make VSCode use the local settings.xml of the project
Restart VSCode if needed
Screenshot
you can use
mvn --settings settings.xml clean install
to individually point respective settings.xml for each java app
First, check whether the terminal executes the command "mvn ... -s my_settings.xml", which indicates the correctness of vscode-maven plugin. If so, the problem comes from the command itself.
A workaround is to delete the global maven settings.xml, typically in $MAVEN_HOME\conf\settings.xml. Then mvn ... -s my_settings.xml should work. It works fine for me.
The reason may be that when global maven settings file is found, mvn CLI may ignore the settings file in mvn option.

Can we configure settings.xml to our own location in maven

Recently I am working with Apache Maven in Windows machine.
I am executing maven from Command Prompt every time.
I know that MAVEN will pick its settings.xml from C:\Users\abc\.m2\settings.xml
Question: Can't I change this settings.xml to be picked from my own provided location instead of default location, like C:\Users\abc\.m2\settings.xml
That is, instead of C:\Users\abc\.m2\settings.xml location I will configuring it in F:\myFolder\settings.xml.
If so, what are the steps I need to follow and check?
You can change the location of user settings using the -s option from the command line:
-s,--settings <arg> Alternate path for the user settings file
You could hence invoke Maven as following:
mvn clean install -s F:\myFolder\settings.xml
If you don't want to specify it at each and every invocation, since Maven 3.3.1 you can specify a .m/config folder in your project folder and a maven.config file in it providing this option.
The .m/config/maven.config file would have in your case the following content:
-s F:\myFolder\settings.xml
And it will be applied to all Maven invocation for that project.

How to specify an alternate location for the .m2 folder or settings.xml permanently?

I am using Maven 3.0, and my .m2 folder location is C:\Users\me\.m2.
However, I do not have write access to that folder, but I want to change the repository location from the one defined in the settings.xml.
Due to restricted access, I am not able to edit the settings.xml to change the repository location.
How can I override the values of my settings.xml -or change the default location of the .m2 folder- without editing my C:\Users\me\.m2\conf\settings.xml file?
You need to add this line into your settings.xml (or uncomment if it's already there).
<localRepository>C:\Users\me\.m2\repo</localRepository>
Also it's possible to run your commands with mvn clean install -gs C:\Users\me\.m2\settings.xml - this parameter will force maven to use different settings.xml then the default one (which is in $HOME/.m2/settings.xml)
It's funny how other answers ignore the fact that you can't write to that file...
There are a few workarounds that come to my mind which could help use an arbitrary C:\redirected\settings.xml and use the mvn command as usual happily ever after.
mvn alias
In a Unix shell (or on Cygwin) you can create
alias mvn='mvn --global-settings "C:\redirected\settings.xml"'
so when you're calling mvn blah blah from anywhere the config is "automatically" picked up.
See How to create alias in cmd? if you want this, but don't have a Unix shell.
mvn wrapper
Configure your environment so that mvn is resolved to a wrapper script when typed in the command line:
Remove your MVN_HOME/bin or M2_HOME/bin from your PATH so mvn is not resolved any more.
Add a folder to PATH (or use an existing one)
In that folder create an mvn.bat file with contents:
call C:\your\path\to\maven\bin\mvn.bat --global-settings "C:\redirected\settings.xml" %*
Note: if you want some projects to behave differently you can just create mvn.bat in the same folder as pom.xml so when you run plain mvn it resolves to the local one.
Use where mvn at any time to check how it is resolved, the first one will be run when you type mvn.
mvn.bat hack
If you have write access to C:\your\path\to\maven\bin\mvn.bat, edit the file and add set MAVEN_CMD_LINE_ARG to the :runm2 part:
#REM Start MAVEN2
:runm2
set MAVEN_CMD_LINE_ARGS=--global-settings "C:\redirected\settings.xml" %MAVEN_CMD_LINE_ARGS%
set CLASSWORLDS_LAUNCHER=...
mvn.sh hack
For completeness, you can change the C:\your\path\to\maven\bin\mvn shell script too by changing the exec "$JAVACMD" command's
${CLASSWORLDS_LAUNCHER} "$#"
part to
${CLASSWORLDS_LAUNCHER} --global-settings "C:\redirected\settings.xml" "$#"
Suggestion/Rant
As a person in IT it's funny that you don't have access to your own home folder, for me this constitutes as incompetence from the company you're working for: this is equivalent of hiring someone to do software development, but not providing even the possibility to use anything other than notepad.exe or Microsoft Word to edit the source files. I'd suggest to contact your help desk or administrator and request write access at least to that particular file so that you can change the path of the local repository.
Disclaimer: None of these are tested for this particular use case, but I successfully used all of them previously for various other software.
Nobody suggested this, but you can use -Dmaven.repo.local command line argument to change where the repository is at. In addition, according to settings.xml documentation, you can set -Dmaven.home where it looks for the settings.xml file.
See: Settings.xml documentation
Below is the configuration in Maven software by default in MAVEN_HOME\conf\settings.xml.
<settings>
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
Add the below line under this configuration, will fulfill the requirement.
<localRepository>custom_path</localRepository>
Ex: <localRepository>D:/MYNAME/settings/.m2/repository</localRepository>
You can change the default location of .m2 directory in m2.conf file. It resides in your maven installation directory.
add modify this line in
m2.conf
set maven.home C:\Users\me\.m2
You can point to a different-settings.xml when you deploy your project. When deployed from the project folder you can have a relative path to get back to your home folder:
mvn clean deploy -s ../../.m2/different-settings.xml

how to force maven to update local repo

I compiled a jar file in one project so it can be consumed in the 2nd one. I can see the jar file in .m2 folder. But in the 2nd project it complains about artifact not found.
I guess I have to force maven to update indices/cache something but don't know what exactly. Any tip, thanks.
Update: thanks for all good suggestions.
Turns out that the maven plugin (of IntelliJ) in the second project doesn't update its index. I use command line it compiled ok.
try using -U (aka --update-snapshots) when you run maven
And make sure the dependency definition is correct
You can also use this command on the command line:
mvn dependency:purge-local-repository clean install
If you are installing into local repository, there is no special index/cache update needed.
Make sure that:
You have installed the first artifact in your local repository properly. Simply copying the file to .m2 may not work as expected. Make sure you install it by mvn install
The dependency in 2nd project is setup correctly. Check on any typo in groupId/artifactId/version, or unmatched artifact type/classifier.
Even though this is an old question, I 've stumbled upon this issue multiple times and until now never figured out how to fix it. The update maven indices is a term coined by IntelliJ, and if it still doesn't work after you've compiled the first project, chances are that you are using 2 different maven installations.
Press CTRL+Shift+A to open up the Actions menu. Type Maven and go to Maven Settings. Check the Home Directory to use the same maven as you use via the command line
Click settings and search for "Repositories", then select the local repo and click "Update". That's all. This action meets my need.
If you are struggling with authenticating to a site, and Maven is caching the results, simply removing the meta-data about the site from the meta-data stash will force Maven to revisit the site.
gvim <local-git-repository>/commons-codec/resolver-status.properties

Missing maven .m2 folder

AFAIK maven does not have an installer for Windows, you simply unzip it wherever you like, as explained here.
However in many places there are references to a .m2 folder under the user folder (in Win7 I would guess it to be by default at C:\Users\.m2. Alas I do not have that folder. Is there some command to create this folder? Am I missing something basic?
On a Windows machine, the .m2 folder is expected to be located under ${user.home}. On Windows 7 and Vista this resolves to <root>\Users\<username> and on XP it is <root>\Documents and Settings\<username>\.m2. So you'd normally see it under c:\Users\Jonathan\.m2.
If you want to create a folder with a . prefix on Windows, you can simply do this on the command line.
Go to Start->Run
Type cmd and press Enter
At the command prompt type md c:\Users\Jonathan\.m2 (or equivalent for your ${user.home} value).
Note that you don't actually need the .m2 location unless you want to create a distinct user settings file, which is optional (see the Settings reference for more details).
If you don't need a separate user settings file and don't really want the local repository under your user home you can simply set the location of your repository to a different folder by modifying the global settings file (located in \conf\settings.xml).
The following snippet would set the local repository to c:\Maven\repository for example:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>c:\Maven\repository</localRepository>
...
When you first install maven, .m2 folder will not be present in C:\Users\ {user} path.
To generate the folder you have to run any maven command e.g. mvn clean, mvn install etc. so that it searches for settings.xml in .m2 folder and when not found creates one.
So long story cur short,
open cmd -> mvn install
It will show could not find any projects(Don't worry maven is working fine :P)
now check your user folder.
P.S. If still not able to view .m2 folder try unhiding hidden items.
Use mvn -X or mvn --debug to find out from which different locations Maven reads settings.xml. This switch activates debug logging. Just check the first lines of mvn --debug | findstr /i /c:using /c:reading.
Right, Maven uses the Java system property user.home as location for the .m2 folder.
But user.home does not always resolve to %USERPROFILE%\.m2. If you have moved the location of your Desktop folder to another place, user.home might resolve to the parent directory of this new Desktop folder. This happens when using Windows Vista or a more recent Windows together with Java 7 or any older Java version.
The blog post Java’s “user.home” is Wrong on Windows describes it very well and gives links to the official bug reports. The bug is marked as resolved in Java 8. The comment of the blog's visitor Lars proposes a nice workaround.
Do you have the file system display config set up to show hidden files and folders? If I remember correctly, by default it's hidden. Should be under c:\users\username\.m2.
If the default .m2 is unable to find, maybe someone changed the default path. Issue the following command to find out where is the Maven local repository,
mvn help:evaluate -Dexpression=settings.localRepository
The above command will scan for projects and run some tasks.
Final outcome will be like below
As you can see in the picture the maven local repository is C:\Users\X\.m2\repository
Check the configurations in {M2_HOME}\conf\setting.xml as mentioned in the following link.
http://www.mkyong.com/maven/where-is-maven-local-repository/
Hope this helps.
Is there some command to create this folder?
If smb face this issue again, you should know the most simple way to create .m2 folder. If you unzipped maven and set up maven path variable - just try mvn clean command from anywhere you like! Dont be afraid of error messages when running - it works and creates needed directory.
If I'm right, it's just because you are missing the cd command. Try c:\Users\Jonathan\cd .m2/.

Resources