Maven dependecies in Eclipse

December 12, 2013

Not having worked with Java on a large-scale project before, I had also never touched Maven. (In this case, by large-scale project, I mean a project with lots of dependencies and a more involved build process - I did write a rather complex 3D renderer, but it only had a handful of dependencies that never changed and it was a traditional "hit f11 to run" application).

Apache Maven is a tool used for, well, a lot of things. It works with Java, C# and a bunch of other languages and automagically manages your dependencies and build process. It's essentially Ant's bigger brother.

When looking to add an existing library as a dependency to a project, it's very often to see it offered as a maven artifact. It would look something like this:

<dependency>
    <groupId>com.ning</groupId>
    <artifactId>async-http-client</artifactId>
    <version>1.7.22</version>
</dependency>

This can be a tad frustrating if you're in a hurry and you just want to download a jar to add to your build path. You just want to keep working and not have to deal with Maven. But worry not, Eclipse and I are here to help! It's very easy to sort this out in only a few minutes!

Step 1: Converting your project to a Maven project

This is a one-click deal in Eclipse. It's not even limited to Java projects! I'm using the Kepler Scala IDE and it's working perfectly. Note: this does NOT change your main project type. If it's a Java project, it STAYS a Java project. If it's a Scala project, it STAYS a Scala project. All this does is add another feature to the project. The "Convert to..." phrasing in Eclipse is a bit confusing.

Right click on menu ->
    Configure -> Convert to Maven project

You will get a dialog asking you for some configuration settings, but you can use the default ones for the time being. They can be edited at any time anyway. Click Finished. A little "M" icon should have now popped up next to your project icon. That's it! You can now start managing your dependencies with Maven! Pretty cool, huh? Moreover, your old referenced libraries (the traditional stuff on your build path) are still there! You can keep them there, or add them as Maven dependencies so that everything is nice and consistent. It's going to be very clear how to do that after you've read this little tutorial. It might be a bit of extra work, but it will make things much easier in the long run.

(Note that in obscure cases where you're using a custom run configuration to run Scala apps - e.g. when using Akka - special care needs to be taken and the Scala Library and Scala Compiler need to be manually added to the Bootstrap entries in the classpath, within the run configuration management window. You get weird errors otherwise. But again, this is just a corner-case I've stumbled upon while working with Maven and Scala.)

Step 2: Adding your dependency

Notice that converting your project to a Maven project has generated a file called pom.xml. This is the so-called Project Object Model and contains all the info required for Maven to build your project. We're just using Maven's dependency management system at this point, so we don't need to configure the build process. We're still using Eclipse's build system.

Double-click the pom file to bring up Eclipse's fancy Maven overview. Which we don't need. Go to the tabs below and select 'pom.xml' to view the raw file.

Adding your dependency.

Just paste as many "dependency" tags as you want inside the "dependencies" tag and Maven will automatically fetch them for you (Yes, automatically. You don't even need to click anything. It will happen as soon as pom.xml gets saved!) and add them to the Maven Dependencies. You will now be able to reference the dependency in your code and use it like any library that's on the build path.

Congratulations, you're now using Maven for dependency management! Do note that Maven can do much, much more than this! Just take a look over the list of Maven's features.

Comments

comments powered by Disqus