Interview Prep/Build Tools (Java, Node.js, Python, .NET)/Java builds (Maven, Gradle, JAR, WAR)

What is the ~/.m2 folder and what is settings.xml used for?

BeginnerConceptJava builds (Maven, Gradle, JAR, WAR)
Answer

Short answer

~/.m2 is Maven's home folder on a machine. ~/.m2/repository is the local cache of every downloaded dependency, so they don't have to be downloaded again. ~/.m2/settings.xml holds machine/user settings that shouldn't live in the project: repository mirrors (like Nexus), credentials, proxies and profiles.

Example settings.xml

<settings>
  <mirrors>
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>                  <!-- send ALL downloads through Nexus -->
      <url>https://nexus.company.com/repository/maven-public/</url>
    </mirror>
  </mirrors>
  <servers>
    <server>
      <id>nexus</id>                          <!-- matches the mirror / repository id -->
      <username>${env.NEXUS_USER}</username>
      <password>${env.NEXUS_PASSWORD}</password>
    </server>
  </servers>
  <proxies>
    <proxy><host>proxy.company.com</host><port>3128</port></proxy>
  </proxies>
</settings>

pom.xml vs settings.xml

  • pom.xml — about the project: dependencies, plugins. Committed to Git.
  • settings.xml — about the environment: where to download from, passwords. Kept on the build agent or injected by CI (Jenkins Config File Provider, a secret file in GitHub Actions).

What to say in the interview

"~/.m2/repository is Maven's local dependency cache, which we also cache between CI runs to speed builds up. settings.xml holds environment-specific config like the Nexus mirror and credentials, and in CI we inject it from a secret rather than committing it."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Java apps: Maven, Gradle, JAR & WAR

More on Java builds (Maven, Gradle, JAR, WAR)