eclipse 如何开始使用 STS 和 Gradle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21497337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start with STS and Gradle
提问by Giulio Piancastelli
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.
安装了基于 Kepler 的 STS 3.4.0.RELEASE(当前最新版本)和来自仪表板扩展的 Gradle 支持包。现在我想从头开始创建一个 Spring MVC 项目,它使用 Gradle 作为其构建和依赖项管理工具而不是 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.
我首先使用 Spring MVC 项目模板创建一个 Spring 项目,但显然这是基于 Maven,并且在任何向导屏幕中都没有选项可以将项目切换到 Gradle。因此,在创建后,我右键单击该项目并选择配置 > 转换为 Gradle 项目。然而,出乎我的意料,该项目仍然保留了它的Maven性质,尽管Gradle启用了依赖解析,但项目中的依赖项仍然属于Maven,pom.xml
文件仍然存在,并且没有build.gradle
任何地方可以看到文件。Eclipse 的控制台报告了一个成功的构建,我相信它来自 Gradle:但基于什么? 当我打开 Gradle Tasks 视图时,它是空的;如果我尝试刷新它,则会在触发另一个成功构建之前出现一条消息,指出该模型尚不可用。然后,当我尝试从项目中删除 Maven 特性时,错误开始出现,关于不再找到的类和标记库描述符。我从项目的上下文菜单中选择 Gradle > Refresh Dependencies,但没有任何效果。
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.
我尝试了另一种方式:从头开始创建一个 Gradle 项目。不幸的是,Gradle 模板根本没有涵盖 Spring 项目,并且之后添加 Spring 性质并没有明显的效果,我也无法找到将 Spring MVC 项目模板应用于这个新创建的 Gradle 项目的方法。
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.
网上大家都在说Gradle Eclipse插件的导入功能有多强大,如何在IDE外创建Gradle项目,然后创建Eclipse能够导入项目的必要文件,最后导入并且能够使用IDE进行开发。这些教程中使用的示例项目不是 Spring MVC 项目,因此我将自己留在目录层次结构的创建和进一步配置中。
So I wonder: is such a thing as creating a Spring MVC Project with Gradle from within STS even possibleat 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?
所以我想知道:此时是否有可能在STS 中使用 Gradle 创建 Spring MVC 项目?考虑到 STS、Spring 和 Eclipse Gradle 插件是由同一家供应商开发的,您会打赌它是,因为它似乎是一个基本特性,即使是 STS/Spring/Gradle 集成的第一个版本也几乎不会缺少工具。那么,我在努力实现目标时是否错过了什么?而且,以防万一这是不可能的,是否还有其他方法可以从 STS 中创建基于 Gradle 的 Spring MVC 项目?或者只是将基于 Maven 的正确转换为 Gradle?
回答by Kris
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.
您可以导入任何 spring.io入门指南的基于 gradle 的版本。
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.
要最轻松地导入这些项目,您需要最新版本的 STS 和 Gradle Tooling(例如 3.5.1 版)。然后使用“文件>>新建”菜单中的“导入入门内容”向导。该向导允许您搜索可用指南并导入其配置为 maven 或 gradle 项目的示例项目。
回答by bluish
@Giulio, you couldn't express better my thoughts on this.
@Giulio,你无法更好地表达我对此的想法。
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.
我可以在此处链接部分解决方案:Gradle – Spring MVC Web 项目示例。部分原因是创建的项目看起来不像 STS 中的 Spring 项目,所以我担心你会丢失一些东西。
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...
我希望能尽快在 STS 中看到一种自动化的方式来创建一个使用 Gradle 的 Spring 项目。与此同时,我将依靠 Maven ...
回答by Vaibhav Gupta
There is no direct way to create a Spring project based on Gradle in Eclipse-Kepler Version, but there is a way around.
在 Eclipse-Kepler 版本中没有直接创建基于 Gradle 的 Spring 项目的方法,但是有一种方法。
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).
基于Maven创建一个Spring项目。现在转到包含文件“pom.xml”的文件夹。运行“gradle init”命令(为此你应该在你的系统上安装了gradle)。
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)
在此之后,从项目的上下文菜单中禁用 Maven Nature(右键单击项目 > Maven > Disable Maven Nature)。现在再次从上下文菜单中将此项目转换为 Gradle(右键单击项目 > 配置 > 转换为 Gradle 项目)
You can find all the tasks are available in Gradle Tasks and all dependencies are present in build.gradle
您可以在 Gradle Tasks 中找到所有可用的任务,并且所有依赖项都在 build.gradle 中