[Recipe] Getting Started with Spring Tool Suite (STS) and Gradle

Problem: 

Need to install and configure STS with Gradle in a Windows machine.

Solution Summary: 

We can download STS and configre following the steps given.

Prerequisites: 
  1. Knowledge of Eclipse IDE (Refer to http://javajee.com/eclipse-ide-notes)
  2. Knowledge of Gradle (Refer to http://javajee.com/build-tools-for-java-applications)
Solution: 

Installing and setting up STS

  1. Download Spring Tool Suite latest version. Current download link is: http://spring.io/tools. Click to download ‘SPRING TOOL SUITE’.

  2. Once downloaded, double click to install. Click next, Accept License, Click Next, Select Installation Path, Click Next.

  3. Select defaults including tc server and click next. STS includes its own version of Tomcat called tc-server.

  4. Select a JDK (not JRE) and click Next.

  5. Continue to Click Next leaving defaults until the last page, where you can select Launch STS and click finish.

  6. While starting, STS will ask for workspace, like eclipse. STS is built on top of eclipse.

 

Configuring STS for Projects

You can configure STS for various projects such as Cloud Foundry Integration for eclipse, Spring Roo, Grails, Gradle etc. from the extension manager. You can go to extension manager by clicking ‘Manage Extensions’ in the Dashboard page. You can reach the Dashboard from the Help menu. We will see how to configure STS for Gradle projects and configuring for other projects will be similar.

 

Configuring STS for Gradle Projects

Step 1: To install Gradle support, from the Dashboard tab, click ‘Manage Extensions’. STS will show you a list of extensions in a separate tab.

Step 2: In the extensions tab, search for gradle (in the Find box on top of the tab page), select Gradle support and click install on bottom right. It will take some time to install.

In certain cases, your installation might fail due to time out or connectivity as: Communication with repository at http://dist.springsource.com/release/TOOLS/update/e4.3 failed. Most such issue are related to Internet security and firewall. I had to disable all internet security to install successfully.

Step 3: If step 2 succeed, continue to Click Next, reading and accepting the license agreements and click Finish. The Gradle plugin should now start downloading and installing. Once it finishes, you will be asked to restart—click Yes.

Workaround Alternative if step 2 or 3 fail

If previous steps fail even after disabling internet security, you can try steps 4 and 5 as workaround:

Step 4: If step 2 or 3 fail, Go to ‘Help’ > ‘Install new software’ > ‘work with’ and give the eclipse update site url in the ‘work with’ box (e.g. http://dist.springsource.com/release/TOOLS/update/3.4.0.RELEASE/e4.3). Now click ADD giving some name as STS Tools.

Step 5: From the list of modules from the update site, select the one you need. (E.g. Eclipse integration for Gradle). STS will now calculate the requirements and dependencies, and ask to Review the items to be installed. Click Next, Accept the license agreement, and click Finish. Installation may take some time depending on your network speed. Once it finishes, you will be asked to restart—click Yes.

Note: After following steps 4 and 5, you might face issues if the update site version is not same as that of your eclipse version.

 

Creating a new Gradle Project

Step 1: Once the plugin is installed successfully, you can create a Gradle project going to New > Project > Gradle > Gradle Project.

Step 2: You also need to select a Sample project to get started. Three samples are available as of this writing. Java Quickstart is a 'single project' sample project from the official Gradle distribution. Java API and Implementation is another 'single project' sample from the official Gradle distribution. It shows you how to customize your project's layout. Last one, Flat Java Multi Project, is a sample we created to demonstrates the 'multi-project' layout within Eclipse. There is a single root project that has most of the build logic, and a number of sub-projects containing the actual source code of the project. Many SpringSource projects such as spring-integration, spring-framework, spring-security etc. have Gradle builds that use this layout. The project name will go to root folder and you can’t select names for subprojects, but are given hardcoded names.

 

Importing a Gradle Project

Step 1: Once the plugin is installed successfully, you can create a Gradle project going to Import > Project > Gradle > Gradle Project.

Step 2: You need to ask Gradle to create project/subproject model by clicking Build Model button.  This model is needed to populate the project selection area below. Building the model may take some time.

Step 3: Once building model is done, you can select the project; if there are more than one projects, you can select the required projects.

Step 4: Finally click finish to do the importing. It might also take time if there are dependencies that need to be downloaded.

 

Issues and Resolutions

Issue 1

After installing STS and creating your first Gradle project as: New > Project > Gradle > Gradle Project, selecting sample project as "Java Quickstart", you might get build errors, including below errors:

Illegal entry in Gradle Dependencies: C:/Program Files/eclipse/unresolved dependency - commons-collections commons-collections 3.2

Illegal entry in Gradle Dependencies: C:/Program Files/eclipse/unresolved dependency - junit junit 4.+

 

Solution

Possible issue for the above error is that you are connecting to internet through a proxy. This happens if you are working from your company from behind the company firewall. You can either connect directly to internet without proxy (if possible) or tell gradle about your proxy as described in the page http://www.gradle.org/docs/current/userguide/build_environment.html.

After connecting to internet directly or configuring proxy, right click the project or the build.gradle file and  go to Gradle > Refresh All.  

It will download all the dependencies and you should see no errors. Refresh workspace or restart eclipse if the changes are not reflecting immediately after you see Build successful.

 

Summary

You can create and import gradle projects easily using the gradle project plugin. You can also run the build.gradle file from within STS.

You can use the gradle eclipse plugin to convert an existing gradle project into an eclipse project generating the eclipse required files. After converting you can import it into an eclipse workspace. You can read about the plugin @ http://www.gradle.org/docs/current/userguide/eclipse_plugin.html.  

Recipe Tags: 

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)