Is it possible to pass a password in Maven Deploy in the command line?
Asked Answered
M

4

59

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

Microcurie answered 21/1, 2015 at 16:5 Comment(0)
M
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

Moulin answered 21/1, 2015 at 19:0 Comment(9)
I don't know if I'd call that a hack, a core feature of Maven is to allow variable substitutionGraecoroman
It was something I was trying to prevent but it does get the job done.Microcurie
About <username>${internal.repo.username}</username>: do you mean adding it to settings.xml or pom.xml?Automatic
I think variable substitution is not possible for settings.xml so I think you are referring to pom.xmlAutomatic
I'm talking about the settings.xmlMoulin
Interpolation of properties is supported in settings.xml since Maven 3.0, according to maven.apache.org/settings.html#Quick_OverviewOrganzine
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?Suber
@Suber the sourcecode will confirm: no. You'll always need a settings.xml. You might want to write a maven-extension to change that.Moulin
"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.Zeta
P
53

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

Prophase answered 3/5, 2019 at 12:46 Comment(6)
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.Suber
A followup: Can I also parameterize the repo id, e.g, <id>$id</id> (inside settings.xml)?Suber
Answer is "yes" -- repository identifier can also be a parameter.Suber
I've created a convenient docker image for that.foldingstuff.tech/…Dysarthria
Great trick! Thank you. Can you change install with deploy?Blanchette
receiving 'servers.server[0].id' is missing - any idea on how to fix this?Salomo
S
5

This also works:

<server>
  <id>${repo.id}</id>
  <username>${repo.username}</username>
  <password>${repo.password}</password>
</server>
Suber answered 19/9, 2019 at 10:18 Comment(0)
A
0

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
Amalia answered 19/9, 2023 at 5:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.