Thursday, November 17, 2011

Running a Java class from maven

To run a java class from maven we can use org.codehaus.mojo:exec-maven-plugin.  This offers two goals: exec & java.  The exec goal runs the program in a seperate process.  Below is the usage of these goals:

args is treated as single string
arguments is treated as an array of strings

mvn exec:java -Dexec.mainClass=org.junit.runner.JUnitCore -Dexec.args="MyTest"
mvn exec:java -Dexec.mainClass=org.junit.runner.JUnitCore -Dexec.arguments="MyTest"

mvn exec:exec -Dexec.executable=java -Dexec.args="-cp %classpath org.junit.runner.JUnitCore MyTest"

I was trying to run a JUnit test case as a class instead of using with surefire plugin as a unit test.  Also note the classpath setting for exec goal, % is only at the beginning.  This would be replaced by the project build classpath.

Tuesday, November 15, 2011

Maven books from Sonatype

Maven by Example - (html link) (pdf link)
Maven complete reference (html link) (pdf link)
Maven cookbook (html link) (pdf link)

Monday, May 30, 2011

Maven local repository

Recently I was working on one of the common modules developed in our office and as part of the distribution I only got access to the jar built with maven without any dependencies.  The jar did include the pom, but it was pointing to a location on intranet which is not accessible to me.  After googling I found two ways of handling this situation assuming that the dependencies are available on internet and you got them locally on your harddisk.
Local repository
Declare an entry for a local repo as below:
    <repositories>
        <repository>
            <id>myId</id>
            <url>file://path to the folder containing all the libs</url>
        </repository>
    </repositories>
Once this is defined, follow the same folder structure (groupid/artifactid/version/jar-name) and on the first run, these jars will be imported into the repository.  But what if you do not want to pollute your repository with these jars not available on the repo?  Here is the second approach.  Declare a dependency for each jar as below:
        <dependency>
            <groupId>gid</groupId>
            <artifactId>aid</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>abs path</systemPath>
       </dependency>

Couple of alternatives from stackoverflow
Using add jars plugin
Using local repository

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

Saturday, January 22, 2011

sftp for password-less authentication

As an org-wide move, ftp access is removed to all our unix systems.  With this change we had to change one of our scheduled jobs, which does the backup of our twiki application data.  This job runs with a common user id and I started to setup ssh based authentication as I already use this with putty.
  • create the key pair using "ssh-keygen -b1024 -t rsa"
  • didn't give password to the private key file as access has to be through script
  • so by default it created the files id_rsa & id_rsa.pub within .ssh folder in my home
  • copied the id_rsa.pub as authorized_keys2 under .ssh of the target machine
  • executed sftp -b /dev/fd/0 <user>@<target host>, still prompting for password
After searching through many forums, realized that the .ssh folder & authorized_keys2 file permissions need to be set properly.

chmod 700 .ssh
chmod 600 authorized_keys2

Still it wouldn't work.  Also observed that the .ssh folder was getting SGID bit set by default.

chmod -s .ssh

Still no luck.  Finally realized that the problem is because of the home (/u/test) folder itself.  It has 755 permissions as usual and only I changed it to 700, did sftp work with prompting for password.