19

It's possible to set the maven local repository in settings.xml:

<localRepository>${user.home}/.m2/repository</localRepository>

And it's possible to set the maven local repository on the command line:

mvn clean install -Dmaven.repo.local=repository

Is it possible to specify within the pom.xml itself?

Note: I'd like a way to specify, in the pom.xml, where maven initially searches for artifacts (by default, ~/.m2/repository) and where maven installs artifacts via mvn install (by default, ~/.m2/repository).

3
  • The first example is exactly what is used as default. If you like you can also use the second one give the parameter for the location... and to answer your question: No it is not possible to define the location of the local cache in the pom file..The question is why would you like to change it?
    – khmarbaise
    Aug 5, 2018 at 9:39
  • 2
    No, the question is how to change it - why isn't relevant :)
    – drew
    Aug 7, 2018 at 17:15
  • It is relevant to understand your motivation and of course get an idea of what you are trying to do or what kind of problem you are trying to solve. This helps and maybe I can suggest a better solution for your problem If I know the why. But If you don't like to be helped Ok..It is up to you....
    – khmarbaise
    Aug 7, 2018 at 18:15

2 Answers 2

22

According to the Maven POM Reference and the Guide to using multiple repositories, you can specify repositories in pom.xml too.

There are two different ways that you can specify the use of multiple repositories. The first way is to specify in a POM which repositories you want to use

And according to Introduction to repositories, you can use the file:// protocol in <url>.

Remote repositories refer to any other type of repository, accessed by a variety of protocols such as file:// and http://.

So the following works:

<project>
  ...
  <repositories>
    <repository>
      <id>example-repo</id>
      <name>Example Repository</name>
      <url>file://path/to/your/local/repository</url>
    </repository>
  </repositories>
</project>

Edit:

Based on your comment and edit, you need to override the default repository and Maven home directory in pom.xml.

I've found a topic about disabling central repository, and tried out the answers, but Maven still uses the values from settings.xml. This answer in another thread explains why:

settings.xml allows you to override definitions in pom.xml, not the other way round.

So it's seems it is not possible to override the default mechanism from pom.xml, Maven will search for dependencies in repositories configured in settings.xml and will install to Maven home specified in that file.

2
  • 3
    Even with a repository set as above, maven will still, by default, download artifacts from and install artifacts to ~/.m2/repository - IIRC, the above simply specifies a backup to download from and a target to deploy to. I'm looking to change - in the pom.xml - where the system initially searches for artifacts and uses as the destination fro the mvn install command.
    – drew
    Aug 4, 2018 at 15:12
  • @juzraai The definition of a repository has nothing to do with the definition of a local cache apart from that it does not make sense to define a repository on file base....
    – khmarbaise
    Aug 5, 2018 at 9:40
2

If the problem is having to write the options every time when running maven, you can use a feature available since version 3.3.1 that allows you to set command line options in your project (or parent project of your module). Read @Brice's answer: https://stackoverflow.com/a/48583079

So with this feature in mind, you can achieve a similar result by setting up a new settings.xml with the <localRepository> pointing to the location you desire and use maven.config in your project to make maven use the new settings.xml, which by the way can be anywhere you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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