Maven
Software Development
Coding Tips
Programming Tools
Java Development

How to force Maven to update dependencies (mvn clean install -U)

Codemia Knowledge Hub ·

Run mvn clean install -U to force Maven to check remote repositories for updated snapshots and for releases that previously failed to download. The -U flag (short for --update-snapshots) bypasses Maven's local cache timestamps and pulls the latest versions from your configured repositories.

Why Maven serves stale dependencies

Maven downloads dependencies once and stores them in your local repository at ~/.m2/repository. It tracks when it last checked for updates using metadata files. By default:

  • Release dependencies are downloaded once and never checked again (they are considered immutable).
  • SNAPSHOT dependencies are checked once per day (controlled by the updatePolicy setting in your repository configuration).

This breaks down when a SNAPSHOT has been republished, a corrupted download is stuck in the local cache, or you need to verify the build against the latest artifacts.

The -U flag

bash
mvn clean install -U
  1. clean deletes the target/ directory, removing all previous build output.
  2. install compiles, tests, packages, and installs the artifact into your local repository.
  3. -U forces Maven to check all remote repositories for updated SNAPSHOT versions, ignoring the daily update interval.

To resolve dependencies without building:

bash
mvn dependency:resolve -U

Clearing the local cache

When -U is not enough (for example, a corrupted JAR or POM in the cache), delete the cached artifacts:

bash
1# Delete a specific artifact
2rm -rf ~/.m2/repository/com/example/my-library
3
4# Nuclear option: delete the entire local repository
5rm -rf ~/.m2/repository

The next mvn install re-downloads everything. Deleting all of ~/.m2/repository is slow on projects with hundreds of dependencies, so target specific artifacts when you can.

Purging with the dependency plugin

The dependency:purge-local-repository goal deletes the specified artifacts from ~/.m2/repository and immediately re-downloads them:

bash
1# Purge a specific artifact and re-resolve it
2mvn dependency:purge-local-repository \
3  -DmanualInclude=com.example:my-library
4
5# Purge all project dependencies and re-resolve
6mvn dependency:purge-local-repository -DreResolve=true

Forcing updates in IDEs

IntelliJ IDEA

  1. Open the Maven tool window (View > Tool Windows > Maven).
  2. Click the Reload All Maven Projects button (circular arrows icon).
  3. For a forced update, go to File > Invalidate Caches / Restart, then reimport the project.

Or check Always update snapshots under File > Settings > Build, Execution, Deployment > Build Tools > Maven, which makes IntelliJ check for updated snapshots every time it syncs the Maven project, much like passing -U.

Eclipse

  1. Right-click the project in Package Explorer.
  2. Select Maven > Update Project (or press Alt+F5).
  3. Check the Force Update of Snapshots/Releases checkbox.
  4. Click OK.

VS Code (with Maven for Java extension)

Open the command palette (Ctrl+Shift+P / Cmd+Shift+P) and run Maven: Update Project. To force updates, edit your settings.json:

json
{
  "maven.executable.options": "-U"
}

Setting updatePolicy instead of passing -U

Configure updatePolicy in your pom.xml or settings.xml to control when Maven checks for updates:

xml
1<repositories>
2    <repository>
3        <id>my-snapshots</id>
4        <url>https://repo.example.com/snapshots</url>
5        <snapshots>
6            <enabled>true</enabled>
7            <updatePolicy>always</updatePolicy>
8        </snapshots>
9    </repository>
10</repositories>
Update PolicyBehavior
alwaysCheck for updates on every build
daily (default)Check once per day
interval:NCheck every N minutes
neverNever check for updates (only use local cache)

always has the same effect as passing -U on every build.

Command reference

CommandWhat It DoesWhen to Use
mvn clean install -UFull rebuild with forced snapshot updatesSNAPSHOT dependencies may have changed
mvn dependency:resolve -UResolve dependencies only, no buildQuick dependency refresh
mvn dependency:purge-local-repositoryDelete and re-download cached artifactsCorrupted JARs, checksum failures
mvn versions:display-dependency-updatesShow available newer versionsPlanning upgrades
mvn versions:use-latest-versionsAuto-update dependency versions in pom.xmlAutomated dependency upgrades
mvn -o clean installBuild in offline mode (no network)Working without internet, using only cached deps
mvn -C clean installStrict checksum checkingDetecting corrupted downloads

Removing .lastUpdated markers after a failed download

When Maven cannot resolve a dependency, it writes a .lastUpdated marker file in the local repository, which stops it retrying for a period. If the dependency becomes available later, Maven may still refuse to check:

bash
1# Find and remove .lastUpdated markers for a specific artifact
2find ~/.m2/repository/com/example -name "*.lastUpdated" -delete
3
4# Then retry
5mvn clean install -U

This often confuses people when a dependency was temporarily unavailable.

When -U does not fix it

Release dependencies. -U only forces updates for SNAPSHOT dependencies. Release versions (like 3.2.1) are immutable to Maven. To re-download one, delete it from ~/.m2/repository manually.

CI with a shared cache. CI builds that share a Maven cache can serve stale SNAPSHOT artifacts. Pass -U in CI or set updatePolicy to always for snapshot repositories.

Auto-updating versions without testing. Running mvn versions:use-latest-versions blindly can introduce breaking changes. Run the full test suite afterwards and review changelogs for major version bumps.

-U versus -u. The flag is uppercase -U. Lowercase -u is not a valid Maven option and causes an error.

Network proxies. If -U still fails to resolve dependencies, check ~/.m2/settings.xml for proxy configuration. Corporate networks often require proxy settings for Maven to reach remote repositories.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms