Tuesday, July 17, 2012

Maven Repositories and configuration

I have recently started writing about Apache Maven. What I wrote till now and what I am going to write in this post are quite basic stuff about Maven, the must know kind of stuff.


So till now I have written about the Apache Maven - Setting Up and Configuring Maven Local Repository in my previous posts. What I am going to write now is about the different types of repositories in Maven. How you can configure them and some basic theoretical explanation on this. From my point of view, these are some of the basic stuff one should know before getting their hands dirty with Apache Maven.


What is Repository?

A repository in maven is like a library in real world. Its a place where all the build artifacts and dependencies of varying types are stored. According to Apache Maven documentation, there are strictly two types of repository, local and remote. 

Different types of Maven Repositories

So, to start with, there are 3 types of Maven repositories:
  • Maven Local Repository.

    Maven local repository is like a library we have at home with books which only concern us. Here in this case, it's a place on our own machine where all the dependency libraries of your project which have been downloaded using Maven are stored. These files usually include plugin jars. It acts as a cache of the remote downloads. 
  • Maven Remote Repository.

    Now, the Remote Repository is like a public libraries in real world. Anyone can access it. In Maven's case, it refers to any repository which one can access using variety of protocols like file:// or http://. Now these repositories can be at least two types:
    • Third-Party public repository
      These are truly remote repository which are completely public, set up by a third party to provide their artifacts for downloading like maven2 repo.Also, you can search for dependencies here. You need an internet connection to connect to these repositories.

    • Internal Repositories
      These are internal repositories set up within an organization with limited access to their own network. It's used to share private artifacts between development teams and for releases. You need at least a network connection (not internet specifically until and unless the repository is at another geographical location) to connect to these repositories.

How to use/configure repositories?

  • Maven Local repositories.

    In general, there is no need to do anything with the local repository on regular basis, until and unless you are required to free some disk space and you need to do a clean up or you can erase it provided you are willing to download everything from beginning.
    For configuring the local repository see my old post Configuring Maven Local Repository.
  • Maven Remote repository.

    Central and Third Party Repository:
    You can use these to download dependencies and sometime upload provided you have permission to do so.
Maven Central Repository
Downloading a dependency in Maven is triggered as a result of missing dependency in the local repository. By default, Maven will look for dependency in the central repository which is set to look in maven2 repo.
The central repository is managed by Maven community. You can edit the setting.xml for maven to globally use some other repository or you can edit in the project's pom.xml, which will only work for that project.
Now let's see how can we configure Maven (pom.xml) to look for a dependency in a remote repository (not the central repository). 
<project> 
   .....
   <dependencies>
      <dependency>
         .....
      </dependency>
      .....
   <dependencies>
   .....
   <repositories>
      <repository>
         <id>organization-name.lib1</id>
         <url>http://download.organization-name.org/maven2/lib1</url>
      </repository>
      <repository>
         <id>organization-name.lib2</id>
         <url>http://download.organization-name.org/maven2/lib2</url>
      </repository>
      .....
   </repositories>
   .....
</project>
Maven Internal Repository:
Using the internal repository is quite simple. You just add the repository element and mention the location of internal repository. Something like this:
<project>
  ...
  <repositories>
    <repository>
      <id>my-internal-site</id>
      <url>http://myserver/repo</url>
    </repository>
  </repositories>
  ...
</project>
If your internal repository requires authentication, an id element can be used in the settings file to specify login information.
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <servers>
    <server>
      <id>server001</id>
      <username>my_login</username>
      <password>my_password</password>
      <privateKey>${user.home}/.ssh/id_dsa</privateKey>
      <passphrase>some_passphrase</passphrase>
      <filePermissions>664</filePermissions>
      <directoryPermissions>775</directoryPermissions>
      <configuration></configuration>
    </server>
  </servers>
  ...
</settings>
After studying a little a about Maven, in my opinion it's not that difficult. What do you think? Share your experience and issues related to maven.

    No comments:

    Follow by Email