Search icon CANCEL
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

Creating Spring STS Eclipse projects using Gradle

Another option in building Spring 5.0 projects is through the use of Gradle. STS Eclipse includes Gradle as one of its tooling and project management tools. In our case, we will be installing an STS Eclipse module extension in the easiest way in order to fully use Gradle.

Getting started

Install the Gradle module extension in our STS Eclipse 3.8 in order to clean, build, and deploy projects in Gradle. Perform the following steps:

  1. Click the Dashboard toolbar option of your Eclipse. After clicking, you will be opening the main dashboard of the IDE:
  1. On the dashboard, look for IDE EXTENSIONS and click that button. A new window showing all the available Eclipse STS extensions will pop up. Click on Gradle (STS Legacy) Support and install it:
  1. The next steps will just be similar to installing new Eclipse plugins. Just click the Install button and follow the installation wizard. Eclipse needs to be restarted after a successful installation.
  1. If you want to change the Gradle distribution, you can replace the Eclipse embedded Gradle installation with some new version at https://gradle.org/gradle-download/. Or you can shift to Eclipse Buildship with Gradle Plugin if some of the files are not supported by the installed Gradle plugin:

How to do it...

After installing the Gradle STS extension, perform the following steps to install the Spring Gradle project:

  1. After installing, you are ready to create a Gradle project for Spring development. Go to the New project wizard (Ctrl-N) of STS Eclipse and create a Gradle project.
  2. On the Gradle Project wizard, assign a name for your project and choose Java Quickstart for the Sample project option. Click Finish and it will take a while to create, build, clean, and install your Gradle STS project.
  3. Delete unnecessary project files. Right-click on the project and click on Gradle (STS) | Refresh all.
  1. Open the build.gradle and overwrite the existing configuration with this:
apply plugin: 'eclipse'
apply plugin: "war"

sourceCompatibility = 1.8
version = '1.0'

war {
baseName = 'ch02-gradle'
version = '1.0'
}

sourceCompatibility = 1.8

repositories {
mavenCentral()
jcenter()
}
  1. Right-click on the project and click Gradle (STS) | Tasks Quick Launcher to run the Gradle Task Launcher. Using the launcher, clean and build the project for the first time:
  1. Finally, at this point, you have created your Spring Gradle project which will look like this:

How it works...

The most important configuration file in a Gradle project is the build.gradle. First, we have to add apply plugin: 'java' to tell Gradle that Java is the core language in building the scripts, testing the codes, executing compiled objects, creating Javadoc, and deploying JAR or WAR files. Since all project management and tooling depends on STS Eclipse, there is a need to add apply plugin: 'eclipse' in order to tell Gradle that all descriptors are Eclipse-specific and can be integrated and executed with Eclipse core and extension plugins. A by-product of its project installation and execution are the Eclipse folders such as .project, and .classpath. And since this is a web development project, we need to apply plugin: 'war' to indicate that the deployment is at WAR mode. Later on we will be adding some plugins needed in the development of our recipes.

In the properties section, the configuration must tell Gradle the version of the JDK through the sourceCompatibility property. Another property is the version of the WAR or project deployment, which is at first set to 1.0. Since the mode of deployment is the web, Java must know the name and the version of the WAR file to be generated.

On the repositories section, the configuration must define where to find the dependencies, which are at JCenter and MavenCentral.

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 €18.99/month. Cancel anytime