How to Encrypt your Maven Password
Tags: maven, java, devops
Published: 2018-09-10 16:30:56

---
title: How to Encrypt your Maven Password
published: true
description: Most people store the password to their organization's Maven repository in plain text in their settings file. Use these easy steps to encrypt it!
tags: maven,java,devops
---

Although Maven documentation has a whole page on <a href="https://maven.apache.org/guides/mini/guide-encryption.html" rel="noopener" target="_blank">their password encryption feature</a>, it doesn't actually tell you how to do what you need to do to encrypt Maven passwords.

<strong>What am I talking about?</strong>
If you have authentication to Maven repos in your organization, you normally store the username and password in the Maven settings file located by default at `~/.m2/settings.xml`. 

<!--moreFor example, I might have something like this in my settings.xml . . . -->

For example, I might have something like this in my settings.xml:

```
<servers>
    <server>
      <id>myorg-internal-repo</id>
      <username>scott.shipp</username>
      <password>notMyRealPasswordForAnything</password>
    </server>
</servers>
```

Obviously, storing a password in a clear-text file like this is foolish.

<img src="http://code.scottshipp.com/wp-content/uploads/2017/09/carpenter_1_md.gif" alt="carpenter_1_md" width="476" height="640" class="aligncenter size-large wp-image-2644" />

<strong>How to encrypt and replace the password</strong>
To remedy this issue, follow these steps:

<em>Create a master password</em>
<ol>
<li>First, you must create a master password that is used to encrypt all the other Maven passwords. Start by opening a terminal.</li>

<li>Type: <pre>$ mvn --encrypt-master-password</pre></li>

<li>You will be prompted for a master password. Enter the password here.</li>

<li>Maven will spit out a big long string like this:

<pre>
{w5+NYEttGTAHV3FanFoel4N5uUmbcvtzRoWZHI5N97jtssbo0O/93W/XLlm0caeM}
</pre>
</li>
</ol>

Keep this terminal window open while you do the next step.

<em>Store the master password</em>
<ol>
<li>Create a file called settings-security.xml in the ~/.m2 directory.</li>

<li>Copy/paste the following block into the new file:

<pre>
&lt;settingsSecurity&gt;
  &lt;master&gt;&lt;/master&gt;
&lt;/settingsSecurity&gt;
</pre>
</li>
<li>Copy/paste the big long encrypted string that Maven spit out in the previous steps in between the <strong>&lt;master&gt;</strong> tags. You'll end with something like this:

<pre>
&lt;settingsSecurity&gt;
  &lt;master&gt;{w5+NYEttGTAHV3FanFoel4N5uUmbcvtzRoWZHI5N97jtssbo0O/93W/XLlm0caeM}&lt;/master&gt;
&lt;/settingsSecurity&gt;
</pre>
</li>

<li>Save the security-settings.xml file, obviously!</li>
</ol>



<em>Encrypt your password</em>
<ol>
<li>In the given example, the settings.xml server entry has a password of 'notMyRealPasswordForAnything'. This is what we want to encrypt. So open a terminal if you aren't already in one.</li>

<li>Type:

<pre>
$ mvn --encrypt-password
</pre>
</li>

<li>Enter the password you want to encrypt (in our fake example scenario, it's 'notMyRealPasswordForAnything').</li>

<li>Maven will spit out an encrypted string that looks similar to the encrypted string it spit out for the master password.</li>

<li>Copy the new string it spit out.</li>

<li>Open the settings.xml file.</li>

<li>Delete the current password between the <strong>&lt;password&gt;</strong> tags.</li>

<li>Paste in the new encrypted version.</li>

<li>Save the file.</li>

<li>Verify that Maven can still access the repo in question.</li>
</ol>

You're all done! Smart!