Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Spring 5.0 Cookbook

You're reading from   Spring 5.0 Cookbook Recipes to build, test, and run Spring applications efficiently

Arrow left icon
Product type Paperback
Published in Sep 2017
Publisher Packt
ISBN-13 9781787128316
Length 670 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Sherwin John C. Tragura Sherwin John C. Tragura
Author Profile Icon Sherwin John C. Tragura
Sherwin John C. Tragura
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Getting Started with Spring FREE CHAPTER 2. Learning Dependency Injection (DI) 3. Implementing MVC Design Patterns 4. Securing Spring MVC Applications 5. Cross-Cutting the MVC 6. Functional Programming 7. Reactive Programming 8. Reactive Web Applications 9. Spring Boot 2.0 10. The Microservices 11. Batch and Message-Driven Processes 12. Other Spring 5 Features 13. Testing Spring 5 Components

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.

You have been reading a chapter from
Spring 5.0 Cookbook
Published in: Sep 2017
Publisher: Packt
ISBN-13: 9781787128316
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image