Maven
Software Development
Coding Tips
Programming Tools
Java Development

How to force maven update?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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

Why Maven Caches Dependencies

Maven downloads dependencies once and stores them in your local repository at ~/.m2/repository. To avoid hitting remote servers on every build, Maven 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 caching behavior is efficient, but it causes problems when a SNAPSHOT dependency has been republished, a corrupted download is stuck in the local cache, or you need to verify that your build works with the latest artifacts.

The -U Flag: Force Update Snapshots

bash
mvn clean install -U

This command does three things:

  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.

If you only need 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), you can delete the cached artifacts manually:

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

After deleting, the next mvn install re-downloads everything from remote repositories. Deleting the entire ~/.m2/repository is slow on large projects (hundreds of dependencies), so target specific artifacts when possible.

Using the Dependency Plugin to Purge

Maven's dependency:purge-local-repository goal provides a more controlled approach:

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

This deletes the specified artifacts from ~/.m2/repository and immediately re-downloads them.

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.

Alternatively, add -U to the Maven runner parameters:

File > Settings > Build, Execution, Deployment > Build Tools > Maven > Runner > VM Options: add -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"
}

Maven Update Policy Configuration

For more control over when Maven checks for updates, configure the updatePolicy in your pom.xml or settings.xml:

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)

Setting updatePolicy to always in your repository configuration has the same effect as passing -U on every build, without needing to remember the flag.

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

Handling Resolution Failures

When Maven cannot resolve a dependency, it creates a .lastUpdated marker file in the local repository. This marker prevents Maven from retrying the failed resolution 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 is a common source of confusion when a dependency was temporarily unavailable and Maven stops trying to download it.

Common Pitfalls

Using -U for release dependencies. The -U flag only forces updates for SNAPSHOT dependencies. Release versions (like 3.2.1) are considered immutable by Maven. If you need to re-download a release artifact, delete it from ~/.m2/repository manually.

Forgetting -U in CI/CD pipelines. CI builds that use a shared Maven cache across builds may serve stale SNAPSHOT artifacts. Either pass -U in CI or configure updatePolicy: always for snapshot repositories.

Auto-updating versions without testing. Running mvn versions:use-latest-versions blindly can introduce breaking changes. Always run your full test suite after updating dependency versions, and review changelogs for major version bumps.

Confusing -U with -u. The flag is uppercase -U. Lowercase -u is not a valid Maven option and will cause an error.

Network proxies blocking resolution. If -U does not resolve dependencies, check your ~/.m2/settings.xml for proxy configuration. Corporate networks often require proxy settings for Maven to reach remote repositories.

Summary

Force a Maven update with mvn clean install -U to bypass the local dependency cache and pull the latest SNAPSHOT artifacts. For corrupted cache entries, use dependency:purge-local-repository or manually delete artifacts from ~/.m2/repository. Configure updatePolicy in your repository settings for permanent control over update frequency. In IDE environments, use the built-in Maven refresh tools with the force-update option checked. The -U flag is your primary tool for SNAPSHOT freshness, but remember it does not affect release dependencies.


Course illustration
Course illustration

All Rights Reserved.