Thursday, January 14, 2010

Maven - It Isn't ALL Bad!

Nope, this isn't a blog post to start another war between Ant and Maven. I am and will remain a huge fan of Ant for many years to come. Having used it extensively in the last decade, I know the in's and out's of writing huge huge build files. Yes, huge build files.

However, I was trying a few new projects and all the samples I tried were mostly in Maven. It had been my New Years Resolution for many years to learn Maven. But, they remained just that , New Years Resolutions and never got a chance to actually either learn or use it in any of my projects.

Last week, I had a few hours of free time, and decided to actually try Maven from scratch. I was always intimidated by the POM; which is THE PROJECT OBJECT MODEL. No idea why. I tried a few simple commands and was able to clearly understand and learn Maven the right way.

If given an option now, what will I choose for my new project, can you guess? Nope, you thought I would choose Maven, you are wrong. I would still use Ant. However, if I am writing an article, or giving a presentation, I would rather use Maven. It is so simple to create a new project, where Maven does all the work for you.

  • Open a command prompt, and type the following command:
    • mvn archetype:create -DgroupId=org.meeras.sample -DartifactId=sample -DpackageName=org.meeras.sample
 The output in the command window will be as such:

 At this point, Maven created the entire directory structure for me as shown below. How cool is that?

  • The POM at this point is very simple, with just JUnit as it's dependency. If you want to see effective POM, which Maven always executes from, run the following command to see all the Parent POM's and also the Super POM.
    • mvn help:effective-pom
The output now within the command window should be something like this:

If you are having trouble finding from where Maven is getting all the information, just try the above command. You will see the merged POM, which can help in many ways.

  •  Maven creates a simple Java class with a static main which you can run from the command line where the POM.xml file resides .This following command compiles, tests, packages and installs the sample application.
    • mvn install .
Now that you have the jar file, you can run it.
C:\dev\projects\maven-samples\sample>java -cp target/sample-1.0-SNAPSHOT.jar org
Hello World!

In one step I was able to create an entire project structure. That's the biggest plus for Maven. Convention over Configuration.

So, that's it for today. I will write more about Maven the next time I do something even more interesting.


  1. Why stick with ANT?

    I have found that getting up to speed on a Maven project is much faster than on one using ANT.

    Since Maven strongly encourages a standard project structure.

    When I get involved in an existing project using ANT, I spend a considerable amount of time getting familiar with the project structure. Finding where the property files are, where the Java code is, where the JSP's are, etc.

    With Maven it is all standard, no need to waste time getting familiar with the structure.

  2. I haven't used Maven in any projects until now. Most places I worked or companies I consulted in the last 2 years, have used Ant.

    So, if I see the usage of Maven next, I wouldn't be intimidated.

    I sincerely have started liking it as well so far.

  3. I moved from Ant to Maven2 recently because I hated checking in the dependency JARs in version control. If Ant had a dependency management system like Maven, I would have stuck with Ant.

  4. Did you try Ivy? I have used it in many projects. It is exactly the same what Maven does for managing dependencies.

  5. Completely agree that Maven is great for "kickstart" scenarios or simple demos. In my experience, when you have to do some specific stuff as part of your build, it is far, far easier to achieve using Ant. I blogged about this here (and there are some thoughts regarding Ivy as well):

    Why you should use the Maven Ant Tasks instead of Maven or Ivy

    I've heard that Maven 3 would natively support scripting (e.g. using Groovy) so that would make things a lot easier. However I expect that IDE support will take a while to catch up. Excellent IDE support is one of the reasons why I prefer Ant.

    And kudos for sticking to your guns that you will continue to use Ant for new projects for now. Especially given the recent spate of pro-Maven articles on DZone and the like ;)

  6. I read your post. Thanks so much for pointing me to the same. It is excellent.

    I tried the Eclipse plugin for Maven. Had no luck getting it to work. Didn't bother digging deep into the problem either.

  7. I was an Ant user (long back), but have been using Maven for almost 5 years now -initially, maven1 and then maven2. I agree it can be intimidating initially. We even used to have joke that everything works by magic in maven. But once you understand it, it is really nice and all the available maven plugins makes your life even more easier. Maven3 seems interesting as well...

  8. That's exactly why I was hesitant to use Maven. Everything happened magically. And of course, magic and programming never go hand in hand.

  9. I am also curious to try out Maven 3. Been happy with Ivy for Ant so far - seems like a good addition for dependency management. Thanks for the informative blog entry.

  10. Haven't tried Maven 3, Levent. Need to work on that as well.

  11. Maven is a disaster. Avoid it at all costs.