Tuesday, June 07, 2011

Maven... for uninitiated

Having been around the java landscape for a while, I couldn't help but run into the tool known as maven. It is fairly accurate to say that maven is ubuiquitous. Having tried maven briefly in the past and given up I decided to bite the bullet and get down and learn maven.

I had help in the form of the book, Maven, a Definitive guide which eased my passage into maven.

A few weeks ago I thought I'd give maven a try. I tried it out, hit a few problems and then gave up. That I refer to as my firstcoming to maven. Then after another few weeks I thought I'd give maven a second chance, this time I was a lot more successful. So having been using maven for more than a few weeks now, and especially having had two bites of the cherry, it is safe to say that although I am not a fan boy and a maven evangelist, I am a convert in the sense that I would want to use it on the applications I build. Let me say that this opinion can change as I mature in my thinking on maven as I extend into more areas of maven's capability.

Firstly, Why use Maven?

1. Dependency management - what a pleasure it has been to simply right click (since I'm using m2eclipse) on my project, select add dependency, type in a few letters of the dependency, and select the dependency and it is automatically downloaded and available. No more trawling the internet for that lost dependency... it's right there when you need it.

2. It provides a platform for adding value. This sounds a little nebulous, I understand, but once you've created all the maven infrastructure you can easily build on top of that through plugins. My test case was a back end app offering a bunch of services exposed through REST. I discovered the jetty plugin for maven - with this plugin, I simply checkout the code, run the maven build and then execute the plugin. Maven starts jetty and deploys my application to it. A very simple way for other developers who need to call into my web app to get access to it. A few command line intructions and my app is running


Some important considerations when starting the journey:

1. Take it slow. Maven, is complicated. It's not good rushing into it, trying a few things, getting frustrated and then backing out. That was the mistake I made initially dived head long into maven, and hit my head more than a few times. There are a number of irritations which should not actually be there, which put me off. These irritations had work arounds which I have since found, but at the same I found them very irritating.

2. Use a local repository - a lot of the irritations of using maven can be resolved if you run a local repository. One of the major changes in thinking when coming the ant or the IDE build world is that with maven _all_ dependencies are imported, no dependencies are placed with the source code _at all_. So if you need to just quickly add this dependency to your build... don't go there. Maven coerces you to put that dependency into a repository and then reference it from elsewhere. Yes, you can install it to your local repo but the command to do this is clunky. Rather just install it on your local repository.

3. Don't try and do it the non maven way - maven is _very_ opinionated. It expects your code to be arranged in a very particular way. Do not try and fight with this. Resistance is futile. Standards are good and work well because they are used by all, not necessarily because they are the best way of doing things. You might think you know better than maven, that's fine - try and be different and you'll soon hit a wall.

A few of the problems I had, and will still need to deal with:

1. I think one of the problems with maven is that not all the code libraries int he world are available via maven repositories. If this were the case, chances are there would be a lot less pain. In my first coming to maven I quickly hit a problem when I added log4j as a dependency. Log4j supports jms based connectors etc and since these libraries have a more restrictive license they are not available from the primary maven repository. So when you add log4j as a dependency, the build simply fails. One work around is to download these artifacts manually and install them - something I wasn't willing to do as what's the point of using maven if I have to download dependencies manually right? At my second coming to maven I discovered that you can configure your dependencies to not include certain transitive dependencies. I excluded these unavailable via maven repository dependencies and my frustration was removed.

2. Handling third party dependencies - I had to make a connection to a mysql database. The mysql jdbc jar is obviously not available in a maven repository. This is where the local repository, artifactory or nexus come in. I figured out how to deploy this jar to this repository and no more problems. I was now doing things the maven way.

In summary, for now I am using maven. It has filled a big gap in my arsenal. A replacement for maven would have very big shoes to fill that I'm not sure I will go back.

But we will see...

How important is the programming language?

  Python, typescript, java, kotlin, C#, .net… Which is your technology of choice? Which one are you currently working in and which do you wa...