Where does Maven store repository credentials, and what is the basic mechanism for keeping them out of plain text?
answer
- ~/.m2/settings.xml <server>
- id must match repo id
- {encrypted} token
- master in settings-security.xml
- obfuscation not vault
basics
~20 sCredentials go in ~/.m2/settings.xml inside a <server> entry (id, username, password). Maven can encrypt the password so settings.xml holds an {encrypted} value instead of the real one, decrypted at build time using a master password.
solid answer
~40 sMaven reads server credentials from a <server> block in ~/.m2/settings.xml (the user-level settings), matched to a repository by a shared id. By default the password is plain text, which is risky if the file is shared or backed up. Maven's built-in password encryption lets you replace it with an {base64ciphertext} token. That ciphertext is decrypted using a separate master password held in ~/.m2/settings-security.xml. At build time Maven combines the two to recover the real password for HTTP(S) auth to the remote repository. Note this is obfuscation, not strong protection: the master file sits on the same machine. The point is to avoid copy-pasteable plaintext, not to defeat a determined local attacker.
code
xml · 9 lines<settings>
<servers>
<server>
<id>nexus-releases</id>
<username>deploy-bot</username>
<password>{COQLCE6Du7v...}</password>
</server>
</servers>
</settings>go deeper
Knows credentials go in ~/.m2/settings.xml under <server> and can be encrypted.
Knows the id-matching rule and the two-file (settings.xml + settings-security.xml) split.
Frames it as obfuscation vs real secret management and avoids committing secrets.
Drives org policy: vault/CI-injected secrets, no plaintext in repos, machine-scoped master files.
## What problem this solves When Maven downloads from a private repository (e.g. Nexus, Artifactory) or deploys artifacts, it needs a username and password. Those must live somewhere on the developer/CI machine. Storing them as plain text in a config file is dangerous because settings.xml is easy to share, commit, or back up by accident. ## Where credentials live Maven looks in **`~/.m2/settings.xml`** (the per-user settings file; there is also a global one in `$MAVEN_HOME/conf/settings.xml`). Credentials go in a `<server>` element under `<servers>`: - `<id>` — must exactly match the `<id>` of the `<repository>` / `<distributionManagement>` / `<mirror>` it authenticates. - `<username>` and `<password>`. ## The two-file encryption model Maven supports encrypting the password so the stored value is not readable: - **`~/.m2/settings-security.xml`** holds the **master password** (itself encrypted with a fixed internal key, or optionally tied to a relocated file). - **`~/.m2/settings.xml`** holds the **server password as ciphertext** wrapped in curly braces, e.g. `{COQLCE6...}`. At build time Maven decrypts the master password from settings-security.xml, uses it to decrypt the server password, and authenticates. ## Important caveat This is **obfuscation**, not real secret management. Both files sit on the same machine, so anyone with local read access can recover the password. It stops shoulder-surfing and accidental plaintext leaks; it does not replace a vault. ```xml <settings> <servers> <server> <id>nexus-releases</id> <username>deploy-bot</username> <password>{COQLCE6Du7v...}</password> </server> </servers> </settings> ```
- Why must the <server> id match the repository id?Maven matches credentials to a remote by id; if they differ, Maven sends no auth and you get a 401/403.
- Is encrypted settings.xml safe to commit to a shared repo?No. It is local obfuscation only; the master file on the same box can decrypt it. Use a real secret store or per-machine files.
saying these in an interview costs you the question
- Thinking encryption makes the password cryptographically safe to publish
- Putting credentials in the project pom.xml instead of settings.xml
- Assuming the id is free-form and doesn't need to match the repository id