177

I’m using Maven 3.3.3 with Java 8 on Mac Yosemite. I have a multi-module project.

    <modules>
            <module>first-module</module>
            <module>my-module</module>
                …
    </modules>

When I build my one of my child modules, for example, “my-module” from above, using “mvn clean install”, the build attempts to download the child module artifacts from a remote repository I have defined in my ~/.m2/settings.xml file. Output is below

[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building my-module 87.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml
Downloading: http://download.java.net/maven/2/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml
Downloaded: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml (788 B at 0.9 KB/sec)
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-module-87.0.0-20151104.200545-4.pom

How do I force Maven to check my local ~/.m2/repository first before trying to download from the remote repositories? Below is where I have my remote repositories defined in my ~/.m2/settings.xml file …

<profile>
    <id>releases</id>
    <activation>
        <property>
            <name>!releases.off</name>
        </property>
    </activation>
    <repositories>
        <repository>
            <id>releases</id>
            <url>https://my.remoterepository.com/nexus/content/repositories/releases/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</profile>
<profile>
    <id>snapshots</id>
    <activation>
        <property>
            <name>!snapshots.off</name>
        </property>
    </activation>
    <repositories>
        <repository>
            <id>snapshots</id>
            <url>https://my.remoterepository.com/nexus/content/repositories/snapshots/</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
</profile>

Edit: In response to the answer saying that the download occurs when the artifact is not there, below is the terminal output in which I prove the file was there in my repo but Maven is trying to download it anyway ...

Daves-MacBook-Pro-2:my-module davea$ ls -al ~/.m2/repository/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-module-87.0.0-SNAPSHOT.jar 
-rw-r--r--  1 davea  staff  10171 Nov  5 10:22 /Users/davea/.m2/repository/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-module-87.0.0-SNAPSHOT.jar
Daves-MacBook-Pro-2:my-module davea$ mvn clean install
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for org.mainco.subco:my-module:jar:87.0.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-antrun-plugin @ org.mainco.subco:my-module:[unknown-version], /Users/davea/Documents/sb_workspace/my-module/pom.xml, line 678, column 12
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building my-module 87.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml
Downloading: http://download.java.net/maven/2/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml
Downloaded: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml (788 B at 0.8 KB/sec)
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-module-87.0.0-20151106.043202-8.pom
Downloaded: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-   module-87.0.0-20151106.043202-8.pom (3 KB at 21.9 KB/sec)
Downloading: http://download.java.net/maven/2/org/mainco/subco/subco/87.0.0-SNAPSHOT/maven-metadata.xml
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/subco/87.0.0-SNAPSHOT/maven-metadata.xml
3
  • 4
    Maven does check your local repository before trying to download an artifact from a remote repository. Are you sure your local had these artifacts before you attempted this build? You can inspect your local repository now and try another build again anyway. Also, you can specify where your local repository is in the settings.xml (see here). Commented Nov 5, 2015 at 15:35
  • 1
    Although I haven't specified my repository in my settings.xml file, it is the default that Maven set up for me -- ~/.m2/repository . Do I have to specify it even when it's the default?
    – Dave
    Commented Nov 5, 2015 at 22:39
  • 3
    For me there are other files in my local repositories, such as *.sha1 or *.lastUpdate. delete other files except *.jar and *.pom will prevent maven from re-download file from remote repository
    – Harun
    Commented Sep 27, 2017 at 8:33

11 Answers 11

80

The dependency has a snapshot version. For snapshots, Maven will check the local repository and if the artifact found in the local repository is too old, it will attempt to find an updated one in the remote repositories. That is probably what you are seeing.

Note that this behavior is controlled by the updatePolicy directive in the repository configuration (which is daily by default for snapshot repositories).

3
  • 20
    Is it possible to "overwrite" this by console command argument? Like: mvn clean install -FORCE_COMMAND
    – Naxos84
    Commented Nov 8, 2016 at 7:01
  • 33
    "Too old" meaning what? It picks the newest snapshot it can find, whether that's local or remote? That would be horrible behaviour surely. If I've just built a snapshot, I really want to use that one, not the one that the CI build has built just a moment later. Commented Sep 5, 2017 at 14:18
  • 5
    Please explain more about what "Too old" means?
    – Bằng
    Commented Nov 28, 2019 at 14:27
72

Use mvn --help and you can see the options list.

There is an option like -nsu,--no-snapshot-updates Suppress SNAPSHOT updates

So use command mvn install -nsu can force compile with local repository.

3
  • 24
    You can also use -o for "offline" maven behavior
    – Sean
    Commented Nov 22, 2017 at 17:51
  • 11
    The -nsu option doesn't prevent mvn to try and fail to download an artifact remotely if it has not yet been downloaded instead of using the local build like when the artifact was built and installed locally. – Commented Jan 16, 2018 at 16:42
  • I had the same problem with an artifact not being downloaded yet and this fixed it for me: maven.apache.org/general.html#importing-jars Commented Oct 17, 2018 at 7:30
38

To truly force maven to only use your local repo, you can run with mvn <goals> -o. The -o tells maven to let you work "offline", and it will stay off the network.

5
  • 11
    The -o option doesn't prevent mvn to try and fail to download an artifact remotely if it has not yet been downloaded instead of using the local build like when the artifact was built and installed locally. Commented Jan 16, 2018 at 16:40
  • 2
    that's true - if you don't have a local copy, you're out of luck. This is useful for projects you have built before, but potentially have recent SNAPSHOT changes that you don't care about.
    – Sean
    Commented Jan 16, 2018 at 20:50
  • Or even if you never built the project you could prepare for it being built offline with mvn dependency:go-offline
    – Aldian
    Commented Jan 26, 2018 at 12:59
  • What if you have a local copy that you just installed (so it's never deployed/available in remote snapshot repo)? Commented Apr 17, 2018 at 18:09
  • 1
    Ah, I just created a pom project with a collection of dependencies, but didn't specify <type>pom</type> in the consuming project, so it was looking for a jar (and of course couldn't find it, even in offline mode) Commented Apr 17, 2018 at 18:59
13

Follow below steps:

    1. Ensure to delete all the contents of the jar folder located in your local except the jar that you want to keep.
      For example files like .repositories, .pom, .sha1, .lastUpdated etc.
    1. Execute mvn clean install -o command

This will help to use local repository jar files rather than connecting to any repository.

2
  • 3
    Removing only *.repositories and *.sha1 files worked for me
    – DLight
    Commented Mar 25, 2020 at 16:58
  • 6
    Doesn't work for me. It says "Could not resolve dependencies for project XXX: Cannot access xxxx (https:/xxxx.com/repository/maven/) in offline mode and the artifact xxxx:test:war:1.0.0 has not been downloaded from it before.
    – Alex
    Commented Feb 10, 2021 at 12:00
7

In my case I had a multi module project just like you. I had to change a group Id of one of the external libraries my project was depending on as shown below.

From:

<dependencyManagement>
    <dependency>
        <groupId>org.thirdparty</groupId>
        <artifactId>calculation-api</artifactId>
        <version>2.0</version>
        <type>jar</type>
        <scope>provided</scope>
    </dependency>
<dependencyManagement>

To:

<dependencyManagement>
   <dependency>
      <groupId>org.thirdparty.module</groupId>
        <artifactId>calculation-api</artifactId>
        <version>2.0</version>
        <type>jar</type>
        <scope>provided</scope>
    </dependency>
<dependencyManagement>

Pay attention to the <groupId> section. It turned out that I was forgetting to modifiy the corresponding section of the submodules that define this dependency in their pom files.

It drove me very crazy because the module was available locally.

1
  • 1
    thx so much bro, was pulling my hair out , b/c I was using the parent POM groupid info (its also multi module) to import into my other project.... You're post made me realize my error....thank you so much reducing pain on your fellow human race <3 Commented Nov 16, 2021 at 3:51
7

Even when considering all answers above you might still run into issues that will terminate your maven offline build with an error. Especially, you may experience a warning as follwos:

[WARNING] The POM for org.apache.maven.plugins:maven-resources-plugin:jar:2.6 is missing, no dependency information available

The warning will be immediately followed by further errors and maven will terminate.

For us the safest way to build offline with a maven offline cache created following the hints above is to use following maven offline parameters:

mvn -o -llr -Dmaven.repo.local=<path_to_your_offline_cache> ...

Especially, option -llr prevents you from having to tune your local cache as proposed in answer #4.

Also take care that parameter <localRepositoryin> in your settings.xml points to the correct folder of your local Maven repository.

4
  • didn't have localRepository attribute in my settings.xml.... thanks bro Commented Nov 16, 2021 at 2:48
  • @ThoR Your answer was helpful. Just remember that not everyone has the same configuration as you. Setting my local repo as you show would break maven, lol. Rather than your last code snippet, just say something like, "point <localRepository> in your settings.xml file to the correct folder".
    – cb4
    Commented Dec 5, 2021 at 6:50
  • 1
    @cb4 You're right. The code snippet was to specific as it focused on a user specific settings.xml and repository folder. Thank you for the hint. Instructions adapted.
    – ThoR
    Commented Dec 7, 2021 at 12:31
  • I wanted to add, for my situation, using maven 3.9.6, the -llr option is no longer available. However, adding the -Dmaven.repo.local=<path_to_your_offline_cache> without the offline flag did the trick Commented Jun 17 at 16:33
6

Maven always checks your local repository first, however,your dependency needs to be installed in your repo for maven to find it.

Run mvn install in your dependency module first, and then build your dependent module.

2
  • 2
    I have edited my question to show that the artifact is in the repo (notice the "ls -al" command I run. Yet Maven is attempting to download it anyway. Any other ideas?
    – Dave
    Commented Nov 6, 2015 at 14:45
  • 6
    @Oliver: you are missing the fact that for artifacts in the local repository, Maven may still consult the remote repositories if the artifact is a snapshot that is too old. Commented Nov 7, 2015 at 14:55
2

I had the exact same problem. Running mvn clean install instead of mvn clean compile resolved it. The difference only occurs when using multi-maven-project since the project dependencies are uploaded to the local repository by using install.

1

I faced the same problem. For me, it was the SNAPSHOT text missing from pom.xml. Looks like, if we want to force maven to build from Local, we need to provide -SNAPSHOT:

<dependency>
    <groupId>com.ABC</groupId>
    <artifactId>QRS</artifactId>
    <version>1.2.65-SNAPSHOT</version>
</dependency>
0

use <classpathentry kind="var" path="M2_REPO/your jar location without creating any environment veriable

1
  • 1
    Hi, thanks for contributing to StackOverflow. Please consider adding more details to your answer before posting. Also, remember to use proper styling. Your answer also seems incomplete. Please edit it or you may incur in downvotes and forced deletion.
    – alelom
    Commented Mar 12, 2021 at 15:30
0

just to give my 2 cents. For me, it was only necessary to find the jar inside the ~/.m2/repository directory and use its version. So, after using

cd local-dependency/
mvn install

the local-dependency jar will be in the .m2/repository/com/your-organization/local-dependency/

~$ tree ~/.m2/repository/com/your-organization/local-dependency/
/home/felipe/.m2/repository/com/your-organization/local-dependency/
├── 0.1-SNAPSHOT
│   ├── maven-metadata-local.xml
│   ├── _remote.repositories
│   ├── local-dependency-0.1-SNAPSHOT.jar
│   ├── local-dependency-0.1-SNAPSHOT.pom
│   └── resolver-status.properties
└── maven-metadata-local.xml

then use it as a local dependency

<dependency>
   <groupId>com.your-organization</groupId>
   <artifactId>local-dependency</artifactId>
   <version>0.1-SNAPSHOT</version>
</dependency>

Not the answer you're looking for? Browse other questions tagged or ask your own question.