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

MavenRepositoryMaven 3Artifacts

Maven Problem Overview


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

Maven Solutions


Solution 1 - Maven

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

Solution 2 - Maven

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.

Solution 3 - Maven

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.

Solution 4 - Maven

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.

Solution 5 - Maven

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.

Solution 6 - Maven

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.

Solution 7 - Maven

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.

Solution 8 - Maven

The -o option didn't work for me because the artifact is still in development and not yet uploaded and maven (3.5.x) still tries to download it from the remote repository because it's the first time, according to the error I get.

However this fixed it for me: https://maven.apache.org/general.html#importing-jars

After this manual install there's no need to use the offline option either.

UPDATE

I've just rebuilt the dependency and I had to re-import it: the regular mvn clean install was not sufficient for me

Solution 9 - Maven

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.

Solution 10 - Maven

use

Solution 11 - Maven

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaveView Question on Stackoverflow
Solution 1 - MavenAndreas VeithenView Answer on Stackoverflow
Solution 2 - MavenZheng ZhongqiView Answer on Stackoverflow
Solution 3 - MavenSeanView Answer on Stackoverflow
Solution 4 - MavenMadhuView Answer on Stackoverflow
Solution 5 - MavenWillaView Answer on Stackoverflow
Solution 6 - MavenOliverView Answer on Stackoverflow
Solution 7 - MavenThoRView Answer on Stackoverflow
Solution 8 - MavenLuigi CristalliView Answer on Stackoverflow
Solution 9 - MavenGJohannesView Answer on Stackoverflow
Solution 10 - MavenDibyendu AdhikaryView Answer on Stackoverflow
Solution 11 - MavenKnoblauchView Answer on Stackoverflow