如何获取 settings.xml 中定义的服务器值以在我的 pom.xml 中使用它们? [英] How to get the values of server defined in the settings.xml to use them in my pom.xml?
问题描述
我知道我可以使用属性检索一些 settings.xml 参数,例如 ${settings.localRepository}
以获取本地存储库的位置.
I know I can retrieve some settings.xml parameters using properties, like, for example ${settings.localRepository}
to get the location of the local repository.
现在假设我的 settings.xml 包含以下服务器定义:
Now imagine my settings.xml contains the following servers definition:
<servers>
<server>
<id>server-dev</id>
<username>devuser</username>
<password>devpass</password>
</server>
<server>
<id>server-hom</id>
<username>homuser</username>
<password>hompass</password>
</server>
</servers>
有没有办法,给定服务器的 id
来获取任何参数的值?例如,像 ${settings.servers.server.server-dev.username}
这样的东西会返回 devuser
.
Is there a way, given an id
of a server to get the value of any parameter? For example, something like ${settings.servers.server.server-dev.username}
would return devuser
.
我已经尝试了以下方法:
I've already tried the following:
${settings.servers.server.server-dev.username}
${settings.servers.server-dev.username}
${settings.servers.server[server-dev].username}
${settings.servers[server-dev].username}
但他们都没有工作......
but none of them worked...
关于这个页面,这是不可能的.但是,由于它是一个没有正确记录的功能,我仍然希望以这种方式做到这一点......
Regarding this page, this is not possible. However, as it is a feature not correctly documented, I still have some hope to do that in this way...
推荐答案
我认为这是不可能的,并且认为公开这些属性的值是一个坏主意.
I don't think it's possible and believe it would be a bad idea to expose the value of these properties.
如 设置参考 中所述,具有 <
不会与 settings.xml
中的 ;servers>pom 一起分发诸如
.因此,公开属性以从任何地方读取它们只会违反这一原则,并且可能是一个安全问题.username
或 password
之类的值.xml
As explained in the Settings Reference, the point of having <servers>
in the settings.xml
is to not distribute values such as username
or password
along with the pom.xml
. So exposing properties to read them from anywhere would just break this principle and may be a security issue.
我又在思考这个问题,我说的不是真的.
I'm thinking again about this and what I said is not true.
AFAIK,Maven 不会公开设置中定义的服务器的 username
和 password
属性和/或提供类似于 OP 描述的机制.但是能够从 pom.xml
访问它们并没有错.
AFAIK, Maven doesn't expose the username
and password
properties of a server defined in the settings and/or provide a mechanism similar to what the OP described. But it wouldn't be wrong to be able to access them from a pom.xml
.
话虽如此,正如 Rich 指出的那样,Maven API 使您可以访问设置中定义的服务器(请参阅 org.apache.maven.settings.Settings#getServer(String serverId)) 所以必须可以从 Mojo 设置属性(即在自定义插件中).
Having that said, as Rich pointed out, the Maven API gives you access to the servers defined in settings (see org.apache.maven.settings.Settings#getServer(String serverId)) so it must be possible to set properties from a Mojo (i.e. in a custom plugin).
但我实际上不确定您到底想做什么,也许使用
和配置文件会是更好的方法.处理常见的环境属性(但特定值)是配置文件的一个很好的用例.查看章节 11.5.1.常见环境和11.5.2保护Maven:权威指南一书中的秘密.
But I'm actually not sure of what you're exactly trying to do and maybe using <properties>
and profiles would be a better approach. Dealing with common environment properties (but specific values) is a good use case for profiles. Check out the chapters 11.5.1. Common Environments and 11.5.2 Protecting Secrets of the Maven: The Definitive Guide book.
这篇关于如何获取 settings.xml 中定义的服务器值以在我的 pom.xml 中使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!