Book Image

Spring 5.0 Cookbook

By : Sherwin John C. Tragura
Book Image

Spring 5.0 Cookbook

By: Sherwin John C. Tragura

Overview of this book

The Spring framework has been the go-to framework for Java developers for quite some time. It enhances modularity, provides more readable code, and enables the developer to focus on developing the application while the underlying framework takes care of transaction APIs, remote APIs, JMX APIs, and JMS APIs. The upcoming version of the Spring Framework has a lot to offer, above and beyond the platform upgrade to Java 9, and this book will show you all you need to know to overcome common to advanced problems you might face. Each recipe will showcase some old and new issues and solutions, right from configuring Spring 5.0 container to testing its components. Most importantly, the book will highlight concurrent processes, asynchronous MVC and reactive programming using Reactor Core APIs. Aside from the core components, this book will also include integration of third-party technologies that are mostly needed in building enterprise applications. By the end of the book, the reader will not only be well versed with the essential concepts of Spring, but will also have mastered its latest features in a solution-oriented manner.
Table of Contents (20 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Deploying Spring projects using Gradle


If the project is written in Gradle, there will be some modification to be done in our gradle.build configuration details.

Getting started

Validate that your Tomcat with TLS-enabled connection is working by running https://localhost:8080/ on any browser. Also, check if your STS Gradle project is clean and updated.

How to do it...

  1. Open the gradle.build file and add the buildScript() function that accepts a closure containing all libraries needed to be referenced in classpath. At this step, we need to import gradle-tomcat-plugin under the group com.bmuschko:
buildscript { 
    repositories { 
        jcenter() 
    } 
    dependencies { 
        classpath 'com.bmuschko:gradle-tomcat-plugin:2.0' 
    } 
} 
  1. Add the following libraries to the classpath:
apply plugin: 'com.bmuschko.tomcat' 
apply plugin: 'com.bmuschko.tomcat-base' 
  1. (Optional) Add the following Tomcat 9.0 libraries that enable Gradle to run embedded Tomcat through the tomcatRun() and tomatRunWar() functions:
dependencies { 
    def tomcatVersion = '9.0.0.M9' 
    tomcat "org.apache.tomcat.embed:tomcat-embed 
    core:${tomcatVersion}", 
    "org.apache.tomcat.embed:tomcat-embed-logging- 
    juli:${tomcatVersion}" 
    tomcat("org.apache.tomcat.embed:tomcat-embed- 
    jasper:${tomcatVersion}") { 
        exclude group: 'org.eclipse.jdt.core.compiler', 
             module: 'ecj' 
    } 
} 
  1. (Optional) Configure Tomcat 9 details through the tomcat() function:
tomcat { 
    httpsPort = 8443 
    enableSSL = true 
    users { 
        user { 
            username = 'packt' 
            password = 'packt' 
            roles = ['manager-gui', 'manager-script'] 
        } 
    } 
} 
  1. To deploy the project into the installed Tomcat 9, create a Gradle task deploy that copies the WAR file to the /webapp folder:
task deploy (dependsOn: war){ 
    copy { 
        from "build/libs" 
        into 
        "C:\MyFiles\Development\Servers\Tomcat9.0\webapps" 
        include "*.war" 
    } 
} 
  1. You can now run deploy in the Gradle Task Launcher.

How it works...

When it comes to building projects with conflicting versions of libraries, Gradle is one of those build tools that can satisfy any structure and state of project deployment and management. Since it is not written in XML, Gradle can provide logic in building classpath and project dependencies.

Gradle is efficient when it comes to incremental builds where the current and previous changes in deployment files are monitored. And when it comes to different repositories, Gradle can monitor changes of artifacts through effective repository-aware caches. In general, Gradle is advanced when it comes to repository management and project deployment than Maven.

Gradle is written in Groovy; thus, the build scripts are declarative, readable, and straightforward and can provide developers with easier conventions and philosophy of deployment.

First, Gradle must build the needed classpath libraries that are the main dependencies to the deployment, which happen to be com.bmuschko:gradle-tomcat-plugin:2.0. After building the external library, import the following plugins: com.bmuschko.tomcat and com.bmuschko.tomcat-base. Inject a closure to the tomcat() function that details all the needed configuration before the deployment. The custom Gradle task deploy takes all the configuration loaded by the tomcat() and war() functions. Running deploy in Gradle (STS) | Gradle Task Launcher will copy the WAR file found in build/libs to the Tomcat instance.