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:
$ gradle assemble :assetCompile Processing File 1 of 22 - apple-touch-icon-retina.png Processing File 2 of 22 - apple-touch-icon.png Processing File 3 of 22 - favicon.ico Compressing File 3 of 22 - favicon Processing File 4 of 22 - grails_logo.png Processing File 5 of 22 - skin/database_add.png Processing File 6 of 22 - skin/database_delete.png Processing File 7 of 22 - skin/database_edit.png Processing File 8 of 22 - skin/database_save.png Processing File 9 of 22 - skin/database_table.png Processing File 10 of 22 - skin/exclamation.png Processing File 11 of 22 - skin/house.png Processing File 12 of 22 - skin/information.png Processing File 13 of 22 - skin/shadow.jpg Processing File 14 of 22 - skin/sorted_asc.gif Processing File 15 of 22 - skin/sorted_desc.gif Processing File 16 of 22 - spinner.gif Processing File 17 of 22 - application.js Uglifying File 17 of 22 - application Compressing File 17 of 22 - application Processing File 18 of 22 - jquery-2.1.3.js Uglifying File 18 of 22 - jquery-2.1.3 Compressing File 18 of 22 - jquery-2.1.3 Processing File 19 of 22 - application.css Minifying File 19 of 22 - application Compressing File 19 of 22 - application Processing File 20 of 22 - errors.css Minifying File 20 of 22 - errors Compressing File 20 of 22 - errors Processing File 21 of 22 - main.css Minifying File 21 of 22 - main Compressing File 21 of 22 - main Processing File 22 of 22 - mobile.css Minifying File 22 of 22 - mobile Compressing File 22 of 22 - mobile Finished Precompiling Assets :buildProperties :compileJava UP-TO-DATE :compileGroovy :processResources :classes :compileTestJava UP-TO-DATE :compileTestGroovy UP-TO-DATE :processTestResources UP-TO-DATE :testClasses UP-TO-DATE :compileIntegrationTestJava UP-TO-DATE :compileIntegrationTestGroovy UP-TO-DATE :compileWebappGroovyPages UP-TO-DATE :compileGroovyPages :jar :findMainClass :startScripts :distTar :distZip :war :bootRepackage :assemble BUILD SUCCESSFUL Total time: 12.972 secs $
Next we can run the generated JAR file:
$ java -jar build/libs/sample-app-0.1-jar Grails application running at http://localhost:8080 in environment: production
We can also use command-line options to set configuration properties. In the following example we start the application on port number 9080:
$ java -jar build/libs/sample-app-0.1-jar --server.port=9080 Grails application running at http://localhost:9080 in environment: production
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:
... // Use app classifier for new JAR file. // For example sample-app-0.1.jar, becomes // sample-app-0.1-app.jar. bootRepackage.classifier = 'app' // Instruct bootRepackage to only use // JAR file from jar task. bootRepackage.withJarTask = jar ...
Written with Grails 3.0.9.