Recently I had to configure the Gradle Distribution plugin for a microservice application, in order to work the same way as the Maven Assembly plugin. This plugin makes it possible to create a tar.gz distribution file for our Spring Boot microservices. It contains the runnable Spring Boot jar file and all necessary configuration files.
To achieve the same result with Gradle, I had to play a little bit with Gradle Distribution plugin.
First of all, you need to add the plugin to your build.gradle file, and apply it.
plugins { id 'distribution' } apply plugin: 'distribution'
The result filename had to be in the following format in order to stay compatible with the Maven plugin and our deploy script:
srv-myservice-1.0.0-SNAPSHOT-distribution.tar.gz
Configuration of the distribution plugin looks like this:
// disable generating zip file along with tar.gz distZip.enabled = false // define suffix for the result file (e.g.: srv-myservice-1.0.6-SNAPSHOT-distribution.tar.gz) distTar { classifier = 'distribution' } distributions { main { baseName = 'srv-' + project.name.toLowerCase() contents { into('/') { // Copy runnable jar into the distribution archive from 'build/libs' } into('/config') { // Copy configuration files to config subfolder from 'src/main/resources' exclude("**/*dev.yml") // exclude the development related yml files } } } } // generate compressed tar.gz distribution file plugins.withType(DistributionPlugin) { distTar { compression = Compression.GZIP extension = 'tar.gz' } }
I did not want to waist time on generating both zip and tar.gz file, so I turned off the zip file generation. (Line 2)
As the maven plugin generates the distribution with a “distribution” suffix. To get it work with Gradle, I defined the corresponding classifier for distTar task. (Line 6)
File name prefix can be easily defined in the baseName definition (Line 11). I used the lowercase project name with the prefix, to show, that string operations are allowed in the definition. The actual build version identifier gets used by the plugin without further configuration.
Content of the distribution file can be defined with “contents” part. You can define files to copy, or exclude using patterns.
In order to get a compressed result, I had to define the compression type and file extension. (Line 11)
After configuring the plugin, it is part of the compilation process. So the distribution file gets created in /build/distributions folder after starting a Gradle build with gradlew clean build