Beauty of Maven

I definitely remember the chaos and problems that I encountered while preparing this very small application. At that time, I thought that something might have helped me through my struggle. Actually “something” is not that far away. The techniques that I employed and explained in Splitting Evernote Notes post helped me tremendously. In this post, I will talk about how I converted the manual process inside Eclipse and how Maven made it too easy to be true.

First, I created a Maven project as I explained in my previous post Since I need to run it on machines having Java 6, I set the source and target parameters inside the pom.xml file accordingly. I had 3 jars of DB2, so I also defined those dependencies in the same file. Since I have the code ready, just created the package and put the source file inside it. The whole process took 5 to 10 minutes. Here is the situation:

Dependencies

As we can see, the Java version of JRE System Library is 1.6. The Maven Dependencies contain all jars that I need. The package is created and the necessary code exists where it should. By the way, there is a problem with the [M2Eclipse(http://www.eclipse.org/m2e/) Eclipse Mars and Neon. When you try to update the projects to het the necessary jars, some of them are not downloaded, even if you adjust the proxy information in settings.xml. To bypass, I downloaded apache maven and run the following command from command line, inside the folder where pom.xml file resides.

mvn clean install

That downloads all my packages correctly.

To run the code on different machines, an executable jar file must be generated. In the previous version, I did all these manually. Moreover, the jars that my code depends on must be present in specific folders on those machines. This time, I decided to put all those dependencies into a single jar file. That way, a simple copy from one machine to another is all we need do to in order to successfully execute this application.

There are many plugins that can be used with Maven. The one that fits perfectly to my case is Maven Assembly Plugin. What it does is to generate the eventual jar file to be used by either other dependent applications or an end user, simply executing it. It also has the ability to add the jars which are necessary for the application to accomplish its task. So all jars are in the single, final, executable jar.

RunnableJar

The “archive” tag inside “configuration” makes the jar executable. The class that contains the main() function should be written the manifest file. This addition to the pom.xml file defines which class should be run. When we run the following command, the JVM finds the main() function in the class which is defined in the “manifest” tag.

java -jar teklif.jar

So, after all these adjustments, we can run the project and generate the jar file under the /target folder. How will we do that? Actually, the Eclipse versions that I have access to (Mars & Neon) do not have a predefined Maven run configuration for packaging. But we can make one, thanks to the help of a stackoverflow user. The aim is to create a Maven run configuration which reads the pom.xml file of the project and runs the following command:

mvn clean package

First, we select the project, Select Run As and select Run Configurations…

Maven Run Configurations

Here, we will add the above command as a Maven Run Configuration. I gave the name “maven-package” and was sure that the Base directory is ${selected_resource_loc}. That is where Maven looks for the pom.xml file. So each time we generate the jar, we must select the main project from explorer view. The goals are clean and package, in that order. That’s it. Save it and run.

The complexity of two methods are not even comparable in my view. This is relatively small application. If gets bigger, managing it without Maven or some other kind of dependency manager, we will definitely be lost. But not with Maven.

Advertisement

%d bloggers like this: