Maven local repository setting being overridden - maven

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.

Related

Setting dynamic password from Maven

We've started using an AWS CodeArtifact Maven repository. Before connecting, developers must run a command ("aws") to acquire a temporary access token. How can I configure Maven to acquire and use this token automatically, without requiring running a script, configuring an IDE or changing the simple command line "mvn install"?
Maybe there's a trick I'm overlooking.
The token must be "interpolated" in the element <server>/<password> in settings.xml. This file will pre-process either an environment variable ({$env.token}) or a Java system property ({$token}). It will not process a project property.
The repository password cannot be specified within the POM file.
The settings.xml file cannot use project properties.
The exec-maven-plugin cannot set an environment variable in the parent process (Windows). It can write to a file.
The properties-maven-plugin cannot set a dynamic system property (e.g., from a file or script output).
The surefire plugin binds to the test phase and forks a JVM.
The Windows setx command does not affect the running process.
UPDATE: Maven downloads repository metadata before the first phase, so no plugin will solve the problem.

Maven & proxy settings

I'm using the Maven template from To Be Continuous in order to build and deploy into Gitlab Packages.
My corporate network requires proxy to access Maven registry (Maven central) but my runner has direct access to Gitlab.
I set the proxy preferences via http_proxy (and related) var env.
The TBC template values the JVM system properties.
The mvn package succeed, but mvn deploy fails.
I add the proxy settings inside settings.xml.
The mvn package succeed, but mvn deploy fails.
My understanding: one plugin detects the proxy but ignores the exclusions.
I add a step to unset the var env just before mvn command, keeping the JVM system properties set by TBC template.
The both commands succeed.
What's wrong?
Edit: related to an issue: https://gitlab.com/to-be-continuous/maven/-/issues/31

jenkins is not using local maven repository

I have few jars which I have installed in my local maven repo(in windows under user/.m2). While building the project from command-line it's perfectly downloading the jars and packaging it.
Now I have created a Jenkins job (mvn clean package) to do so, but while running the jobs it's not picking up those jars from local repo instead trying to download it from central repo.
I tried all possible solutions available in Internet but still no luck. Can you please how I can configure Jenkins so that it should download those jars from my local repo ?
I also tried :-
1. offline mode
2. gave local repo path in settings.xml
3. use of nexus/artifactory in not an option for me
Seems maven executed by Jenkins not used the settings.xml in your local
you can try to change the maven command/goal in Jenkins job configuration with one more option:
-f <your local settings.xml path>
you can copy the settings.xml to your source code folder for debug purpose, after prove it work, then consider how to prepare the settings.xml for jenkins job with below options:
Option 1 use Config file provider plugin
Click Config Files -> Add a New Config -> Maven settings.xml
Change name and set your xml into content field:
In your job configuration, click the Advance... button in Build Section, choose your added settings.xml as below:

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

Specifying Maven's local repository location as a CLI parameter

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

Resources