maven

How to pass Maven settings via environment vars


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?


Solution

  • Yes, you can do this in two ways:

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

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