Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent maven to resolve dependencies in local repository

Tags:

maven-2

Is there a way to tell maven (when doing mvn package, mvn site or ...) not to resolve the dependencies from the local repository?

Background of this question: Sometimes I get into problems, when previously cached dependencies (e.g. SomeProject-0.7-ALPHA) are no longer available in the remote repository. In my local build everything still works fine as the dependency has been cached before. As soon as I share my pom with others, they may get into trouble, as they dont have a cached version of that dependency and the dependency can no longer be resolved from the remote repository.

Any help will be appreciated. Thanks in advance!

like image 363
Nils Schmidt Avatar asked May 05 '10 11:05

Nils Schmidt


People also ask

Why does Maven always download dependency?

When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository. It helps to avoid references to dependencies stored on remote machine every time a project is build. Maven local repository by default get created by Maven in %USER_HOME% directory.

Where When does Maven retrieve dependencies from where are they stored locally?

Maven's local repository is a directory on the local machine that stores all the project artifacts. When we execute a Maven build, Maven automatically downloads all the dependency jars into the local repository. Usually, this directory is named . m2.

How does Maven decide which repository to use?

By resolving the most recent version available from the list of repositories. For enterprise development, or even sane versioning, you want to version your dependencies (and use only managed internal repositories so you can control licensing and approvals on third party licenses).


2 Answers

Is there a way to tell maven (when doing mvn package, mvn site or ...) not to resolve the dependencies from the local repository?

No, that's how the whole dependency resolution works (via the local repository).

Sometimes I get into problems, when previously cached dependencies (e.g. SomeProject-0.7-ALPHA) are no longer available in the remote repository.

Sorry for stating the obvious but removing dependencies from a remote repository is an horrible practice and leads to... well the kind of troubles you're facing. If possible, avoid doing that.

As soon as I share my pom with others, they may get into trouble, as they dont have a cached version of that dependency and the dependency can no longer be resolved from the remote repository.

One way to check that things would work for others would be to purge the dependencies of the project you're going to share from your local repository and to re-resolve them. Of course, doing this manually would be really painful but the good news is that the Maven Dependency Plugin has a purge-local-repository goal for that.

mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:purge-local-repository  \
    -DreResolve=true \
    -DactTransitively=true \ 
    -Dverbose=true

If this fails, then you know that something is missing and can't be resolved anymore which is basically what you want to know. But you've also lost it so make a backup of your repository if this matters to you.

like image 69
Pascal Thivent Avatar answered Sep 23 '22 01:09

Pascal Thivent


The basic test of a maven build is to delete the local repository .m2/repository and try to build (mvn package) and see if it is working. If not you have other problems here. As mentioned the problem seemed to be the process you are working with. If a dependency is not available anymore from a remote repository there is an other problem. Are you using a Repository Manager for example Nexus, Archiva, Artifactory ?

like image 33
khmarbaise Avatar answered Sep 23 '22 01:09

khmarbaise