Maven in 15 minutes

1. What is Maven

Maven is a Project Management tool. It allows you to keep track of the releases and documentation of your project and use other projects in your project(dependencies). It wraps all this information up in an XML file called the Project Object Model (POM)

2. Why Use Maven

So, why use Maven when we already have excellant version control systems like GIT.

The main benifit of Maven is transitive dependencies. Suppose I have Project A that requires Project B to work. We can add B directly to the POM and Maven will build it for us. Now, Suppose B requires Project C. We don't have to do anything. When compiling B, Maven automatically will add C to our Build Path

If you want to look at why people use Maven take a look here

3. Installing Maven

Download Maven from this link and extract it from its zip folder.

Computer > Properties > Advanced System Settings > Environment Variables

  1. Add a new Variable name : JAVA_HOME value: {JAVA_PATH}, for example C:\Program Files\Java\jdk1.6
  2. Add a new Variable name : MAVEN_HOME value: {MAVEN_PATH}, for example C:\Program Files\maven
  3. Go to the PATH variable and add this to the end (after another ;) {MAVEN_PATH}\bin

Now test by going to Command Prompt and typing mvn. If the command prompt does not say 'mvn' is not recognized then its installed correctly

4. Create a Maven Project

To create a Maven Project we use the following command

mvn archetype:generate -DgroupId={YOUR_PACKAGE} -DartifactId={YOUR_NAME} -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  1. DarchetypeArtifactId : Defines the basic archetype the project will use. You can read more about it here
  2. DinteractiveMode : If set to true it will ask you for all the options not specified while running the command. If set to false it will assume them

5. Maven and Eclipse

To allow your new Maven Project to be used in Eclipse you have to

Go to the project folder in the command line and run this command : mvn eclipse:eclipse

This is called a classpath variable. Whenever you have a maven project in eclipse you need dependencies from you Maven Repo. This will set that variable. To do this type this command in the Command Line

mvn -Declipse.workspace="your Eclipse Workspace" eclipse:configure-workspace

In Eclipse File > Import > General > Existing Projects into Workspace > Select project folder > Finish.

6. Done

Now you have a fully functioning Maven project in Eclipse. You can edit the pom.xml to pull dependencies and update your version.

05 Apr 2014