Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Layered Fat Jar and Gradle Plugin layertools jarmode

Spring boot 2.3.0 has the layered fat jars which are optimized for container image builds. Now, the Spring documentation discusses only Maven build tool, and not Gradle.

Please, what do we need to do to the Gradle plugin to allow it to build a "layered jar" for spring boot?

For example: if extracting a jar with the command java -Djarmode=layertools -jar app.jar extract which has been made with Gradle Spring boot plugin id("org.springframework.boot") version "2.3.0.RELEASE" it fails with message: Unsupported jarmode 'layertools'. Basically if the jar has been built with ./gradlew bootJar it seems layered jar is not active by default.

Extracting a layered jar should result in the following directories, and these are then copied into another container image for execution, and only if lower layers have changed does the caching system have to re-read them. Layered jar extracted with -Djarmode=layertools flag:

./dependencies/
./snapshot-dependencies/
./resources/
./application/

The blog at spring Spring Blog 2.3.0.ML discusses Maven build tool and the configuration:

<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
    <layout>LAYERED_JAR</layout>
</configuration>

How do we enable the same configuration for Gradle plugin?

like image 671
kaicarno Avatar asked Jun 09 '20 17:06

kaicarno


People also ask

What is spring boot Gradle plugin?

The Spring Boot Gradle Plugin provides Spring Boot support in Gradle. It allows you to package executable jar or war archives, run Spring Boot applications, and use the dependency management provided by spring-boot-dependencies .

What is bootJar in Gradle?

bootJar on the other hand is a specific task added by Spring Boot Gradle plugin that, when the java plugin is present, attaches itself to the assemble lifecycle task. The assemble task is automatically configured to depend upon the bootJar task so running assemble (or build ) will also run the bootJar task.


2 Answers

The documentation is available for how to activate the layered fat jar for spring boot applications with the spring-boot-gradle-plugin.

See the details at Packaging Layered Jars with Gradle.

In the build.gradle file:

// Groovy solution
bootJar {
    layered()
}


// Kotlin solution
tasks.getByName<BootJar>("bootJar") {
    layered()
}

Hope this helps.

like image 150
kaicarno Avatar answered Sep 18 '22 02:09

kaicarno


You can also find more information in here:

https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/gradle-plugin/reference/html/#packaging-layered-jars

bootJar {
  layered {
    enabled = true

    application {
      intoLayer("spring-boot-loader") {
        include "org/springframework/boot/loader/**"
      }
      intoLayer("application")
    }
    
    dependencies {
      intoLayer("snapshot-dependencies") {
        include "*:*:*SNAPSHOT"
      }
      intoLayer("dependencies")
    }

    layerOrder = ["dependencies", "spring-boot-loader", "snapshot-dependencies", "application"]
  }
}

You will find corresponding layer names in layers.idx file inside you JAR.

like image 23
mate00 Avatar answered Sep 20 '22 02:09

mate00