Adding JAR Dependencies to an Eclipse Plugin Maven Tycho Build

Something that is constantly bothering me when developing the OPP project is the use of hard-coded Java library dependencies. I manually downloaded the jars for the libraries I use, copied them to a directory in the plugin that required them, and the added them to the MANIFEST.MF file.

You are probably asking why am I doing this. Well, Eclipse plugins (or more correctly OSGi projects) are built in Maven using a plugin called “Tycho”. This build process is different from the standard java Maven build process, specifically in how it handles dependencies.

OSGi dependencies are defined in a MANIFEST.MF file and not defined in POM of the project. When Maven is executed, it looks at the MANIFEST.MF file, extracts the dependencies, and then tries to download them from one of the repositories defined in the POM, which, as expected, are defined differently than Maven repositories.

We love standards. That is why we need at least two of them :-(.

So back to my problem: I have a number of simple Java dependencies that I am using in my projects. So searching the net I found the Apache Felix Maven Bundle Plugin (BND). After trying and trying again to get its configuration right, I gave up. Maybe you need a PhD to configure this, so I’ll come back again in a couple of months (hopefully :-)).

But hey, I’m a developer, and I should scratch my own itch, so why not develop a Maven plugin that does what I want? Yep, good idea! But after two weeks of work (by work I mean the only half hour available to me each day to work on this, excluding weekends) I found myself with a simple Maven plugin that is able to copy a file from here to there, but the learning curve is a steep.

And then I reinvented the wheel, because the solution was already posted in StackOverflow and I had read it a while ago, but disregarded it. Last week after learning some more Maven (because I was writing a plugin), I understood that I could create a new project (standard Java maven project) where the dependencies are downloaded and then copy the dependencies to my Eclipse plugin project. After a couple of minutes, Voila! It Works! You can find the results in the OPP repository. The Java only project is com.vainolo.phd.opp.java-dependencies which is required by the Maven module com.vainolo.phd.opp.utilities. Check how the POM files are written to see how this works. I also added the dependencies project as a child project of com.vainolo.phd.opp.releng so it gets built as part of the project.

There is also a simpler way to do this without another project, as described in this StackOverflow answer. Here the maven-dependency-plugin plugin is invoked directly in one of the initial phases of the build (i.e. validate), using plugin specific configuration to select the dependencies. Tried this but while it works for the direct dependency, it didn’t download transitive dependencies. This is probably a configuration things but at this stage I am not going to invest more time in this.

Maybe next year.

Happy coding!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.