If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Monday, March 8, 2010

Creating executable jar file with Maven 2

This article explains very shortly how to create a simple executable jar file using maven2.

First step is to create a new maven project:


Before importing the project into Eclipse we run following command inside the newly created myproject folder:


Now import the project into eclipse and open the pom.xml which will look like


Also the maven archetype already created a App.java whose main method prints "Hello World".

So now it's matter of defining in our manifest that we want to execute the main method of this java class.


First add a new source folder "src/main/resources". Inside this folder create a new folder "META-INF". Inside the META-INF folder create an empty file "MANIFEST.MF".

Make following changes to the pom.xml like below:


Now we can package our jar file from the command line:


Now go into the target folder where you will see the newly created myproject-1.0-SNAPSHOT.jar.

From the command line issue following command:


This will output Hello World! to the console.


However, keep in mind that when your project depends on multiple 3rd party jars, you definitely should take a look at
- maven-assembly-plugin
- maven-shade-plugin

2 comments:

  1. Thank you very much!
    This short tutorial saved me a lot of time.
    Thank you indeed!

    ReplyDelete
  2. very very helpful..
    thanks a lot.

    ReplyDelete