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 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))

          <name>UK Central</name>

    In pom.xml (Inside project)

                <name>Spring Framework Maven Milestone Repository</name>
    			<name> repositoryy</name>
  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

About ChandanPandey

Try to come up with a good design as by product of good coding practices

Posted on November 1, 2012, in Build and integration and tagged , . Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: