How it works...

As you can see, getting the packaged executable JAR file is fairly straightforward. All the magic is already coded and provided to us as part of the Spring Boot Gradle plugin. The addition of the plugin adds a number of tasks, which allow us to package the Spring Boot application, run it and build the JAR, TAR, WAR files, and so on. For example, the bootRun task, which we have been using throughout this book, is provided by the Spring Boot Gradle plugin, among others. We can see a complete list of the available Gradle tasks by executing ./gradlew tasks. When we run this command, we will get the following output:

    ------------------------------------------------------------
    All tasks runnable from root project
    ------------------------------------------------------------
    
    Application tasks
    -----------------
    bootRun - Run the project with support for auto-detecting main 
class and reloading static resources
run - Runs this project as a JVM application Build tasks ----------- assemble - Assembles the outputs of this project. bootJar - Assembles an executable jar archive containing the main
classes and their dependencies.
build - Assembles and tests this project. buildDependents - Assembles and tests this project and all projects
that depend on it.
buildNeeded - Assembles and tests this project and all projects it
depends on.
classes - Assembles classes 'main'. clean - Deletes the build directory. jar - Assembles a jar archive containing the main classes. testClasses - Assembles classes 'test'. Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] Distribution tasks ------------------ assembleBootDist - Assembles the boot distributions assembleDist - Assembles the main distributions bootDistTar - Bundles the project as a distribution. bootDistZip - Bundles the project as a distribution. distTar - Bundles the project as a distribution. distZip - Bundles the project as a distribution. installBootDist - Installs the project as a distribution as-is. installDist - Installs the project as a distribution as-is.

The preceding output is not complete; I've excluded the non-relevant task groups such as IDE, documentation, and so on, but you will see them on your console. In the task list, we will see tasks such as bootRun, bootJar, and others. These tasks have been added by the Spring Boot Gradle plugin and executing them gets the required Spring Boot steps added to the build pipeline. You can see the actual task dependency if you execute ./gradlew tasks --all, which will not only print the visible tasks, but also the depended, internal tasks, and the task dependencies. For example, when we were running the build task, all the following dependent tasks were executed as well:

    build - Assembles and tests this project. [assemble, check]
    assemble - Assembles the outputs of this project. [bootJar, 
distTar, distZip, jar]

You can see that the build task will execute the assemble task, which in turn will call bootJar, where the creation of the Uber JAR is actually taking place.

The plugin also provides a number of very useful configuration options. While I am not going to go into detail about all of them, I'll mention the two that I find very useful:

bootJar { 
  classifier = 'exec' 
  baseName = 'bookpub' 
} 

This configuration allows us to specify the executable JAR file classifier, along with the JAR baseName, allowing for having the regular JAR contain just the application code and the executable JAR with the classifier in the name, bookpub-0.0.1-SNAPSHOT-exec.jar.

Another useful configuration option allows us to specify which dependency JARs require unpacking because, for some reason, they can't be included as nested inner JARs. This comes in very handy when you need something to be available in the system Classloader such as setting a custom SecurityManager via the startup system properties:

bootJar { 
  requiresUnpack = '**/some-jar-name-*.jar' 
} 

In this example, the contents of the some-jar-name-1.0.3.jar dependency will be unpacked into a temporary folder on a filesystem when the application is launched.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.12.242