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 :
- Introduction to maven
- Maven plugin for eclipse and first maven project
- What is Maven?
- Welcome to Maven!
- Two Key Concepts:
- Use archetype to indicate what type of project to use
- It will generate standard project folders
- It will generate POM.xml
- How do I get started?
- What are the most basic concepts and terms to get started?
- How dependencies are resolved?
- Well I know how to define dependency, from where do we get the group Id and artifact Id?
- Where are the downloaded dependencies stored?
- Where is the local repository?
- Repository
- 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
- Do I need to have a command prompt?
- How can I Integrate it in the eclipse?
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.
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.
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 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.
To get a feel of maven visit.
For details visit
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.
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.
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.
It’s stored in HOME folder for user, for example in case of windows it’s typically C:\Users\\.m2\repository
It’s stored in HOME folder for user, for example in case of windows it’s typically C:\Users\\.m2\repository
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>
Common Maven phases:
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.
I have written details in this blog
Posted on November 1, 2012, in Build and integration and tagged eclipse, maven. Bookmark the permalink. 1 Comment.
Pingback: Adding maven plugin to eclipse and setting up first project « Thoughts on Software design and development