How to start with STS and Gradle

Giulio Piancastelli picture Giulio Piancastelli · Feb 1, 2014 · Viewed 16.5k times · Source

Installed Kepler-based STS 3.4.0.RELEASE (current latest version) and the Gradle Support package from the Dashboard Extensions. Now I would like to create a Spring MVC project from scratch that uses Gradle as its build and dependency management tool instead of Maven.

I start by creating a Spring Project with a Spring MVC Project Template, but apparently this is based on Maven, and there is no option in any wizard screen to switch the project to Gradle. So, after creation, I right-click on the project and select Configure > Convert to Gradle Project. However, contrary to my expectations, the project still holds its Maven nature, dependencies in the project still belong to Maven despite Gradle having dependency resolution enabled, the pom.xml file is still there, and there is no build.gradle file anywhere to be seen. Eclipse's Console reports a successful build, which I believe comes from Gradle: but based on what? When I open the Gradle Tasks view, it is empty; if I try to refresh it, a message saying that the model is not yet available appears, just before triggering another successful build. Then, when I try and remove the Maven nature from the project, errors start to appear, about classes and tag library descriptors that are not found anymore. I select Gradle > Refresh Dependencies from the contextual menu of the project, but it has no effect.

I try the other way around: create a Gradle project from scratch. Unfortunately, Gradle templates do not cover Spring projects at all, and adding the Spring nature afterwards has no tangible effect, nor I am able to find a way to apply the Spring MVC Project Template to this newly created Gradle project.

On the Internet, everyone talks about how powerful the import facility of the Gradle Eclipse plug-in is, and how to create a Gradle project outside the IDE, then create the necessary files for Eclipse to be able to import the project, finally import it and be able to develop using the IDE. The sample project used in these tutorials is not a Spring MVC project, so I would be left on my own as the creation of the directory hierarchy and further configuration are concerned.

So I wonder: is such a thing as creating a Spring MVC Project with Gradle from within STS even possible at this time? Given that STS, Spring, and the Eclipse Gradle plug-in are developed by the same vendor, you would bet it is, because it seems such a basic feature that would hardly be missing from even a first release of STS/Spring/Gradle integration tools. So, did I miss something when trying to achieve my goal? And, just in case this is not possible, are there any other means to create a Gradle-based Spring MVC project from within STS? Or just properly convert a Maven-based one to Gradle?

Answer

Kris picture Kris · May 22, 2014

This question is old and there probably wasn't a good answer to your question when it was asked, but today there is.

You can import gradle-based versions of any of the spring.io getting started guides.

To import these projects most easily you will need a recent version of STS and Gradle Tooling (e.g. release 3.5.1). Then use the 'Import Getting Started Content' Wizard from the 'File >> New' menu. The wizard lets you search the available guides and import their sample projects configured either as maven or gradle projects.