Gradle: creating executable jars for DropWizard

Some days ago, I created a Simple REST interface for my Open Source Epub organizer. I used the excellent DropWizard documentation, which guides you through the process. However, the DropWizard documentation uses Maven. While that’s not really a problem, I’m trying to switch to Gradle.

For creating a executable jar in Gradle, there are some plugins available, like the FatJar, UberJar. It’s also explained in a sample DropWizard project. However, if you look at the forks, all the forks are different. Some of them use FatJar, some use UberJar, some switched from one to the other, etc. It’s a bit of a mess. Fortunately, there’s also support from Gradle itself, and while the documentation is a bit minimal in this area, it works very nicely.

Running a ./gradlew jar will create an artifact in the build/libs directory. You can then run java -jar build/libs/epub-organizer-dropwizard-1.0.jar, et voila, the executable DropWizard jar is working:

usage: java -jar epub-organizer-dropwizard-1.0.jar
[-h] [-v] {server} ...

positional arguments:
{server} available commands

optional arguments:
-h, --help show this help message and exit
-v, --version show the service version and exit

If you want to test the DropWizard application without building a jar, that’s also possible. For that, you can use the Gradle application plugin, in the following way:

You need to define the mainClass and optionally a run block, which will pass the arguments to the main application. Running ./gradlew run will then run the DropWizard application:

GET /hello-world (nl.jworks.epub.dropwizard.resources.HelloWorldResource)

INFO [2013-07-16 18:33:15,863] com.yammer.dropwizard.config.Environment: tasks =

POST /tasks/gc (com.yammer.dropwizard.tasks.GarbageCollectionTask)

INFO [2013-07-16 18:33:16,457] org.eclipse.jetty.server.AbstractConnector: Started InstrumentedBlockingChannelConnector@
INFO [2013-07-16 18:33:16,463] org.eclipse.jetty.server.AbstractConnector: Started SocketConnector@

In short, no need for extra plugins, but using the Gradle core functionality most of your requirements to create executable jars should be covered.

2 replies

Comments are closed.