Although Maven documentation has a whole page on their password encryption feature, it doesn’t actually tell you how to do what you need to do to encrypt Maven passwords.
What am I talking about?
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
.
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>
Enter fullscreen mode Exit fullscreen mode
Obviously, storing a password in a clear-text file like this is foolish.
How to encrypt and replace the password
To remedy this issue, follow these steps:
Create a master password
- First, you must create a master password that is used to encrypt all the other Maven passwords. Start by opening a terminal.
- Type:
$ mvn --encrypt-master-password
- You will be prompted for a master password. Enter the password here.
- Maven will spit out a big long string like this:
{w5+NYEttGTAHV3FanFoel4N5uUmbcvtzRoWZHI5N97jtssbo0O/93W/XLlm0caeM}
Keep this terminal window open while you do the next step.
Store the master password
- Create a file called settings-security.xml in the ~/.m2 directory.
- Copy/paste the following block into the new file:
<settingsSecurity> <master></master> </settingsSecurity>
- Copy/paste the big long encrypted string that Maven spit out in the previous steps in between the <master> tags. You’ll end with something like this:
<settingsSecurity> <master>{w5+NYEttGTAHV3FanFoel4N5uUmbcvtzRoWZHI5N97jtssbo0O/93W/XLlm0caeM}</master> </settingsSecurity>
- Save the security-settings.xml file, obviously!
Encrypt your password
- 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.
- Type:
$ mvn --encrypt-password
- Enter the password you want to encrypt (in our fake example scenario, it’s ‘notMyRealPasswordForAnything’).
- Maven will spit out an encrypted string that looks similar to the encrypted string it spit out for the master password.
- Copy the new string it spit out.
- Open the settings.xml file.
- Delete the current password between the <password> tags.
- Paste in the new encrypted version.
- Save the file.
- Verify that Maven can still access the repo in question.
You’re all done! Smart!
暂无评论内容