简体   繁体   English

使用Maven外部化SCM凭证

[英]Externalising SCM credentials with Maven

Is there a method to externalize my SCM credentials so they are not stored in the project's POM? 有没有一种方法可以将我的SCM凭据外部化,以便它们不存储在项目的POM中? The problem being if they're contained in the project's POM, they will be visible to all when the project is deployed. 问题在于,如果它们包含在项目的POM中,则在部署项目时所有用户都可以看到它们。

For some SCM providers you can specify your credentials in the <servers> section of settings.xml . 对于某些SCM提供程序,您可以在settings.xml<servers>部分中指定您的凭据。 As an <id> use the domain name of your repository. 使用存储库的域名作为<id> This works for me with mercurial. 这对我有用。 SubVersion works too. SubVersion也可以。

For example, given my pom.xml contains: 例如,给定我的pom.xml包含:

<scm>
  <connection>scm:hg:http://jukito.googlecode.com/hg/</connection>
  <developerConnection>scm:hg:https://jukito.googlecode.com/hg/</developerConnection>
  <url>http://code.google.com/p/jukito/source/browse/</url>
</scm>

Then I can specify my credentials in settings.xml as such: 然后,我可以这样在settings.xml指定我的凭据:

<server>
  <id>jukito.googlecode.com</id>
  <username>philippe.beaudoin</username>
  <password>1234567890ABC</password>
</server>

I realise that this question is old and the answers is accepted, but for the sake of completeness would like to provide the following alternative, which might be preferred in certain cases. 我意识到这个问题已经过时,可以接受了,但是为了完整起见,我想提供以下替代方案,在某些情况下可能更可取。

Motivation 动机

It so sometimes happens that the same server is used for several purposes, which requires different user credentials. 有时会发生同一服务器用于多种用途的情况,这需要不同的用户凭据。 For example, it is used for hosting Nexus-driven Maven repository as well as an SVN server that have different credentials for the same user. 例如,它用于托管Nexus驱动的Maven存储库以及具有相同用户凭据的SVN服务器。 In such cases, there would need to be several <server> entries with the same id and username , but different password . 在这种情况下,需要有多个<server>条目,它们具有相同的idusername ,但具有不同的password This, however, is not viable. 但是,这是不可行的。

Solution

Maven release plugin supports flags username and password that can be provided as part of the release:prepare and release:perform commands. Maven发行插件支持标志usernamepassword ,这些标志可以作为release:preparerelease:perform命令的一部分提供。 Values for username and password should match the respective SVN credentials. usernamepassword值应与相应的SVN凭据匹配。

mvn release:prepare -Dusername=[username] -Dpassword=[password] 

This can be done for many of the SCM providers, I assume Subversion as the implementation based on your tag. 对于许多SCM提供程序而言,都可以做到这一点,我假设Subversion是基于您的标记的实现。

You can define your Subversion settings in $user.home/.scm/svn-settings.xml (or [maven home]/conf/.scm/svn-settings.xml, though this means they'll still be visible to users of the system) 您可以在$ user.home / .scm / svn-settings.xml(或[maven home] /conf/.scm/svn-settings.xml)中定义Subversion设置,尽管这意味着它们仍然对以下用户可见系统)

In that file you can set your username and password for the Subversion server. 在该文件中,您可以设置Subversion服务器的用户名和密码。 The file contents should look like this: 文件内容应如下所示:

<svn-settings>
  <user>[svn user]</user>
  <password>[svn password]</password>
</svn-settings>

You can define some other properties in that configuration file. 您可以在该配置文件中定义其他一些属性。 For more details see the "Provider Configuration" section of Subversion SCM page . 有关更多详细信息,请参见Subversion SCM页面的“ Provider Configuration”部分。

Other SCM providers have a similar approach, for example in CVS the settings are stored in cvs-settings.xml in the same location (the .scm folder), see the CVS SCM page for details. 其他SCM提供程序也采用类似的方法,例如,在CVS中,设置存储在cvs-settings.xml中的同一位置(.scm文件夹)中,有关详细信息,请参见CVS SCM页面

I've managed to get this to work by using the solution provided above ie adding a new server configuration to my settings.xml and giving the domain name as the id. 我已经设法通过使用上面提供的解决方案来使它工作,例如,将新的服务器配置添加到我的settings.xml并提供域名作为ID。

<server>
  <id>domain.name:port</id>
  <username>[username]</username>
  <password>[password]</password>
</server>

In addition to the previous answer, if you are specifying a port in your URL this also need to be included in the server.id. 除了上一个答案外,如果您在URL中指定端口,则还需要将其包含在server.id中。 Another quirk that I found is that you need to put the password in quotes if it contains any characters that might interfere with the command line call. 我发现的另一个奇怪之处是,如果密码包含任何可能会干扰命令行调用的字符,则需要将其置于引号中。

You cannot use the following in your $user.home/.scm/svn-settings.xm as it is not valid! 您不能在$ user.home / .scm / svn-settings.xm中使用以下内容,因为它无效! There are no such elements as <user> and <password> under <svn-settings> (as specified here ) 不存在这样的元素作为<user><password><svn-settings> (如指定此处

<svn-settings>
  <user>[svn user]</user>
  <password>[svn password]</password>
</svn-settings>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM