Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Maven settings via environmental vars

Tags:

maven

In our setting.xml file we have the following:

<servers>
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
</servers>

Would it be possible to pass those settings (or their equivalent) via environmental variables instead of the settings.xml?

like image 215
Jose Martinez Avatar asked Jul 06 '15 16:07

Jose Martinez


People also ask

How do you pass an environment variable in Maven?

Add M2_HOME, M2, MAVEN_OPTS to environment variables. Set the environment variables using system properties. Open command terminal and set environment variables. Open command terminal and set environment variables.

Do we need to set environment variables for Maven?

Note that if you want to use Maven, you need to have Java installed and an environment variable set up. Open Google and search for maven download.

How do I change settings xml in Maven?

Navigate to path {M2_HOME}\conf\ where M2_HOME is maven installation folder. Open file settings. xml in edit mode in some text editor. Update the desired path in value of this tag.


1 Answers

Yes, you can do this in two ways:

  • passing properties in the command line, using variables. For example, you can use in your settings.xml something like this:
<servers>
    <server>
      <id>deploymentRepo</id>
      <username>${server.username}</username>
      <password>${server.password}</password>
    </server>
</servers>

And in the command line, pass these variables in this way:

mvn clean package -Dserver.username=yourusername -Dserver.password=yourpassword

Please note that passing password as command-line options is a security issue and therefore prefer the second option.

  • exporting environments properties. For example, if you export (in Linux, something like export SERVER_USERNAME=yourusername) SERVER_USERNAME and SERVER_PASSWORD variables, you can use like this:
<servers>
    <server>
      <id>deploymentRepo</id>
      <username>${env.SERVER_USERNAME}</username>
      <password>${env.SERVER_PASSWORD}</password>
    </server>
</servers>

For more information about properties, see the reference documentation.

like image 59
Bruno Ribeiro Avatar answered Oct 19 '22 10:10

Bruno Ribeiro