Jose Martinez
Jose Martinez

Reputation: 11992

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?

Upvotes: 51

Views: 64815

Answers (3)

Bruno Ribeiro
Bruno Ribeiro

Reputation: 6197

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.

Upvotes: 105

Adam
Adam

Reputation: 657

You can pass the URL including the credentials like

mvn deploy -DaltReleaseDeploymentRepository=myrepo::https://user:pass@server/repo

see https://maven.apache.org/plugins/maven-deploy-plugin/deploy-mojo.html

Upvotes: 1

Sarfaraz Khan
Sarfaraz Khan

Reputation: 2186

You can pass values from command line

mvn -Dvar=someValue -Dtest.username=xyz install

In the POM file, you can refer to system variables (specified on the command line, or in the pom) as ${var}, and environment variables as ${env.myVariable} i.e,${test.username}

You can also refer to the sure-fire plugin doc

Upvotes: 0

Related Questions