How to start with STS and Gradle
Asked Answered
T

3

21

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?

Thousandfold answered 1/2, 2014 at 11:15 Comment(0)
M
3

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.

Monitory answered 22/5, 2014 at 17:37 Comment(2)
It is therefore my understanding that I cannot create a new Gradle-based project from scratch, but I have to base my projects on a sample included in a Getting Started guide. Is it correct?Thousandfold
You can create one from scratch, but you have to put all the needed files / folders etc in place yourself. This may be tough. Your question seems to want a 'springy' project. So one of the spring.io guides is likely a good starting point for you. There's isn't currently a 'create spring template project' equivalent to create a gradle instead of maven project.Monitory
M
3

There is no direct way to create a Spring project based on Gradle in Eclipse-Kepler Version, but there is a way around.

Create a Spring Project based on Maven. Now Go to the folder which contains the file 'pom.xml'. Run 'gradle init' command (For this you should have gradle installed on your system).

After this disable Maven Nature from contextual menu of the project (Right click on the project > Maven > Disable Maven Nature). Now convert this project to Gradle from contextual menu again (Right click on the project > Configure > Convert to Gradle Project)

You can find all the tasks are available in Gradle Tasks and all dependencies are present in build.gradle

Mcbrayer answered 29/6, 2015 at 21:13 Comment(0)
N
2

@Giulio, you couldn't express better my thoughts on this.

I can link a partial solution here: Gradle – Spring MVC Web Project Example. Partial because the created project won't look as a Spring project inside STS, so I fear you will loose something.

I hope to see in STS an automated way to create a Spring project that uses Gradle as soon as possible. In the meanwhile I'll rely on Maven...

Neuropath answered 25/11, 2014 at 9:19 Comment(2)
Thanks for the link. It is indeed a sort-of subpar kludgy approach that I would like to avoid at all, if possible. Lately, I've been reading about how good NetBeans support for Gradle is (by using an external plug-in); I have not checked out its Spring MVC support though, so I'm left wondering if NetBeans could be a viable IDE for writing such applications.Thousandfold
I think I'm going to burst into tears soon. I just switched from Netbeans to STS helping there would be better support for Gradle. The thing I'm wanting to use it SpringLoaded which I can't seem to get working in Netbeans.Mcnair

© 2022 - 2024 — McMap. All rights reserved.