Stale SNAPSHOT Artifacts Resolved Despite Remote Repository Updates
0 reputation · 26 Jul 2022, 04:37 UTC
0 reputation · 26 Jul 2022, 04:37 UTC
Maven's local repository caches SNAPSHOT dependencies according to the updatePolicy interval defined in settings.xml or the POM. The default daily interval means a remote SNAPSHOT artifact can be rebuilt and deployed multiple times before Maven rechecks the repository. This creates a window where builds resolve a stale cached version instead of the latest deployed artifact.
The tension centers on the maven-metadata.xml timestamp comparison that Maven performs during resolution. When the local metadata timestamp falls within the update policy window, Maven skips the remote check entirely, even if the remote artifact's checksum has changed. This behavior is by design for performance, but it introduces non-determinism across developer machines and CI agents that may have cached different SNAPSHOT builds.
Teams relying on frequent SNAPSHOT deployments need a reliable mechanism to ensure resolution consistency without globally disabling caching via -U, which degrades build performance and increases repository load.
updatePolicy be scoped to specific repositories or dependencies rather than applied globally?-U refresh?Maven decides whether to contact a remote repository for a SNAPSHOT based on the timestamp in the locally cached maven-metadata.xml and the updatePolicy for that repository. If the local metadata timestamp is still within the policy window (e.g., daily or a custom interval), Maven skips the remote HTTP request entirely. During this skip it does not verify the artifact’s checksum, even if the remote artifact has been redeployed with new content. Consequently a build can resolve an older cached SNAPSHOT while a newer version already exists on the server.
updatePolicy is configured per <repository> (or <pluginRepository>) in settings.xml or the POM, not globally.-U (force update all SNAPSHOTs) or manual deletion of the artifact directory under ~/.m2/repository.<checksumPolicy>) only applies when Maven actually downloads the artifact; it is ignored when the timestamp policy says the cached metadata is fresh.settings.xml (or the POM) and set a short interval for the repository that hosts the SNAPSHOTs, leaving other repositories at a longer interval.
<settings>
<profiles>
<profile>
<id>snapshot‑profile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>internal-snapshots</id>
<url>https://repo.example.com/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:30</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
</settings>
maven-metadata.xml on each deployment – if your repository manager does not refresh the metadata timestamp, Maven will never see a change regardless of policy. Ask your admin: “Does the snapshot repository update the maven-metadata.xml timestamp whenever a new SNAPSHOT is deployed?” If the answer is no, fix the repository manager configuration first; otherwise proceed.always or interval:1) only on the CI agent’s settings.xml. This limits extra traffic to CI builds while developer machines retain a reasonable interval.
maven-dependency-plugin to purge just that SNAPSHOT:
mvn dependency:purge-local-repository \
-DmanualInclude=com.example:my-artifact \
-DreResolve=false
This removes only the selected artifact’s version‑specific directory, forcing a fresh download on the next build.
versions-maven-plugin to rewrite the POM with a timestamped SNAPSHOT (e.g., 1.0-20261019.123456-7) and commit that version. This guarantees reproducibility without relying on update policies.
mvn versions:use-latest-snapshots -DprocessAllModules
If your repository manager does not update the maven-metadata.xml timestamp on each SNAPSHOT deployment, the above policy changes will not help. In that case you need to adjust the repository manager’s deployment behavior before changing Maven settings.
Use comments to ask for clarification. Post a solution as an answer.
34,275 reputation · 26 Jul 2022, 05:34 UTC
In settings.xml you can give each repository its own <updatePolicy>. For example:
<repository>
<id>snapshots</id>
<url>https://repo.example.com/snapshots</url>
<snapshots>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
This lets you enforce a strict ‘always’ policy only for the frequently redeployed snapshot repo while keeping the default daily policy for others.
To refresh a single snapshot without a full -U, run:
mvn dependency:purge-local-repository -DmanualInclude='groupId:artifactId'
This removes the artifact and its maven‑metadata.xml, forcing Maven to pull the latest version.
Maven verifies a checksum only when it fetches fresh metadata. If the remote artifact changes but the metadata timestamp stays the same, the stale local copy is kept.