57

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

stef52
  • 1,089
  • 2
  • 15
  • 23

3 Answers3

54

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

Robert Scholte
  • 11,889
  • 2
  • 35
  • 44
  • 8
    I don't know if I'd call that a hack, a core feature of Maven is to allow variable substitution – Alex Jan 21 '15 at 19:04
  • It was something I was trying to prevent but it does get the job done. – stef52 Jan 23 '15 at 15:12
  • About `${internal.repo.username}`: do you mean adding it to `settings.xml` or `pom.xml`? – Daniel Aug 03 '16 at 20:35
  • I think variable substitution is not possible for `settings.xml` so I think you are referring to `pom.xml` – Daniel Aug 03 '16 at 20:36
  • 1
    I'm talking about the `settings.xml` – Robert Scholte Aug 03 '16 at 20:51
  • Interpolation of properties is supported in settings.xml since Maven 3.0, according to https://maven.apache.org/settings.html#Quick_Overview – Magnus Reftel May 31 '17 at 08:01
  • 7
    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? – aloraine Aug 01 '19 at 17:40
  • @aloraine the sourcecode will confirm: no. You'll always need a `settings.xml`. You might want to write a maven-extension to change that. – Robert Scholte Aug 16 '19 at 14:07
49

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

Viacheslav Shalamov
  • 4,149
  • 6
  • 44
  • 66
  • 1
    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. – aloraine Sep 03 '19 at 21:07
  • A followup: Can I also parameterize the repo id, e.g, $id (inside settings.xml)? – aloraine Sep 18 '19 at 01:57
  • 1
    Answer is "yes" -- repository identifier can also be a parameter. – aloraine Sep 19 '19 at 10:17
  • I've created a convenient docker image for that.https://foldingstuff.tech/simple-maven-deploy-with-credentials-as-arguments/ – Radim Göth Jul 31 '20 at 12:44
  • Great trick! Thank you. Can you change `install` with `deploy`? – dmarrazzo Jan 14 '21 at 13:50
  • receiving 'servers.server[0].id' is missing - any idea on how to fix this? – divine Apr 04 '23 at 09:27
5

This also works:

<server>
  <id>${repo.id}</id>
  <username>${repo.username}</username>
  <password>${repo.password}</password>
</server>
aloraine
  • 106
  • 1
  • 5