Sunday, January 23, 2011

Working with maven

Most of the times I came across maven in the ebooks I was reading to learn some web-framework or some ORM tool.  I got two options - either go on my own with ant or download the source (often huge) for the ebook and follow the instructions for maven as given.  As it was getting a struggle everytime, I started to look at maven seriously and this book from sonatype helped a lot.  Below are some of the issues I got stuck while working with maven and workarounds/solutions found (definitely by googling).
  1. I started with maven 2, but later on moved to maven 3.  In this one archetype:create is deprecated and archetype:generate is the way to start creating a new project.  Sometimes (always when I do from office) it happened that my maven process is not able to retrieve the available archetypes from the central repository and would give only a partial listing.  I being behind a proxy could be the reason.  One of the workarounds I got from maven forums is to have the archetype catalog downloaded locally to .m2 folder of the user folder and then run mvn as mvn archetype:generate -DarchetypeCatalog=local
  2. Which archetype to use?  Although most of the open-source projects do mention the maven dependency and available archetypes if any, in case you do not find searching Jarvana or MVNRepo might get you something useful.
  3. Love eclipse - how to have the project created in eclipse?  Original source run mvn eclipse:eclipse.  However if looking for the latest wtp version, not supported by this plugin just create a dummy dynamic web (or whatever project type) project and take the contents from .project file (natures and buildCommands) and update your .project.
  4. I want to have source and javadoc if available for a dependency? mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true or mvn dependency:sources.  However for any of them to work the sources/javadocs must be available in the repository.  Another option is dependency:resolve -Dclassifier=javadoc
  5. Want to see how to use a plugin? mvn help:describe -Dplugin=
    or  mvn help:describe -Dplugin=
    -Ddetail=true
    to get complete details.  In case the plugin name is not unique we can execute mvn -Dhelp:describe -DartifactId=org.apache.maven.plugins -DgroupId=maven-eclipse-plugin to get help on eclipse plugin.
  6. Sometimes we want to try out various options, but do not want maven to update/download any dependencies.  We can run maven in offline as mvn -o
  7. To see the effective pom mvn help:effective-pom
  8. To execute a class mvn -Dexec:java -Dexec.mainClass= -Dexec.args=
  9. Another strange issue I got on XP was that the output of  mvn command gets cleared immediately on cmd.  Commenting out the last line in mvn.bat (cmd /C exit /B %ERROR_CODE%) solved this issue.
Will add if I come across any more tips.

Edit on 20-Dec-12
Another alternative to #1 if we know the archetype to use:
mvn archetype:create -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeVersion=1.1 -DartifactId=my-test -DgroupId=com.phani

No comments:

Post a Comment