40

In the command line help, I see that maven "checks" for updates:

-U,--update-snapshots                  Forces a check for updated
                                       releases and snapshots on remote
                                       repositories

However, most questions on Stack Overflow imply that this option forces Maven to update. Does this mean it forces a re-download of the dependencies?

e.g. https://stackoverflow.com/a/9697970/1119779

Community
  • 1
  • 1
imagineerThat
  • 5,293
  • 7
  • 42
  • 78

3 Answers3

45

If you do not use -U, maven might cache results - even if a dependency could not be found previously (e.g. because your nexus [or alike] was unavailable, misconfigured, didn't contain the dependency [yet] or whatever). SNAPSHOT versioned jars are cached similarly.

If that's the case. Maven follows the repository's updatePolicy, which tells it how often (if ever) maven checks if a dependency has been updated (in the case of SNAPSHOT), or has become available, in the case of a released version. Default is daily therefore if a temp error causes maven to not download a dependency, it might take one day before maven tries again. -U overwrites that and tells it to check now.

-U does not re-download a SNAPSHOT dependency if it has already been downloaded and if the checksum is the same! It only checks for the checksum.

Update: as @Stas pointed out, if the checksum differs, it will re-download and override you local JARs with the ones from the remote repository.

** -U also checks for "updated" release versions if you specify a "version" range etc.

BTW: Maven uses a timestamp file that has the same name as the dependency + ".lastUpdated" to know when a dependency has been last checked on which server. E.g. ~/.m2/repository/org/springframework/spring-webmvc/3.1.2.RELEASE/spring-webmvc-3.1.2.RELEASE.jar.lastUpdated

Example for updatePolicy:

<repositories>
  <repository>
    <releases>
      <enabled>false</enabled>
      <updatePolicy>always</updatePolicy>
    </releases>
    <snapshots>
      <enabled>true</enabled>
      <updatePolicy>never</updatePolicy>
    </snapshots>
    <!-- ... -->
  </repository>
  <!-- ... -->
</repositories>

See http://maven.apache.org/pom.html#Repositories for further information about the updatePolicy.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Alexander
  • 2,925
  • 3
  • 33
  • 36
  • 1
    To speed things up, I'd like to switch to using mvn -U to update our local repo rather than deleting our m2 repo before every build. Can I trust that this doesn't alter the behavior of our builds? – imagineerThat Mar 20 '15 at 17:14
  • 3
    It does not update your whole local m2 repo, but it forces checks if there are any updates to those maven dependencies that your project uses. BTW: this should only be the case for SNAPSHOT dependencies -- released maven artifacts should not changed after they have been released! – Alexander Mar 22 '15 at 17:07
  • `maven might cache results` what kind/type of results? – Kanagavelu Sugumar Jul 01 '20 at 12:02
  • dependencies that your project relies on – Alex Jul 22 '20 at 08:24
  • For maven 2.2.1 Java 1.7_171_64, mvn -U only updates SNAPSHOTS, no releases (i.e. version: a.b.c), beside the reference documentation. So for this case this answer is wrong. – abentan Oct 20 '20 at 12:55
  • @Alexander, are you saying Maven would cache a not found result even if the reply was an HTTP 503 Service Unavailable? And if so, isn't that a bug? – Philippe Cloutier Mar 07 '23 at 23:03
6

It's important to add that executing mvn -U will override your local SNAPSHOT jars with remote SNAPSHOT jars.

Without -U argument, local SNAPSHOTS won't be override.

Johnny
  • 14,397
  • 15
  • 77
  • 118
  • Agree with maven 2.2.1, mvn -U doesn't update releases, only SNAPSHOTS. – abentan Oct 20 '20 at 12:57
  • 2
    This is incorrect! As already mentioned in the accepted answer `-U` WILL update them, but at the time the update policy settings are specifying such an update. To prevent local snapshot artifacts from getting overwritten by remote ones, you have to specify -o to not download anything at all. – Marvin Emil Brach Dec 09 '20 at 02:35
-1

when you will get https://repo.spring.io/milestone was cached in the local repository, resolution will not be reattempted until the update interval of spring-milestones has elapsed or updates are forced

in that case you have to use mvn clean package -U