How to get server values โ€‹โ€‹defined in settings.xml to use them in my pom.xml?

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

Now imagine that my settings.xml parameter contains the following server 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 any way using the server id to get the value of any parameter? For example, something like ${settings.servers.server.server-dev.username} will return devuser .

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


As for this page , this is not possible. However, since this is incorrect documentation, I still have the hope of doing it this way ...

+7
properties maven-2
source share
5 answers

I do not think this is possible and I think it would be a bad idea to expose the value of these properties.

As explained in the Reference Reference , the <servers> point in settings.xml is to not propagate values โ€‹โ€‹like username or password along with pom.xml . Thus, exposing properties for reading from anywhere, just violates this principle and can be a security problem.

EDIT: I think about it again, and what I said is not true.

AFAIK, Maven does not provide the username and password properties of the server defined in the settings, and / or provides a mechanism similar to the described OP. But it would be wrong to have access to them from pom.xml .

Having said that, as Rich said, the Maven API gives you access to the servers defined in the settings (see org.apache.maven.settings. Settings # getServer (String serverId) ), so you need to set the properties from Mojo (i.e. in the user plugin).

But I'm not really sure what you are trying to do and maybe use <properties> , and profiles are the best approach. For general profiles, it is useful to use common environmental properties (but specific values). Check out chapters 11.5.1. Common Environments and 11.5.2 Secrets Protection Maven: The Definitive Guide book.

+4
source share

If you are on Maven 3+, this can be achieved using servers-maven-extension . After registering, the contents of settings.xml / servers can be referenced using $ {settings.servers.server. <server id>. <property>} (for example, $ {settings.servers.server.server-dev.username}).

disclosure: I am a supporter of the project.

+9
source share

I do not know an easy way to do this. But you can write a small plug-in and bind it to the early phase, then access the parameter values โ€‹โ€‹from the plug-in and either use them directly in your plug-in or set them as properties.

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

+1
source share

Option 1 will use the GMaven plugin . You can easily access the server settings, for example:

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

and then put it in the general properties

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

In the following steps, you cannot access properties from the POM, as always:

 ${srvuser} 

Option 2 will define properties in the settings.xml file, as shown here . It is not taken from the server settings, but from some properties based on the profile.

In any case, I believe that Maven should use all server settings by default.

0
source share

As described in:

Sonata: full link 5.5.2

The best way:

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

All Articles