62

This is the way it currently works, and it's the Maven Deploy Plugin Usage

pom.xml

[...] <distributionManagement> <repository> <id>internal.repo</id> <name>MyCo Internal Repository</name> <url>Host to Company Repository</url> </repository> </distributionManagement> [...] 

settings.xml

[...] <server> <id>internal.repo</id> <username>someUser</username> <password>somePassword</password> </server> [...] 

and what I'm trying to achieve is finding a way in which the username and password are typed in at the command line. to achieve mvn deploy -someUser -somePassword

4 Answers 4

57

I'll lay out here the full solution, but basically Robert Scholte's solution works brilliant.

In your ~/.m2/settings.xml you should have the following

<settings> <servers> <server> <id>${repo.id}</id> <username>${repo.login}</username> <password>${repo.pwd}</password> </server> </servers> </settings> 

and then you just

mvn -Drepo.id=myRepo -Drepo.login=someUser -Drepo.pwd=somePassword clean install

You can even use your environment variable (if you are doing that on the remote server/container, for example):

mvn -Drepo.id=$REPO_ID -Drepo.login=$REPO_LOGIN -Drepo.pwd=$REPO_PWD clean install

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! I'm using bitbucket pipelines with a publicly available Docker file to provide build environment, so I want to avoid hardcoding the user name and password into the settings.xml file. The proposed solution above should work great.
A followup: Can I also parameterize the repo id, e.g, <id>$id</id> (inside settings.xml)?
Answer is "yes" -- repository identifier can also be a parameter.
I've created a convenient docker image for that.foldingstuff.tech/…
Great trick! Thank you. Can you change install with deploy?
|
55

The settings.xml is considered personal, so for that reason the username+password are stored in the (user-)settings.xml. So in general there's no reason to pass them as argument. (btw, passwords can be stored encrypted here) The maven-deploy-plugin has no option to pass them via commandline. However, I've seen hacks like:

<username>${internal.repo.username}</username> 

And now you can do -Dinternal.repo.username=someUser

9 Comments

I don't know if I'd call that a hack, a core feature of Maven is to allow variable substitution
It was something I was trying to prevent but it does get the job done.
I'm talking about the settings.xml
I am building a project using bitbucket pipelines and a public Docker image. For various reasons it would be much easier to pass the user name and password via options to mvn, as described in the original post. Is there any way to do this?
"So in general there's no reason to pass them as argument" is not true for CI/CD build process, where the container doing the build/deploy is not your personal machine. The solution offered maybe the best available, but it's a hack that's complicated to implement in tools like CircleCI, where it's easy to securely set environment variables and pass them to commands, but very awkward to compose a settings file.
|
5

This also works:

<server> <id>${repo.id}</id> <username>${repo.username}</username> <password>${repo.password}</password> </server> 

Comments

2

It is working fine for me, we can define username and password variable mane anything, we choose, but we must use the repositoryId variable for the id in the settings.xml.

It has been tested with maven version "Apache Maven 3.6.3 (Red Hat 3.6.3-13)"

Add this snippet to settings.xml :

<servers> <server> <id>${repositoryId}</id> <username>${repo.login}</username> <password>${repo.pwd}</password> </server> </servers> 

Run this snippet to upload a file to Nexus :

mvn deploy:deploy-file -DrepositoryId=<repoid> -Drepo.login=<username> -Drepo.pwd=<password> -Durl=http://<url of Nuxus>:<port>/repository/<repository space> -Dfile=<file location> -DgroupId=<Group Id> -DartifactId=<artfact id> -Dversion=<version> -Dpackaging=<packaging> -Dclassifier=<classifier> -X 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.