Grails Goodness: Creating A Runnable Jar

Grails 3 makes it very easy to create a JAR file that is runnable with a simple $java -jar command. We must use the Grails command package or the Gradle task assemble to package our application as a so-called runnable JAR file. This JAR file has all the necessary classes to start up our Grails application.

In the following example we have a Grails application sample-app. We use the Gradle task assemble to package the application into a JAR file. The resulting JAR file can be found in the build/libs directory of our project:

Next we can run the generated JAR file:

We can also use command-line options to set configuration properties. In the following example we start the application on port number 9080:

We can copy this JAR file to another location, for example a test or production server, and use the same command to run our Grails application.

All this works because the bootRepackage task of the Gradle Spring Boot plugin will use the JAR file of the jar task and converts it to a runnable JAR file. The original JAR file is overridden so next time when we execute the assemble task the JAR file is regenerated. To use Gradle’s incremental build feature we can configure the bootRepackage task to create a new JAR file instead of overriding the default JAR file. In the following Gradle build script snippet we use a different name for the JAR file that is generated by the bootRepackage task:

Written with Grails 3.0.9.

Original post

This entry was posted in Grails and tagged , by mrhaki. Bookmark the permalink.

About mrhaki

My name is Hubert A. Klein Ikkink also known as mrhaki. I work at the great IT company JDriven. Here I work on projects with Groovy & Grails, Gradle and Spring. At JDriven we focus on SpringSource technologies. All colleagues want to learn new technologies, support craftmanship and are very eager to learn. This is truly a great environment to work in. You can contact me via Google+ or @mrhaki.

Leave a Reply

Your email address will not be published. Required fields are marked *