Specifying Maven's local repository location as a CLI parameter - maven

Is it possible to set the location of the local Maven repository as argument on the Maven command line?
The thing is that I don't use the default one in ~/.m2/repository. However I checked out some project that is being built with its own settings with -s settings.xml. That settings.xml doesn't specify my local repository, so Maven uses uses ~/.m2/repository again... I would like to use a non-default local repository location without having to add a <localRepository> element in the project's settings.xml
I've tried
-DlocalRepository="..."
$mvn invoker:run -s settings.xml clean install -DskipTests -DlocalRepositoryPath=
-Dsettings.localRepository
but none of these options works.
So I think I have to decide whether I will be modifying a third party settings.xml or move my local repo to ~

use maven property maven.repo.local:
mvn -Dmaven.repo.local=$HOME/.my/other/repository clean install
No modifications to settings.xml are necessary.

For git:
alias mvn='mvn "-Dmaven.repo.local=$(git rev-parse --show-toplevel)/.m2/repository"'
This uses a separate maven repository in each git repository

One kind of hacky way that would work is:
Add <localRepository>${m2.localRepository}</localRepository> to your settings.xml
In your mvn.sh or mvn.bat, add -Dm2.localRepository=<full path to home dir>/.m2/repository before the "$#" in the command that gets executed. This will make your default local repo stay where it should be.
Now you can use mvn -Dm2.localRepository=... <phases/goals>. Because your command line system property gets added to the command line after the one in the mvn script, it will take precedence. (At least I'm pretty sure it works that way on both windows and linux, but testing will tell.)

Related

Maven creating local repository in the current directory with a '?' subdirectory

I tried to switch java version on Ubuntu through update-alternatives. However, in the process something happened to maven settings. Maven is not using local repository as <user>/.m2/repository anymore. Instead, whenever I am running mvn command it is creating a suddirectory with '?' name in the current directory and downloading packages in .m2 path within the curren_dir/'?' location. How can I fix the original maven settins. mvn help:effective-settings also showing the same thing.
I tried to remove and purge maven and reinstall, but that does not solve anything.

Is possible to have 2 settings.xml for a single maven home folder?

I am having 2 projects. As per the requirement, I have to maintain the repos separately and some configuration also different.
I don't have permissions to install one more maven installation folder in my lap.
Is possible to have 2 settings.xml for a single maven installation home folder? (I would like to maintain like settings_A.xml and settings_B.xml in different locations).
Is it valid? Please correct me if I am wrong.
Thanks.
yes you can point to a custom settings.xml per use and pass it as argument to Maven as following:
mvn --settings YourOwnSettings.xml clean install
or use shorter form:
mvn -s YourOwnSettings.xml clean install
you can also use project specific settings.xml configuration that explained in other stackoverflow question here

Use default settings.xml file instead of one in ~/.m2

Is there possibility for Maven to ignore settings.xml contained in .m2 folder?
I want it to use default repository, but my laptop has configured development environment for other repositories and I don't want to lose these settings. What's more, these repositories have priority over repos in my pom.xml and fail if they can't download artifact.
What are the options? Should I choose other settings.xml file or is there option to use none?
You don't have to have a settings.xml in the .m2 folder. If there is none Maven uses <Maven installation folder>/conf/settings.xml, which has everything commented out by default.
See also Settings Reference, Quick Overview:
There are two locations where a settings.xml file may live:
The Maven install: ${maven.home}/conf/settings.xml
A user’s install: ${user.home}/.m2/settings.xml
You can use custom settings files with the command line options:
-s,--settings
Alternate path for the user settings file
-gs, --global-settings
Alternate path for the global settings file
And, BTW, .m2 is not the Maven home. It's the folder for user-specific configuration and local repository. I corrected the question's title acordingly.

Maven local repository setting being overridden

My local maven build has been failing due to plugin dependencies not resolving. I've found on SO that this is likely caused by the correct settings not being picked up. So I ran mvn clean install -X to check which settings files are being used. The relevant part of the output from debug is:
[DEBUG] Reading global settings from c:\Maven\apache-maven-3.2.5\conf\settings.xml
[DEBUG] Reading user settings from h:\.m2\settings.xml
[DEBUG] Using local repository at C:\Maven\apache-maven-3.2.5\repo
The local repository is defined in both the global and local settings.xml as:
<localRepository>C:\.m2\repository\</localRepository>
I've been able to build successfully by overriding the local repository via the command line:
mvn clean install -Dmaven.repo.local=my/local/repo
My M2_HOME environment variable is C:\Maven\apache-maven-3.2.5
The command line fix is fine as a workaround but it's not ideal. The local repository is not being defined in the pom.xml.
Other than the settings file, is there anywhere else that the local repository can be defined, and is therefore overriding the configuration in settings.xml?
There are three (and a half) ways how you can configure your local repo:
Globally in <MAVEN_HOME>/conf/settings.xml
per User in <USER_HOME>/.m2/settings.xml
per command line arg: mvn clean install -Dmaven.repo.local=my/local/repo
per environment Variable MAVEN_OPTS: You can set your MAVEN_OPTS to set the maven.repo.local variable for maven operations. It's kind of like passing the environment variable directly, but for all mvn calls. See Is there a way to tell maven where to place the artifacts?
The MAVEN_OPTSway should be what you're looking for.

Change Maven settings at build time

I would like to deploy my Maven build to an Artifactory repo. This repo requires authentication, but I would prefer not modifying my settings.xml file. Is there a way to provide the credentials at build time? I know that you can set properties in the POM with the -D switch:
mvn clean package -Dmy.prop=blah
Is there a way to do something similar to provide the contents of a <server> block in the settings.xml file?
You can use the Maven Artifactory plugin, which accepts credentials in pom file and use the -D as intended.
And, of course, you'll get the full build-info support :)
You can prepare separate settings.xml file for build purpose and use this by -s options.
Eg.
mvn -s build_settings.xml clean deploy

Resources