Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why add the Maven local repository as a distributionManagement in the POM?

Tags:

maven

Does adding this repository entry to a pom.xml make sense?

...
<distributionManagement>
    <repository>
        <id>dev</id>
        <name>Local repository</name>
        <url>file://${user.home}/.m2/repository</url>
    </repository>
...

I always thought the local repo would be included by default anyways.

I am aware of the fact this won't build on Windows. I found the entry above in a pom.xml at my company.

like image 576
Hubert Grzeskowiak Avatar asked Jan 17 '17 10:01

Hubert Grzeskowiak


People also ask

What is the use of distributionManagement in Maven?

In case of the project managed in Maven, package repository in which artifact is stored, should be specified using <distributionManagement> tag of pom. xml. The mvn deploy command uploads the artifact with HTTP PUT for the URL specified using <distributionManagement> tag.

Why do we need local repository in Maven?

A repository in Maven holds build artifacts and dependencies of varying types. There are exactly two types of repositories: local and remote: the local repository is a directory on the computer where Maven runs. It caches remote downloads and contains temporary build artifacts that you have not yet released.

What is distributionManagement tag in Maven?

Where as the repositories element specifies in the POM the location and manner in which Maven may download remote artifacts for use by the current project, distributionManagement specifies where (and how) this project will get to a remote repository when it is deployed.


1 Answers

There are several concerns here. No, it does not make sense to do that, but not because it is included by default—it's not—but because it's a very wrong thing to do.

Keep in mind that <distributionManagement> configures where Maven will deploy and release your artifacts. With this bit of configuration, Maven will release artifacts into your local repository; this is problematic:

  • Your local repository is supposed to be a cache of downloaded and installed dependencies, it can be removed and deleted entirely at any time; everything should still work (Maven will just re-download the dependencies). In fact, it isn't surprising to have clean builds on CI systems that have a distinct local repository per build, so that each build is completely independent from the others (and does not rely on previously downloaded artifacts).
  • When you will deploy your artifacts, it'll be deployed on your local machine, unavailable to anyone else. This is something useful for testing purposes (as some fake remote repository), but when you will produce your actual final artifacts, you want them to be globally available (for example on a company artifact manager, on Central, etc.).

In short, this is the difference between your local repository, used when installing and downloading, and a remote repository, used when deploying or releasing. They can be confused because the remote repository can very well be a file-based repository (like the local repository), but their semantics are really different. See also in the Maven docs:

The local repository refers to a copy on your own installation that is a cache of the remote downloads, and also contains the temporary build artifacts that you have not yet released.

Remote repositories refer to any other type of repository, accessed by a variety of protocols such as file:// and http://. These repositories might be a truly remote repository set up by a third party to provide their artifacts for downloading [...]. Other "remote" repositories may be internal repositories set up on a file or HTTP server within your company, used to share private artifacts between development teams and for releases.

You should remove that configuration entirely, and use mvn clean install if you really want to put artifacts into the local repository.

like image 57
Tunaki Avatar answered Oct 09 '22 07:10

Tunaki