Change Maven Local Repo via Command Line

When running Maven builds on shared hosts or ephemeral CI agents, override the local repository directory with a JVM property:

mvn clean install -Dmaven.repo.local=/path/to/cache

Why It Matters

  • Keeps build artifacts out of your default ~/.m2/repository when you need isolation.
  • Speeds up CI by pointing multiple jobs to a warmed cache directory.
  • Helps reproduce customer builds by cloning their repository structure in a scratch folder.

Additional Tips

  • Combine with --fail-at-end when running large multi-module builds to keep the temporary cache populated even if one module fails.
  • Remember to clean up disposable caches to reclaim disk space on build agents.
  • For permanent configuration, use the <localRepository> element in ~/.m2/settings.xml instead of the per-build flag.