Maven

Maven Local Repository example

In this example we are going to see some of the capabilities from the maven local repository.

Maven is a build automation tool used mainly for java projects from apache.

We are going to see some examples of the capabilities of the maven local repository.

For this example we use the following technologies:

  • MAC OSX
  • Eclipse Mars.1
  • Maven3
  • JDK 1.8.0_65 64bits

1. Introduction

Maven uses spaces for store artifacts and dependencies. Those spaces are called repositories. There are two types of repositories: Remotes and Locals.

Remote repositories are accesed in differentes ways (http, ftp, etc…) and contains artifacts and dependencies provided by a third party, one example of this kind of repositories is repo.maven.apache.org which is maven central repository.

Local repositories are copies of a remote repository inside your own installation and acts as a cache. It also can have the copy of your not-deployed-yet artifacts and dependencies.

The structure of both local and remote repositories is the same, there are not differencies between them.

2. Local repository structure

The local repository by default is located under the .m2/repository folder under the user home folder. Inside of it you will find all the artifacts and dependecies organized in folders for each group_id, artifact_id and version.

You can see the typical structure of a local repository in the following image

Local repository structure
Local repository structure

Each folder usually has the jar file, the pom file and meta-files that allow maven to manage the repository status. The files would vary depending on the type of the artifact/dependency.

3. Deploying artifacts to the local repository

You can deploy artifacts to the local repository, launching the mvn install command, after running it, you can go to the local repository and search a folder for your group_id, navigate down inside the folder structure and you will find a folder with the artifact version, inside of it you will see the artifact itself.

You can find more information here

4. Installing artifacts/dependencies in the local repository

You can install a jar into your local repository that is not managed by maven and is not under any other remote repository.

Use the following command in order to do that

command:

mvn install:install-file -Dfile=./jar_file-1.0.0.jar -DgroupId=your_group -DartifactId=your_artifact -Dversion=1.0.0 -Dpackaging=jar -DgeneratedPom=true

After running the command, you will find inside the local repository a folder called your_group/your_artifact/1.0.0 and inside of it the jar file, the associated pom and all the related meta-file.

  • You can find more information here.

5. Maven locate artifacts strategy

Maven will always try to find an artifact or dependencies into the local repository first, in order to improve the building process. If you do not have internet connectivity or you are under a very poor internet connection you should install your dependencies/artifacts in your local repository as described in points 3 and 4.

If you are in a organization when only one machine has internet connection you can download all the artifacts/dependencies needed in order to build your project in that machine and then zip the local repository and distribute it among all your team members, so all of you can work offline without problems.

6. Conclusions

As we have saw the maven local repository is an important part of maven, and it acts as a cache for remote artifacts and dependencies. The maven local repository allow you to work offline once you have downloaded all the needed artifacts and dependencies into your local repository.

Francisco Hernandez

JEE technologies geek and development engineer. I have over 13 years of experience as software engineer in JEE architectures: Design, development, improvement etc. Currently I work as software architect and consultant. I am mainly involved in projects related to the bank and energy sectors based on Java technologies and Oracle products. I am also very interested in open-source projects
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button