Pages

Sunday, October 20, 2013

Maven 101: Day 1

Day 1:Get the ball rolling

I have been on Ant for ages, yeah yeah call me a troglodyte. Anyways, we are going to move to maven pretty soon, so thought of dabbling in it and getting familiar. So Maven I choose thee as my first 'learn it yourself' project.

Fun fact: Maven is Yiddish for "One who understands, based on an accumulation of knowledge"

Download and install maven.


This was a breeze. I downloaded maven 3.1.0 onto my brand new Windows 8 laptop.
Followed the steps for Windows given on Apache Maven Install. Doc.

  •  Downloaded the maven binary zip from Apache Maven Download  and extracted. 
  •  
  • I extracted it to a folder call c:\work. This resulted in the following folder structure
    • C:\work\apache-maven-3.1.0-bin\ and
      C:\work\apache-maven-3.1.0-bin\apache-maven-3.1.0
    • Set the M2 environment variable to :
      C:\work\apache-maven-3.1.0-bin\apache-maven-3.1.0\bin

    • The PATH variable was set to include the M2 variable ie C:\work\apache-maven-3.1.0-bin\apache-maven-3.1.0\bin

    • Confirmed that JAVA_HOME was set correctly and that, Java Home was included in Path. Maven is a Java tool so Java needs to be there before you can make any headway with Maven.  In my case JAVA_HOME was set to

      • C:\Program Files\Java\jdk1.7.0_40
      • And the PATH variable included
      • C:\Program Files\Java\jdk1.7.0_40\bin

Now I was ready to try the mvn --version command. So I opened a command window and typed it.
Nothing...happened...:(, mvn not recognized. Tried the age old developer trick, Reboot.
Voila!! mvn is now a recognized cmd. Was the Reboot necessary, dunno for sure.

mvn --version displayed the following:


Declared Victory!!!!
 

Got bored of the mvn --version output in about 30 seconds. Time to move on.

Configuring Maven on my lappy

Went through the following Apache Guide to Configure Maven
I was installing maven on my private laptop, did not want to complicate matters by meddling with the default settings. Maybe later, not now. Did not have any need for configuring HTTP proxy either.

My first Maven Project


Created my first maven project : Namaste

Fun fact: Namaste, derived from Sanskrit "Namaskar", I bow to thee. So yeah, I Indianized the "Hello World"

Here is the command I used.

mvn archetype:generate -DgroupId=com.gsm.app -DartifactId=namaste -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 

It took like for ages to get this first project created, but the dudes and dudettes at Apache had already warned  about it. They had mentioned that the first time I run this command, maven will download all the required plugins into my local repository. My connection did not time out, but it did take for ever..:)

The project was created successfully and everything, but at this point I did not know how, why or what.
So decided to take the command apart and figure out what each piece did:

archetype:generate

Here "archetype" is Maven Plugin and generate is the goal.

Here is a summary of what I understood. For details check out page : Apache Documentation on Archetypes


Q: What is an archetype? How is it used?
A :  An archetype is nothing but a Maven project template. The archetype author creates an archetype and installs it in the organizations repository. The developers can now use the archetype to generate projects in a consistent fashion. All the developers use the same archetype to create their project structure and thus standardization and best practices implementation is easy to achieve.

Q: What is a maven plugin? What is a goal?
A: Maven is a collection of plugins, where each specific task is implemented as a goal. Thus "archetype" plugin has a goal called "generate". This task implements the generation of a project structure from the archetype. Or in other words a plugin is a collection of related goals

To summarize, when I typed mvn archetype:generate, our ever so wise Maven realized that I meant to use the archetype plugin to generate a project structure.

Fun Fact: Archetype came into English from Greek via Latin. Original Greek word: archetupon,  meaning "first moulded". Plain english meaning of Archetype: A prototype which is copied or emulated by others.

If archetype:generate was the only command typed in by me, maven would ask me a series of questions which would give it enough information to create a project structure. This mode is called the interactive mode.  Details on the interactive mode available at Apache Archetype Plugin Usage

However, I had volunteered all the information before hand, thus had no need for interactive mode. Here is what the rest of the command means:

 -DgroupId

 Here is how I conveyed package structure information to maven. Thus maven now knows that the package structure for my project is com.gsm.app.

-DartifactId

Here is how I conveyed the project name; the artifact name in mavenese (I just made that up)


-DarchetypeArtifactId

 Now that I have told maven what my project should be called and what my package structure should look like, maven would be eager to go ahead create the project structure. But wait, Maven says: you requested a create from an archetype, care to tell me which one? Oh yes -DarchetypeArtifactId is how I let maven know which archetype I would like my project structure to be based on.

Here I chose maven-archetype-quickstart but I could have chosen:

maven-archetype-j2ee-simple : To create a simple J2ee project structure
or maven-archetype-simple : To create a simple maven project.
 Etc. Etc.

You get the idea, the project structure depends on the archetype I choose, thus I choose the archetype with the project structure I want. Simple rule of life, choose that which will give you what you want...:D


-DInteractiveMode

Setting this false, as I have already told maven all it needs to know. I don't need to interact with it further. Setting this to false to keep maven from asking questions.



Final Result: A beautiful Maven project




Just for fun also decided to create one with the j2ee archetype (maven-archetype-j2ee-simple) , decided to name it heyjee ,here is what I got.




Also noticed that running archetype:generate with archetype ids for quick start and the j2ee resulted in these files being downloaded in the maven repository:

Under .m2\repository\org\apache\maven\archetypes
 In found the following archetypes which I believe were downloaded by archetype:generate.



POM : The Project Object Model

Oh, btw, here is the pom.xml for my very first maven project. A dependency to Junit is automatically added to the pom.xml. As per my understanding, this is due to the archetype pom information.



Enough for Day 1 I guess. So signing off.






No comments:

Post a Comment