Apache Maven & Homebrew

The OK-HTTP project lists that I can compile this project using Maven.  The Maven project is a build tool for (primarily) Java projects.  It provides a dependency list of other bits of compiled code and pointers to how to fetch them for a compile.  There is an alternative tool called gradle, but I’m not going to delve into that that.  I’d love to learn it at some point but I’ll stick with Maven.

Let’s check and see if I’m running Maven already: 

mv --version

.  I get an error back that signifies that I’m not running it on my local machine.  Ok, very well let’s go grab it. This seems like a good time to mention Homebrew, an package manager for OSX.  The description of Homebrew on the official site is that it gives you all the programs and tools that Apple took out of of OSX.  It is command line based, and it’s a quick scripting engine to find common projects and libraries and compile them and any dependencies they may have.  Its very straightforward to install, and you can query it for formula (read: software packages) that they have by running:

brew search maven
brew install maven

 At the end you’ll have a version of Maven installed in your “cellar” under “/usr/local/Cellar/…”  The idea is that programs compiled or installed by Homebrew shouldn’t interfere with a possible prior installed version in the normal “/usr/local/…” directory.  In this way there’s no real conflict if some other app also happens to install maven in the default directory.

Alright, so first up I’ll rerun the “mvn –version” command.  This tells me that unfortunately the version of Java it found on my $PATH is the default Apple provided version, JDK 1.6.**.  That’s definitely not very useful because the OK-HTTP project requires compiling against Java 7, and even I’m running the latest sub-version of Java 8.  Apparently this is common enough that someone has written a shell script to solve this here.  It looks legit, and I’ll tweak it to look for Java 1.8.  Success!  Here is the result of Maven now reporting the correct version:Switch JDK Version

Alright, clone the project and let’s see what we’ve got here.  I’ve followed the “Get up and running with Maven in 5 minutes” tutorial here, which gave me everything I needed (I think).  I created a Maven “goal” to create a stubby simple HelloWorld app.  This gives you a nice organizational tree and a basic JUnit test for that same basic Java class.  It also creates a pom.xml file which is how Maven configures your project.  It’s actually relatively simple– there is a key called “dependencies” and that’s where you can specify that during a build/compile phase of your project, that it reach out and grab a specific version (or the newest) of a JAR to compile your code with.  This makes it really easy for us to add the Ok-HTTP code as a dependency to the project with just a few lines.

Here’s the code I added to my pom.xml file: 

<dependency>
  <groupId>com.squareup.okhttp</groupId>
  <artifactId>okhttp</artifactId>
  <version>2.1.0</version>
</dependency>

Once there, I asked Maven to go compile my project once again using “mvn compile”, which gave me a successful compilation and the following result:OK-HTTP Maven

I’m now feeling pretty good with the basics of Maven.  If I was to use this in a Java project I could start a project in Maven, do my normal source code, and compile and build against these references to someone else’s code.  I’m ok with that, but honestly (and don’t shoot me), I do most of my Java code in the Eclipse IDE.  Considering this whole project structure that Maven implemented is still different than the Eclipse project structure, (which is in itself different than the Git blob structure) I’ve got some more work ahead of me.