Maven Tips

I am a bit of a Maven fanboy. Actually I am more of a - use anything available to make your task easier, smoother and more efficient - person.  And Maven has fit the bill perfectly for near about 3 - 4 years now.

I had written earlier about Maven here. In a very recent conversation around standardization of toolkit for software project / product lifecycle, I realized that Maven was perhaps one of the lowest common denominators to start from (speaking of Java world). I simultaneously - as I was rattling off the virtues of adopting Maven enterprise wide, to the audience - realized that I had not really written enough about it. And hence this article.

With something as widely used as Maven, I don't see much reason to write, yet again, about how to set up, how to use etc. You will find almost everything very neatly described in the Maven website. I have found loads of very good articles on Maven at this link as well. However, I will share a couple of tips here.

Tip 1
The beauty of Maven (and indeed most of the Java based software) is that the installation is too simple. You just put the jar in a folder of your choice and the software just works. Most of the default configurations are good enough for first use. However, there is one bit that I suggest you change. Change the localRepository - the folder where Maven would download dependencies for your code from internet and store in your harddisk - to something that suits you. I like to put it in a drive that is not my primary drive i.e. not the drive in which I have Windows installed. So, in case the windows dies on me, or something happens, this folder with all the dependencies have a better chance of being retained. As you write code, this dependencies folder tends to grow and should you lose it you will have to spend considerable amount of time and bandwidth to download all the stuff.  So, all that I suggest you do is add the following configuration to the default configuration.

File: \apache-maven-3.0.4\conf\settings.xml
<localRepository>D:/mavenrepo</localRepository>

Tip 2
I dislike polluting (some people will disagree that this is "polluting") my system settings with product specific stuff e.g. JAVA_HOME etc. I have always found it much easier to use a simple batch file instead. I use that with Maven as well. I tend to have this (or some variation of this) batch file, which when run (after taking off the appropriate REMs) will do the basic stuff e.g. creating a java application, creating a web application, running them, executing a specific unit tests etc. These instructions are easily available, but I personally found it useful to have it handy and ready.
ECHO OFF 

REM =============================
REM Set the env. variables. 
REM =============================
SET PATH=%PATH%;C:\ProgramFiles\SpringSource\apache-maven-3.0.4\bin;
SET JAVA_HOME=C:\ProgramFiles\Java\jdk1.7.0_09

REM =============================
REM Check the versions. 
REM =============================
REM mvn --version

REM =============================
REM Get a list of all template projects. 
REM =============================
REM mvn archetype:generate

REM =============================
REM Standalone java application. 
REM =============================
call mvn archetype:generate ^
    -DarchetypeArtifactId=maven-archetype-quickstart ^
    -DinteractiveMode=false ^
    -DgroupId=foo.bar ^
    -DartifactId=corejava

 
REM =============================
REM Standalone java web application. 
REM =============================
REM call mvn archetype:generate ^
REM     -DarchetypeArtifactId=maven-archetype-webapp ^
REM     -DinteractiveMode=false ^
REM     -DgroupId=foo.bar ^
REM     -DartifactId=web001


REM =============================
REM Run the main class. 
REM =============================
REM mvn exec:java -Dexec.mainClass="foo.bar.Main"
REM mvn exec:java -Dexec.mainClass="foo.bar.Main" -Dexec.args="arg0 arg1 arg2" 

REM =============================
REM Run unit tests. 
REM =============================
REM mvn test
REM mvn -Dtest=myUnitTest test

pause

This is all that I had time for today, but I will come back and add more tips to this page.

If you want to get in touch, you can look me up at Linkedin or Google+ .

No comments:

Post a Comment