04 - Maven Repositories

 

            4.1 Types of repositories

                                    4.1.1 Local repository

                                                4.1.1.1 Setting the local repository

                                    4.1.2 Central repository

                                                   4.1.2.1 Mirrors

                                    4.1.3 Remote repository

            4.2 Distributed development

Maven repositories are folders used to store artifacts resulted from development work.

Storing Maven artifacts (jars and poms) in specific repositories is preferable to storing them in CVS or SVN (control versioning systems) because jars are binary files and these types of environments handle text files better and updates are faster.

Maven repositories have multiple advantages:

  • are open source and free
  • provide repository browser

-easy to set up and use

  • backup facility
  • create, edit and delete subdirectories
  • active community support

-are deployable on a standard web server.

4.1 Types of repositories

There are three types of repositories:

          - local

         - central

         - remote

4.1.1 Local repository

Local repositoryis the folder from the developer machine or build system where Maven stores project’s dependencies:

         - plugins

         - jar

         - other files downloaded by Maven.

 During the build of a project all the dependency files will be stored in the local repository.

The default Maven repository is .m2 folder in %USER HOME% directory.

       Unix/Mac OS X - ~/.m2

Windows - C:\Documents and Settings\{your username}\.m2

In order to install a project artifact into the local repository the following command line has to be executed:

       mvn install

This command builds the project and copies the packaged jars into the Maven local repository. The command triggers all the build phases before install but we will discuss at the next chapters about the build phases.

4.1.1.1 Setting the local repository

The default local repository can be changed from .m2 to another location folder.

If another folder will be used as local repository then in {M2_HOME}\conf\setting.xml file it is necessary to change the localRepositorytag  and its content.

<settings>
<!--localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ~/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
  --

       <localRepository>C:/Temp</localRepository>

Save the file and open the directory selected for local repository after Maven has downloaded the dependencies to it.

 

4.1.2 Central repository

When the project is built, Maven checks the pom.xml file and determines which dependency to download.

First of all Maven gets the dependency from the local repository and in case that is not found Maven try to get it from the default repository - central repository.

       http://repo1.maven.org/maven2/

The site is not available and finally it can be redirected to http://search.maven.org/.

Big open source organizations Apache, Eclipse, JBoss and the other open source organizations publish their components to the Maven Central Repository. Open source components should be found normally to the central repository of Maven.

Using resources from the Central Repository is free, popular and secure. The resources from this repository are available.

The Central Repository is managed by the Maven community.

If a project is published to the Central Repository the individuals have to respect some basic requirements regarding the quality of the projects metadata.

4.1.2.1 Mirrors

Because repositories are declared inside a project (custom repositories) the persons who   sharethe project can get the right settings from the Maven files. The mirror is an alternative which can be used for a particular repository without changing the project settings files.

Mirrors are used because:

    ▪       Exist a synchronized mirror on the internet that is geographically closer and faster

    ▪       A particular repository should be replaced with an internal repository with a greater control over

    ▪       A repository manager should provide a local cache to a mirror and it need to use its URL instead

A mirror of a given repository can be configured in the settings file (${user.home}/.m2/settings.xml), giving the new repository its own id and url, and specify the mirror of the setting that is the ID of the repository you are using a mirror of. For example, the ID of the main Maven Central US repository included by default is central, so to use the European Central instance, you would configure the following:

<settings>
  ...
<mirrors>
<mirror>
<id>UK</id>
<name>UK Central</name>
<url>http://uk.maven.org/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
  ...
</settings>

4.1.3 Remote repository

Projects have unique dependencies that are not available in the Maven Apache Central Repository.Local repositories cannot be used every time from the team members to download manually new libraries.

To prevent such situations Maven provides the solution of centralized Remote Repository where all the dependencies need to be made maintained and available. The missing dependencies are automatically installed on the local repositories from the remote repository.

The Remote Repository is a repository located on a web server similar to Central Repository from which Maven can download dependencies. It can be located on the local network of a company or anywhere on the internet.

This repository is used usually for hosting the internal projects of a company (organization) which then are shared among other internal projects. For example projects containing common utilities tools for programming (date, string parsing), security access, printing modules, object model etc. These projects are not public and not accessible outside the company and cannot be hosted on the public central repository.

The Remote Repository can be configured in POM after the tag <dependencies>

Repositories and their location:

4.2 Distributed development

People from different geographical locations are working together in distributed cross functional teams. In order to make distributed software development more efficient it is necessary to enforce some practices.

One important practice for starting is setting up:

  • remote repository
  • continuous integration server
  • issue tracking system
  • mailing list systems

All the above elements have to be configured correctly.

For a developer other following steps are:

-updating the code from revision control system

  • pick up task / issue

-writing tests for the task / code  for implementing

  • run local build , run tests
  • update code
  • committing the code

At the end of development iteration, the developer needs to be able to package his project and deploy it to a test, an acceptance and eventually a production server.

Maven helps all this process to be successful on a distributed environment because it can work on a large scale. Maven design makes our builds much more stable and robust.

Like us on Facebook