Spring Boot + Spring-Loaded (IntelliJ, Gradle)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24371111/
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
Spring Boot + Spring-Loaded (IntelliJ, Gradle)
提问by kuceram
I'd like to use hot swap with my Spring Boot project. Somehow I am not able to make it working by running it in my IDE (IntelliJ), despite of having this topic covered by documentation. I simply run the class with my main method with VM attributes:
我想在我的 Spring Boot 项目中使用热插拔。不知何故,尽管文档涵盖了这个主题,但我无法通过在我的 IDE (IntelliJ) 中运行它来使其工作。我只是使用带有 VM 属性的 main 方法运行该类:
-javaagent:/path/to/jar/springloaded.jar -noverify
My question is, how do I make it work? :-)
我的问题是,我如何使它工作?:-)
Further question is how to use spring loaded with Gradle and IntelliJ. I find it quite inconvenient to force the developer to download the JAR manually, place it somewhere and point to it with a JVM parameter. Is there any better way (should I configure my own task which does the job and run it from my IDE as a Gradle task)?
进一步的问题是如何使用装有 Gradle 和 IntelliJ 的弹簧。我发现强制开发人员手动下载 JAR,将其放置在某处并使用 JVM 参数指向它非常不方便。有没有更好的方法(我应该配置自己的任务来完成这项工作并从我的 IDE 作为 Gradle 任务运行它)?
采纳答案by Deigote
You need to configure the project as stated in the documentation:
您需要按照文档中的说明配置项目:
After that, you must configure your IDE to output the compiled classes in build/classes/main (with Idea plugin, you can configure the outputDir as specified in the above link, and then invoke gradle idea
to have it done).
之后,您必须将IDE配置为在build/classes/main中输出已编译的类(使用Idea插件,您可以按照上述链接中的指定配置outputDir,然后调用gradle idea
以完成)。
Then, if you launch the task (run / bootRun) or run the main class from the IDE's using the debug mode, hot code reloading should work when a class is compiled.
然后,如果您启动任务 (run / bootRun) 或使用调试模式从 IDE 运行主类,则在编译类时热代码重新加载应该起作用。
The gotcha here is that IntelliJ, unlike Eclipse, doesn't automatically compile a class when it is saved (even if you configure the compiler to "Build on save", it won't do it when is Running/Debugging). This is apparently a design decission made by IntelliJ - as stated here Intellij IDEA Java classes not auto compiling on save(CrazyCoder answer) .
这里的问题是 IntelliJ 与 Eclipse 不同,在保存类时不会自动编译它(即使您将编译器配置为“在保存时构建”,它在运行/调试时也不会这样做)。这显然是 IntelliJ 做出的设计决策- 如此处所述Intellij IDEA Java classes not auto compiling on save(CrazyCoder answer) 。
It would be ideal if spring boot provided a configuration option to monitor your source code files and recompile them when they change - that is what Grails does. But I think such a think does not exist yet, and maybe is not even possible to combine that with gradle, which is the responsible of managing the classpath and that kind of things.
如果 spring boot 提供一个配置选项来监视源代码文件并在它们更改时重新编译它们,那将是理想的 - 这就是 Grails 所做的。但我认为这样的想法还不存在,甚至可能无法将其与负责管理类路径之类的 gradle 结合起来。
So there are two options as far as I can tell:
因此,据我所知,有两种选择:
- You remember to compile everything you edit (adding an easier Compile shortcut as suggested in the previous StackOverflow link might help).
- You put some filesystem monitor (inotify-tools for Linux, launchd for Mac OS X are examples) that invokes gradle compileJava/compileGroovy when a change is detected in any source code file.
- 您记得编译您编辑的所有内容(添加一个更简单的 Compile 快捷方式,如上一个 StackOverflow 链接中的建议可能会有所帮助)。
- 您放置了一些文件系统监视器(Linux 的inotify-tools,Mac OS X 的launchd 是示例),当在任何源代码文件中检测到更改时调用gradle compileJava/compileGroovy。
First is tedious, second is slow :) . Actually there's another option: you change your IDE :-D (or install the EclipseMode IntelliJ plugin).
第一个很乏味,第二个很慢:)。实际上还有另一种选择:您更改 IDE :-D(或安装 EclipseMode IntelliJ 插件)。
回答by geoand
If one wants to be able to run the application solely from IntelliJ (using Right Click -> Debug on the main method) and not involve Spring Boot's Gradle tasks at all, you simply need to do the following:
如果您希望能够仅从 IntelliJ 运行应用程序(在 main 方法上使用右键单击 -> Debug)并且根本不涉及 Spring Boot 的 Gradle 任务,您只需执行以下操作:
- Configure the run configuration in IntelliJ to use the SpringLoaded agent. This is easy to do and an example is shown in the following screenshot:
- 在 IntelliJ 中配置运行配置以使用 SpringLoaded 代理。这很容易做到,下面的屏幕截图显示了一个示例:
Notice how I have added a VM Option
: -javaagent:/path/to/springloaded-${version}.jar -noverify
(which you can download here)
请注意我是如何添加一个VM Option
:(-javaagent:/path/to/springloaded-${version}.jar -noverify
您可以在此处下载)
- Debug using Right Click -> Debug like the following screenshot:
- 使用右键单击->调试进行调试,如下图所示:
- Everytime you make a change and want to reload it, just compile the project. The default shortcut is Cntrl+F9, but you can also access it from the menu
Build
->Make Project
- 每次进行更改并想要重新加载它时,只需编译项目即可。默认快捷方式是Cntrl+ F9,但您也可以从菜单中访问它
Build
->Make Project
回答by Nándor Krácser
I managed to do this with IDEA in a Maven project, it should work with the Gradle version as well I guess, my procedure was the following.
我设法在 Maven 项目中使用 IDEA 做到了这一点,我猜它也应该与 Gradle 版本一起使用,我的程序如下。
- Settings -> Compiler -> Make project automatically (only works while not running/debugging !)
- Start the project with the sprint-boot-plugin outside of the IDE (a trick because of the above sentence).
- 设置 -> 编译器 -> 自动生成项目(仅在不运行/调试时有效!)
- 使用 IDE 外部的 sprint-boot-plugin 启动项目(因为上面这句话的一个技巧)。
The Maven plugins setup looks like the following:
Maven 插件设置如下所示:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Now change some code and reload the page.
现在更改一些代码并重新加载页面。
回答by sparkyspider
I'm running SpringBoot and Gradle in IntelliJ Idea. Auto-reloading is working 100%
我在 IntelliJ Idea 中运行 SpringBoot 和 Gradle。自动重新加载工作 100%
- Static Content is autoreloading (instantly)
- Thymeleaf is autoreloading (instantly)
- Controllers / Java Files require me to hit the "build" button (takes a few seconds), but does not require a restart - it's just to compile the files, so Spring Loaded can pick them up.
- 静态内容正在自动重新加载(立即)
- Thymeleaf 正在自动重新加载(立即)
- 控制器/Java 文件需要我点击“构建”按钮(需要几秒钟),但不需要重新启动——它只是编译文件,所以 Spring Loaded 可以选择它们。
Step 1: Get SpringLoaded going
第 1 步:启动 SpringLoaded
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.2.1.RELEASE"
classpath 'org.springframework:springloaded:1.2.1.RELEASE'
}
}
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'idea'
apply plugin: 'spring-boot'
repositories {
jcenter()
mavenLocal()
mavenCentral()
}
mainClassName = 'com.noxgroup.nitro.NitroApp'
idea {
module {
inheritOutputDirs = false
outputDir = file("$buildDir/classes/main/")
}
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:1.2.1.RELEASE")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
}
jar {
baseName = 'org.noxgroup-nitro'
version = '0.1.0'
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
Step 2: Create an Application.properties
第 2 步:创建一个 Application.properties
And add the following:
并添加以下内容:
spring.thymeleaf.cache=false
Step 3: Run the bootRun task
步骤 3:运行 bootRun 任务
(Not just the standard run task - this adds SpringLoaded functionality)
(不仅仅是标准运行任务 - 这增加了 SpringLoaded 功能)
Step 4: Compile your Java
第 4 步:编译您的 Java
Make your Java files by hitting "Make Project" or pressing "Ctrl/Command + F9
通过点击“Make Project”或按“Ctrl/Command + F9”来制作你的 Java 文件
回答by Matthew Payne
In later versions of gradle you can start a command window and run
在更高版本的 gradle 中,您可以启动命令窗口并运行
gradle -t classes
This will start a process that look for changes to source code and recompile them.
这将启动一个查找源代码更改并重新编译它们的过程。
In another run.
在另一个运行。
gradle bootRun
Any changes to java or groovy src will automatically be recomplied. While not intellij specific, your good to go with intellij. You could even make do with you favorite text editor (e.g. submlime, atom ).
对 java 或 groovy src 的任何更改都将自动重新编译。虽然不是 Intellij 特定的,但您可以使用 Intellij。您甚至可以使用您最喜欢的文本编辑器(例如 submlime、atom)。