Generate Single Jar Or War In Spring Boot App Using Gradle Build Tool

Table of Contents

Introduction

Generally most of the Spring Boot applications generate jar archive for deployment. Very few Spring Boot applications generate war archive for deployment in the production environments. There are two types of jar or war files generated by gradle build tool.

Two types of archives generated by gradle tool are fat and thin jar or war files. You don’t need to do any special configuration for generating two types jar files from the Spring Boot app, but for generating war files you need to include the war plugin in the build.gradle script. Again the build tool will automatically generate two types of war files just by including the war plugin.

The two types of archive files generally end in *.jar and *-plain.jar or *.war and *-plain.war files.

Why do you need a single jar or war?

In your local environment you can easily remove the other which is not required, but think of the central repository, such as, Nexus, where your released library (jar or war) is stored and there may be a policy to keep only one jar or war file. This single jar or war should end in *.jar or *.war and does not accept *-plain.war or *-plain.jar then you need to generate only one jar or war file. And finally you are going to need only one type of jar or war for deployment.

If you need executable jar or war file then you need to consider for *.jar or *.war file. If you need thin archive then go for *-plain.jar or *-plain.war file.

Prerequisites

Java 1.8+(11 – 12), Gradle 6.8.2+(6.8.2 – 7.4.2)

Generating Jar

Let’s consider the following build.gradle script in your Spring Boot application.

buildscript {
  	ext {
    	springBootVersion = '2.6.7'
  	}
 
  	repositories {
    	mavenCentral()
  	}
 
  	dependencies {
    	classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
  	}
}
 
plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"
}
 
sourceCompatibility = 11
targetCompatibility = 11
 
repositories {
    mavenCentral()
}

dependencies {
	implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
}

In the above build script, I have not included any jar plugin and it will by default generate the *.jar and *-plain.jar files. Therefore, your app will generate two archives – lib.jar and lib-plain.jar under the folder build/libs.

Now if you want to generate only single jar then you need to turn off other jar. Again what type of jar you want to generate – thin or fat jar? Fat jar will have all the dependencies included in the archive whereas thin jar will not have any dependencies in the archive or library.

Generating Thin jar

To generate thin jar you can use the following configuration the build.gradle script:

...
jar {
	enabled = true
	classifier = ''
}

bootJar {
	enabled = false
}
...

The jar {...} configuration generates plain jar and bootJar {...} configuration generates the fat jar.

Notice I have used classifier = '' in the thin jar config, because I do not want -plain in the jar name. So, it will generate only lib.jar file with size 3 KB.

Generating Fat Jar

In the above configuration you have seen how to generate thin jar with custom name. Now I am going to generate fat jar.

...
jar {
	enabled = false
}

bootJar {
	enabled = true
}
...

The above configuration will generate fat jar with all dependencies and its size is 17131 KB.

Generating War

If you want to generate war file out of your Spring Boot application then you need to include the war plugin as shown below under the plugins {} section.

...
plugins {
    id 'war'
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"
}
...

The above configuration will generate two war files – lib.war and lib-plain.war. The size of lib.war is 17131 KB and the size of lib-plain.war file is 15283 KB.

Generating Thin War

Now I am going to generate thin war file using the following configuration. Remember you need war plugin for generating war archive.

...
war {
	enabled = true
	classifier = ''
}

bootWar {
	enabled = false
}
...

The above war configuration will generate only thin war file having size 15283 KB and plain will be removed from the name.

Generating Fat War

The fat jar will have all dependencies and size will be bigger than thin war file.

...
war {
	enabled = false
}

bootWar {
	enabled = true
}
...

Therefore, if you need to deploy your war file into external server then you can generate thin war. If you need to run war file as an executable then you can generate fat war file.

To generate jar or war files in the above configurations you need to run command gradlew clean build or gradle clean build or gradle build or gradlew build.

To run a fat jar or war archive you can simply execute command java -jar <jar or war file name>.

Source Code

The sample application for this example can be downloaded here.

Leave a Reply

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