If the project is written in Gradle, there will be some modification to be done in our gradle.build configuration details.
Deploying Spring projects using Gradle
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...
- 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' } }
- Add the following libraries to the classpath:
apply plugin: 'com.bmuschko.tomcat' apply plugin: 'com.bmuschko.tomcat-base'
- (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' }
}
- (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'] } } }
- 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" } }
- 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.