Java 是否可以在命令行中的 Maven Deploy 中传递密码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28071697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:34:37  来源:igfitidea点击:

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

javamavenpom.xml

提问by stef52

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

这是它目前的工作方式,它是Maven Deploy Plugin Usage

pom.xml

pom.xml

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

settings.xml

设置.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

我想要实现的是找到一种在命令行中输入用户名和密码的方法。达到mvn deploy -someUser -somePassword

采纳答案by Robert Scholte

The settings.xmlis 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-pluginhas no option to pass them via commandline. However, I've seen hacks like:

settings.xml被认为是个人的,所以对于这个原因,用户名+密码存储在(用户)settings.xml。所以一般来说,没有理由将它们作为参数传递。(顺便说一句,密码可以在这里加密存储)maven-deploy-plugin无法通过命令行传递它们。但是,我见过这样的黑客:

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

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

现在你可以做 -Dinternal.repo.username=someUser

回答by Viacheslav Shalamov

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

我将在这里列出完整的解决方案,但基本上 Robert Scholte 的解决方案非常有效。

In your ~/.m2/settings.xmlyou should have the following

在你的~/.m2/settings.xml你应该有以下

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

and then you just

然后你就

mvn -Drepo.login=someUser -Drepo.pwd=SomePassword clean install

mvn -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.login=$REPO_LOGIN -Drepo.pwd=$REPO_PWD clean install

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

回答by aloraine

This also works:

这也有效:

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