Introduction
In this tutorial I will tell you how to create executable jar using gradle or maven build tool in Spring Boot application. Generally maven or gradle tool is used in Java applications to automate the build process. I will also see how to control the target jar name with version appended into it.
Spring Boot provides lots of benefits and one of the benefits is that you can make an executable jar out of web application.
Prerequisites
Java 8 or 12, Spring Boot 2.2.1/2.3.3, Maven 3.6.1/3.6.3 or Gradle 5.6/6.5.1
Using Maven
You can create executable jar using the following spring boot maven plugin. You can have other dependencies and plugins in the pom file.
<build>
<finalName>spring-boot-app-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8 or 12</source>
<target>8 or 12</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springBoot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
In the above file, the EL expression ${project.version}
is evaluated to the <version/>
tag of your pom file. The <finalName/>
tag produces the jar name in the target directory when you execute command mvn clean install
or mvn package
. The jar name would be, for example, spring-boot-app-0.0.1-SNAPSHOT
.
Maven compiler plugin is used to compile Java classes.
The ${springBoot.version}
is evaluated to the tag <springBoot.version/>
under <properties/>
section if you had mentioned otherwise you have to replace this by actual version, for example, 2.2.1.RELEASE
.
The above configuration for executable jar will find the single main class in the Spring Boot application and will create executable jar. If you have multiple classes with main method then it will throw exception. Therefore you can use the below configuration to specify which main class to use for your executable jar.
<build>
<finalName>spring-boot-app-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8 or 12</source>
<target>8 or 12</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springBoot.version}</version>
<configuration>
<mainClass>com.roytuts.starter.SpringBootHelloApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Using Gradle
You can make executable jar for your Spring Boot application in different ways. You will see different ways of making executable jar using the bootJar
task in gradle tool.
buildscript {
ext {
springBootVersion = '2.3.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 8 or 12
targetCompatibility = 8 or 12
bootJar {
baseName = "SpringBoot"
version = '0.0.1'
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
}
The above bootJar
task in the build.gradle script produces the jar with name SpringBoot-0.0.1.jar
under build/libs folder. The above configuration will check for single main class in the entire application. If there are multiple classes with main method then it will throw exception.
You can create specifying only the main class:
bootJar {
manifest {
attributes 'Start-Class': 'com.roytuts.spring.internationalization.SpringInternationalizationApp'
}
}
Or
bootJar {
mainClassName = 'com.roytuts.spring.internationalization.SpringInternationalizationApp'
}
You can create assigning name to the archiveName
variable:
bootJar {
archiveName = "$baseName-0.0.1-SNAPSHOT.$extension"
}
Where $baseName
is evaluated to the project name in settings.gradle file and $extension
is evaluated to jar.
You can also generate without version into the jar file mentioning main class name:
bootJar {
archiveName = "$baseName.$extension"
manifest {
attributes 'Main-Class': 'com.roytuts.spring.internationalization.SpringInternationalizationApp'
}
}
You can also generate with $baseName
and version:
bootJar {
baseName = "$baseName"
version = '0.1.0'
}
Where $baseName
is evaluated to the project name found in settings.gradle file.
That’s all about generation of executable jar from Spring Boot application using gradle or maven tool.
Thanks for reading.