Explain the `-o` (offline) and `-U` (update-snapshots) flags. When would you use each, and how do they interact with SNAPSHOT dependencies?
answer
- -o = local cache only, no network
- -U = refresh SNAPSHOTs + retry failures
- releases immutable, snapshots mutable
- default updatePolicy = daily
- -o and -U conflict
basics
~10 s-o runs offline using only the local repository, never reaching the network. -U forces Maven to re-check remote repos for newer SNAPSHOTs (and re-attempt failed downloads) instead of using cached copies.
solid answer
~40 sMaven caches artifacts in the local repository (`~/.m2/repository`). `-o`/`--offline` tells Maven to resolve everything from that local cache and never contact remote repositories — fast and deterministic, but fails if something isn't already cached. `-U`/`--update-snapshots` does the opposite: it forces Maven to check remote repositories for updated **SNAPSHOT** artifacts even if the local copy is within its freshness window, and it also retries dependencies previously marked as 'not found' (cached failures). Released (non-SNAPSHOT) versions are immutable, so `-U` mainly matters for SNAPSHOTs; by default Maven only re-checks SNAPSHOTs once per day per the repository `<updatePolicy>`. Use `-o` in air-gapped or CI-cache scenarios; use `-U` when a teammate just published a new SNAPSHOT and you need it now, or to clear a stale cached resolution failure.
code
bash · 8 lines# Build using only ~/.m2, no network (air-gapped / cached CI)
mvn -o clean package
# Force re-check of remote SNAPSHOTs and retry cached download failures
mvn -U clean install
# Repository updatePolicy in settings.xml controls default snapshot freshness
# <snapshots><updatePolicy>daily</updatePolicy></snapshots>go deeper
Know -o = offline, -U = update snapshots.
Explain snapshot mutability, updatePolicy, and cached failure retry.
Decide cache strategy for CI and diagnose stale-snapshot bugs.
Define repository updatePolicy and offline-cache governance across the build fleet.
## Background: the local repository and SNAPSHOTs Maven downloads dependencies and plugins from **remote repositories** (e.g. Maven Central, a company Nexus/Artifactory) and stores them in the **local repository** at `~/.m2/repository`. Once cached, they're reused. Versions ending in `-SNAPSHOT` (e.g. `1.4.0-SNAPSHOT`) are **mutable, in-development** versions — they can change without the version string changing. Released versions (`1.4.0`) are **immutable** and downloaded once forever. Each repository has an `<updatePolicy>` for snapshots (`daily` by default, also `always`, `never`, `interval:N`). 'daily' means Maven only re-checks a SNAPSHOT remotely once per day; otherwise it trusts the local copy. ## `-o` / `--offline` Forces Maven to work **only from the local repository** — zero network calls. - Pros: fast, deterministic, works on a plane / air-gapped CI. - Cons: build fails immediately if any needed artifact isn't already cached. - Common with a pre-warmed `~/.m2` cache in CI to avoid flaky network and speed up. ## `-U` / `--update-snapshots` Forces Maven to: 1. Re-check remote repos for newer **SNAPSHOT** versions, ignoring the daily update policy. 2. **Retry artifacts previously cached as 'missing'** — Maven remembers resolution failures in `_remote.repositories`/`.lastUpdated` files, and `-U` clears that to try again. It has little effect on release versions (they're immutable, fetched once). ## Interaction `-o` and `-U` are contradictory: `-o` means 'no network', `-U` means 'go to the network and refresh'. If you pass `-o`, `-U` is effectively moot because nothing can be fetched. ## When to use - `-o`: deterministic offline builds, cached CI. - `-U`: a teammate just deployed a new SNAPSHOT you depend on; or you got a spurious 'could not resolve' that you know is now published. ```bash mvn -o package # fully offline mvn -U clean install # force snapshot refresh + retry failed downloads ```
- Does `-U` help when you depend on a release version like 1.4.0?Practically no — release artifacts are immutable and fetched once. `-U` mainly forces SNAPSHOT re-checks (and retries cached failures).
- Why might a freshly published SNAPSHOT not appear in your build without `-U`?The default `daily` updatePolicy means Maven only re-checks the SNAPSHOT remotely once per day; until then it uses the cached copy.
saying these in an interview costs you the question
- Saying `-U` re-downloads release versions (they're immutable).
- Thinking `-o` and `-U` can meaningfully combine.
- Believing SNAPSHOTs are re-checked on every build by default (it's daily).