Showing posts with label Apache maven. Show all posts
Showing posts with label Apache maven. Show all posts

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.

    Sunday, July 15, 2012

    Configuring Maven Local Repository

    What is Maven Local Repository?

    Maven local repository is the place on your machine where all the dependency libraries of your project which have been downloaded using Maven are stored. These files usually include plugin jars. 

    Maven Configuration File:

    Maven local repository is defined in it's configuration file which is usually located under directory \conf with name settings.xml.

    Changing Default Maven Repository:

    By default on windows 7, the maven local repository is set for C:\Users\user_name\.m2\. You can change this by editing the settings.xml. 
    In the settings.xml look for the commented localRepository definition which looks like:
     

    Either un-comment it or add the following line:
    D:/dev/maven_repo
    Save the file and you are done. Now all the future dependencies will be downloaded to the path you set. 


    Refer to the Maven Repositories and Configuration post to see what types of Maven repositories are there and how you can configure them.

    Sunday, July 1, 2012

    Apache Maven - Setting up

    What is Apache Maven?

    In simple words Maven is a build automation tool for Java projects. 
    There are many things which can be written for maven but to get more theoretical details  visit Apache Maven-Wikipedia page or can refer to the official Apache Maven website.


    Setting-up Apache Maven:

    Setup - Windows:

    1. First download the latest version maven form here. For this tutorial, I have downloaded maven-3.0.4.
    2. Unzip the downloaded apache-maven-3.0.4-bin.zip. In my case, unzipped it in C:\dev\maven3.0.4.
    3. Set a new environment variable M2_HOME pointing to the directory where maven is installed or  unzipped. In case if you don't know how to set an environment variable in windows, follow the link. 
    4. Set another environment variable M2 with value %M2_HOME%\bin. In the same way create/update the PATH variable with value %M2% so that Maven can be accessed from command line. 
    5. Check if the PATH variable also has the value of JAVA_HOME pointing to the installed jdk.
    6. Open command prompt (WinKey + R and type cmd) and execute the command mvn --version to see if its installed well.

    Setup - Eclipse:


    For this there is a plugin for Eclipse called M2E which can be used to launch Maven builds from within Eclipse and other stuff which maven can do.
    Follow the steps given below to get M2E on eclipse:
    1. Open Eclipse.
    2. From the eclipse menu list, go to help -> Install New Software...
    3. Get maven from the update site "maven - http://download.eclipse.org/technology/m2e/releases".
      • Add the maven update site and get the list of plugins provided by it.
      • Select the plugin Maven Integration for Eclipse and press next and next.
      • Read and accept the terms of license agreement and press finish.


      • Restart eclipse.

    More on Maven:

    Feel free to share your experience/issues.