Monthly Archives: November 2012

Adding maven plugin to eclipse and setting up first project


In my previous post I wrote about basic maven concepts for a beginner. In this post I will try to out line how to install maven for eclipse and creating a maven project in eclipse.

  1. Go to Help->”Install New Software”.
  2. On ->”Install New Software” screen click on “available software sites”.
  3. It will pop up list of download sites, on top is the default for eclipse version say for indigo it’s Juno – http://download.eclipse.org/releases/juno . Select it and click ok.
  4. In drop down associated with “Work with”, the selected link will appear. Click to select the download.
  5. This will show all updates available from the link.
  6. Type maven in the filter box, select the option under collaboration.
  7. Click next and follow screen instructions. It’s DONE
  8. It will need a restart of eclipse. To confirm whether eclipse plugin was success fully installed or not open windows->preferences->Java->BuildPath->Classpath Variables. A variable named M2_REPO must appear. This is the location where all the downloaded dependencies will appear.

  9. Where can I get a sample maven project to get started?
  10. Click Ctrl+N to open new dialog box. Select “Maven project” under “Maven” section

  11. Select a project folder. If it does not exist, maven will create one. DO NOT select the workspace folder only, other wise all files will be directly generated there instead inside a project specific folder.
  12. Clicking on next will ask to select an artifact ID.
  13. In simple terms artifactId represent a type of project to be generated for example skeleton for a simple Java project, A j2ee project, spring MVC project etc. Selecting an artifact ID will generate folder structure for the project along with the POM file which will have all the dependencies defined for that type of project. I have selected Spring MVC

  14. Next Screen will ask for Group Version and ID

  15. This will create the required project. Take a look at the POM.xml, it already contains all the dependencies for the Spring MVC project.

Maven -basic concept, set up in eclipse and first project


I had worked extensively on ANT and always wondered why maven is so talked about. I did couple of projects recently and leveraged maven -I have tried to list basic concepts and a layman hands on for eclipse set up and first project startup.
As the title suggest I will cover following topics :

  1. Introduction to maven
  2. Maven plugin for eclipse and first maven project
  1. What is Maven?
  2. You want to start with a project, say a J2ee or Spring MVC and you are lost what is the minimum set of Jars, how should I create the folder structure, Should it be a WAR, EAR, What are the “Best Practices” for a particular kind of project ..and many others.

  3. Welcome to Maven!
  4. Maven can be described as “Project Management Tool” which helps in cleaning, building, dependency resolution, integration, running test cases among many other activities related to project lifecycle management.
    All o f these tasks can be carried out by good old ANT scripts as well, but it involves manual steps to write script for all of these activities. For example, listing all the dependencies , then downloading them and maintaining a repository, if a version changes manage it manually — Maven provides many of the task as “off the shelf” plugin.

  5. Two Key Concepts:
  6. POM.xml: –This is heart of maven, all dependencies definition go here and it’s instrumental in controlling the lifecycle activities of the project.
    Archetype: It could be understood as “off the prototype”, for example for generating a simple Web project what should be the project structure, what are the required dependencies or for generating a spring MVC project what are the required details – Some one have already defined all these, just use the appropriate archetype and project setup will be done “automagically”. Typical step

    • Use archetype to indicate what type of project to use
    • It will generate standard project folders
    • It will generate POM.xml

    Use maven predefined command to download the dependencies. Nonetheless to say, one need to be connected to the net for getting the jars.
    There are “off the shelf” archetypes defined, I will explain below how to get some of them in eclipse.

  7. How do I get started?
  8. To get a feel of maven visit.
    For details visit

  9. What are the most basic concepts and terms to get started?
  10. GroupId– Represents unique identifier for the project that creates it. For example Spring can be a group under which there could be many artifacts. GroupId is quiet handy for dependency management.
    Artifact Id– This is the name of the generated project artifact –JAR, WAR , EAR or any other output. While creating from eclipse this same ID wil be used for the project name.
    Packaging- This element indicates the package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.).
    Version- This element indicates the version of the artifact generated by the project
    There are many more but to get started, these are enough.

  11. How dependencies are resolved?
  12. Heart of Maven is POM.xml file. All the dependencies are defined here. First these dependencies are looked into the local repository, pointed by M2_REPO vaiable. To see the value of this variable go to windows->preferences->Java->BuildPath. If the needed JAR is not found in the local repository, then Maven site is contacted to download needed JAR.

  13. Well I know how to define dependency, from where do we get the group Id and artifact Id?
  14. Searching on repository provide the whole dependency element. For example go to http://mvnrepository.com and search for Junit. It will list down the options.

    Clicking on one of the links will list the versions available and further clicking on the version number will open up the dependency page.

  15. Where are the downloaded dependencies stored?
  16. It’s stored in HOME folder for user, for example in case of windows it’s typically C:\Users\\.m2\repository

  17. Where is the local repository?
  18. It’s stored in HOME folder for user, for example in case of windows it’s typically C:\Users\\.m2\repository

  19. Repository
  20. A repository in Maven is used to hold build artifacts and dependencies of varying types.
    There are strictly only two types of repositories: local and remote.
    Local repository refers to a copy on local installation that is a cache of the remote downloads, and also contains the temporary build artifacts that have not yet been 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 .

    To override this, new repositories can be configured either in settings.xml ((${user.home}/.m2/settings.xml)) or directly inside pom.xml (Part of the project).
    When to define it inside settings.xml: if repository need to be shared across project.
    When to define in pom.xml : if its for a specific project only then it can be specified directly inside pom.xml
    Example for mirror and repositories:

    In settings.xml((${user.home}/.m2/settings.xml))

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

    In pom.xml (Inside project)

     
        <repositories>
            <repository>
                <id>repo.springsource.org.milestone</id>
                <name>Spring Framework Maven Milestone Repository</name>
                <url>https://repo.springsource.org/milestone</url>
            </repository>
            <repository>
    			<id>maven.ala.org.au.repository</id>
    			<name>maven.ala.org.au repositoryy</name>
    			<url>http://maven.ala.org.au/repository/</url>
    		</repository>
        </repositories>
    
  21. Maven Phases
  22. Common Maven phases:

    • compile: Compile the source code of the project. This DOES NOT compile the test classes. To compile the test classes use test-compile.
    • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
    • package: take the compiled code and package it in its distributable format, such as a JAR.
    • install: install the package into the local repository, for use as a dependency in other projects locally
  23. Do I need to have a command prompt?
  24. Not really. At least I prefer getting thing done form the IDE instead of switching back and forth in any other window. It can be directly integrated with most of the IDEs and I will explain it for eclipse.

  25. How can I Integrate it in the eclipse?
  26. I have written details in this blog