77

I’m using Maven 3.1.1. In one of my projects, I reference another one of my projects …

<dependencies>
    <dependency>
        <groupId>org.mainco.subco</groupId>
        <artifactId>myprojectA</artifactId>
        <version>${project.version}</version> 
    </dependency>
</dependencies>

The above is dependent on a couple other of my projects. However, when I run “mvn clean install,” Maven attempts to download these artifacts instead of just using what’s in my local repository. How do I get Maven to only download things if they do not exist in my local repository? Here’s the output of what I’m seeing …

davea$ mvn clean install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building subco admin Module 57.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://download.java.net/maven/2/org/mainco/subco/myprojectA/57.0.0-SNAPSHOT/maven-metadata.xml
Downloading: http://download.java.net/maven/2/org/mainco/subco/subco/57.0.0-SNAPSHOT/maven-metadata.xml
Downloading: http://download.java.net/maven/2/org/mainco/subco/projectB/57.0.0-SNAPSHOT/maven-metadata.xml
Downloading: http://download.java.net/maven/2/org/mainco/subco/projectC/57.0.0-SNAPSHOT/maven-metadata.xml
[INFO]
banan3'14
  • 3,810
  • 3
  • 24
  • 47
Dave
  • 15,639
  • 133
  • 442
  • 830
  • 2
    Just because most of the answers do not specifically mention this, the download of artifacts is different for snapshots and released artifacts. Released artifacts will typically be downloaded only once (per repository), and not refreshed. Snapshots on the other hand are supposed to be refreshed. This is one of the reasons while having released artifacts is a good idea, not only for reproduceability, but also because it improves caching. – eckes Nov 15 '19 at 17:46

6 Answers6

128

If you use offline flag it will use your libraries from local repo.

mvn clean install -o 
jayalalk
  • 2,382
  • 3
  • 17
  • 14
  • 4
    Is there any way to make this the default behavior -- in other words, it will scan the local repos first before going out to the Internet? – Dave Feb 24 '14 at 15:00
  • This is already the default. In this case you have SNAPSHOT's which will be checked every day (default). – khmarbaise Feb 24 '14 at 16:26
  • 3
    This gives me an error: [ERROR] Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved: Cannot access central (https://repo.maven.apache.org/maven2) in offline mode and the artifact org.apache.maven.plugins:maven-install-plugin:jar:2.4 has not been downloaded from it before. – Stepan Yakovenko Nov 02 '18 at 02:34
  • Maven actually checks for SNAPHOT's every time it builds, not just every day – Brain Jun 27 '22 at 08:21
39

You may control the update frequency by configuring repositories in the $USER_HOME/.m2/settings.xml file. Specifically, change the updatePolicy to a value that results in less frequent updates.

This Stackoverflow answer has more detail.

user944849
  • 14,524
  • 2
  • 61
  • 83
  • Just to add that default update policy is 1 day (for SNAPSHOT versions). Cite from Maven [configuration](https://maven.apache.org/settings.html) page: **updatePolicy**: This element specifies how often updates should attempt to occur. Maven will compare the local POM’s timestamp (stored in a repository’s maven-metadata file) to the remote. The choices are: _always_, _daily_ (default), _interval:X_ (where X is an integer in minutes) or _never_. – Svilen Oct 21 '21 at 13:18
14

If you want to update some jars but not the snapshots of locally installed ones you should use the -nsu (--no-snapshot-updates) flag to prevent Maven from fetching the latest snapshot from the main repository. Using -o will prevent it from fetching other upgrades and (often) essential maven jars from remote repositories.

Ian Turton
  • 10,018
  • 1
  • 28
  • 47
3

In my experience, none of that works once maven has "decided" that it must download the file from an specific server.

Configure updatePolicy a other suggest, but in order to suceed, you should go to the folder inside the local repository where the jar is, and delete a file named "_maven.repositories". Delete also al files ending in ".lastUpdated". Also "m2e-lastUpdated.properties" if you are using eclipse plugin.

Rober2D2
  • 580
  • 3
  • 8
  • This did the trick for me! Our project has one oracle jdbc lib that isn't available from Central but has to be downloaded from the Oracle website (thereby acknowledging their license) and which has to be put into the local repo. But Maven insisted on downloading this thing until I removed all the mentioned files (which had been created when using the same lib for a different project). After that the build finally continued and succeeded. – mmo Nov 27 '21 at 22:16
1

You can setup repositories in the

settings.xml

file of your maven install.

Read more about it on the Maven configuration webpage.

Leo
  • 64
  • 3
  • Specifically, the `updatePolicy` value for each repository should be adjusted. From the sound of it, the value is currently 'always', you probably want to set it to something that results in less frequent updates. – user944849 Feb 24 '14 at 16:26
  • Yeah that updatePolicy seems to be what was causing it. If you want to create an answer to that effect, I'll accept it. – Dave Feb 24 '14 at 16:54
0

I know this sounds ridiculous ;-) But one way to do it is to set the DNS entry for the maven repositories to localhost, so it fails 100% since the host can't be resolved.

/etc/hosts

# Comment this in/out as needed.
127.0.0.1 download.java.net

Note if you don't want to use localhost, you could use one of the IPs mentioned here, for example, 254.0.0.1. These IPs are reserved for future use, and thus unused.

Brad Parks
  • 66,836
  • 64
  • 257
  • 336