How to get the values of server defined in the settings.xml to use them in my pom.xml?

穿精又带淫゛_ 提交于 2021-02-07 06:13:26

问题


I know I can retrieve some settings.xml parameters using properties, like, for example ${settings.localRepository} to get the location of the local repository.

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>

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


回答1:


I don't think it's possible and believe it would be a bad idea to expose the value of these properties.

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.

EDIT: I'm thinking again about this and what I said is not true.

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.

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).

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.




回答2:


In case you are on Maven 3+, this can be achieved with servers-maven-extension. Once registered, settings.xml/servers content can be referenced using ${settings.servers.server.<server id>.<property>} (e.g. ${settings.servers.server.server-dev.username}).

disclosure: I am the maintainer of the project.




回答3:


I don't know of a simple way to do this. But you can write a small plugin and bind it to an early phase, then access the settings values from within the plugin and either use them directly in your plugin, or expose them as properties.

You can see how to read values from the settings by looking at the source of the nexus-maven-plugin, and how to set them by looking at the properties-maven-plugin




回答4:


Possibility 1 would be using the GMaven plugin. You can easily access the server settings like:

<source>
    def server = settings.servers.find{ it.id.equals('server-hom') }

and then put it into the common properties

    project.properties.srvuser = server.username
</source>

In the following phases you cann access the properties from the POM like always:

${srvuser}

Possibility 2 would be defining properties in the settings.xml like shown here. It is not taken from the server settings, but from some profile based properties.

Anyway I find Maven should pull in all the server settings by default.




回答5:


As described in:

Sonatype: Complete Reference 5.5.2

Best way is:

<settings>
    <profiles>
        <profile>
            <activeByDefault>true</activeByDefault>
            <properties>
                <environment.type>prod</environment.type>
                <database.password>m1ss10nimp0ss1bl3</database.password>
            </properties>
        </profile>
    </profiles>
</settings>


来源:https://stackoverflow.com/questions/1559179/how-to-get-the-values-of-server-defined-in-the-settings-xml-to-use-them-in-my-po

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!