Maven : Basics


Basics, Installation

Maven is a project management tool. I am going to be practical about this page, since there are numerous books/pages/documentation out there explaining maven and I don’t want to be a “me-too” kind of a tutorial on maven. In keeping with my basic philosophy “lets get down and dirty”, lets install and get you up to speed on maven.Now before starting I would assume you have Eclipse IDE, Java 5 or higher installed on your system. Lets start, first lets get maven and install it on the system. This one’s a no-brainer, go to Maven Download site! and get it up and running. follow all the instructions of setting paths correctly. should you miss somethings go and consult documentation again.

Now lets get the Maven eclipse plugin, Use the following steps :

  1. Click on help on upper right corner of eclipse IDE. use Help to go to Software updates > Find and Install > Search for New features to install > new remote site
  2. Plugin M2Eclipse in URL and give it a name.

Once you get the plugin, it will ask you to restart Eclipse, do so.

Now lets get down to doing some stuff with maven. First we will create a maven project, a maven project will have a particular setup with it (You will see how it is once you create a maven project), so lets execute our first maven command:

mvn archetype:generate

The above will give you a series of options, usually till 36, each of the option is a type of project you want to generate with basic structure and libraries in built for you. select the most basic one which is option 15 which is maven-archetype-quickstart (). Next it will ask you punch in group-id, artifact-id, version, package. Use below values: (this is just a sample, you can enter whatever you want to ):

Well, first off all those four things are what will identify your project for maven, they are in most simple terms maven project coordinates.Next, maven will as you to confirm all the settings, just hit enter or enter “Y” to confirm all the settings, and viola! you have your very first maven project. Now, once you go inside the project you will notice that there is a file that is called POM xml, this file is where all the settings are for maven. (Lets not talk in details about it for now, we will explore this in detail later). Now that our project is made, lets try and import it to eclipse … WAIT!

Take a deep breath! :) The project is NOT ready yet to go and become a star Eclipse project ;) . Let us enable it to be imported to eclipse workspace. First to do so, you have to set M2_REPO variable.

mvn -Declipse.workspace=”ENTER PATH TO YOUR ECLIPSE WORKSPACE HERE WITHOUT QUOTES” eclipse:add-maven-repo

Next up, run the following command:

mvn eclipse:clean eclipse:eclipse

Nothing special here, For now ignore the messages that maven gives about javaDocs and sources. Now we are ready to do a formal import. use File > Import > Existing Projects in the workspace.

Use browse to select the project we just made. And viola! we are done. Finally you should see something like this :
Click here to see the Project setup!

You are good to go. Some of the other important things / commands to remember are :

mvn compile

The above will compile the project.

mvn test

The above will compile, and run all the test cases of a project. (Notice the ordinance, Maven executes everything in phases, like for example if you write, mvn validate then just validate phase is executed. But if you give something like mvn compile, then first maven will run validation and then compile the project, similarly mvn test would result in validation –> compilation –> test). There are other phases after this too, like Integration-test, verify, install, deploy (In ordered form respectively). So for example if you give a command like mvn deploy (deploy is the last phase), maven will execute all the preceding phases starting from validate. Maven also (thankfully) allows you to get Javadocs and source code for the dependencies you are downloading, this can be helpful when you are debugging an application that uses other frameworks like Hibernate, Spring or JSF implementations out there or Wicket. (Forgive me if i forgot tapestry and other ones … cant give the whole shopping list here ;) ). so for that you simply do mvn eclipse:clean eclipse:eclipse as we did before but add -DdownloadSources=true and -DdownloadJavadocs=true to it and there we go Ladies and Gentleman, complete maven project.

So summarizing things, we can now import a simple maven project to eclipse workspace, we know how to compile, run test cases (using command line, I’ll talk about doing this with the eclipse plugin later, Shouldn’t be that hard) and finally we know how to generate sources for our dependencies (like Hibernate for example).

In next part, we’ll talk more about POM xml and go into details of maven. For now, you can play with maven (or at least have enough to fool around with it).

Thanks for your time!

Regards
Vyas, Anirudh