How do I force Maven to use my local repository rather than going out to remote repos to retrieve artifacts?
Asked Answered
C

11

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
Campanulaceous answered 5/11, 2015 at 15:22 Comment(3)
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).Assorted
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?Campanulaceous
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 repositoryMyiasis
A
79

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).

Aiguillette answered 7/11, 2015 at 14:52 Comment(3)
Is it possible to "overwrite" this by console command argument? Like: mvn clean install -FORCE_COMMANDHeadlong
"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.Trail
Please explain more about what "Too old" means?Tripoli
P
71

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.

Packton answered 3/7, 2017 at 2:49 Comment(3)
You can also use -o for "offline" maven behaviorBarcot
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. –Toni
I had the same problem with an artifact not being downloaded yet and this fixed it for me: maven.apache.org/general.html#importing-jarsUnpopular
B
37

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.

Barcot answered 22/11, 2017 at 17:50 Comment(5)
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.Toni
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.Barcot
Or even if you never built the project you could prepare for it being built offline with mvn dependency:go-offlinePentavalent
What if you have a local copy that you just installed (so it's never deployed/available in remote snapshot repo)?Autotomize
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)Autotomize
C
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.

Coloquintida answered 29/5, 2019 at 10:48 Comment(2)
Removing only *.repositories and *.sha1 files worked for meThirzi
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.Conifer
Y
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.

Ytterbium answered 13/2, 2016 at 10:7 Comment(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 <3Narco
M
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.

Macintyre answered 5/11, 2015 at 15:56 Comment(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?Campanulaceous
@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.Aiguillette
J
6

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.

Jackquelinejackrabbit answered 15/10, 2020 at 12:26 Comment(3)
didn't have localRepository attribute in my settings.xml.... thanks broNarco
@Jackquelinejackrabbit 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".Longship
@Longship 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.Jackquelinejackrabbit
W
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.

Wembley answered 29/9, 2020 at 10:50 Comment(0)
C
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>
Coonan answered 26/4, 2023 at 14:52 Comment(0)
E
0

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

Essy answered 12/3, 2021 at 10:17 Comment(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.Informer
M
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>
Mccormick answered 9/12, 2021 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.