使用 Spring 引导开发人员工具时,IntelliJ 无法启动 Spring Boot/Gradle 应用程序

IntelliJ fails to start Spring Boot/Gradle application when using Spring Boot developer tools

我有一个 Spring Boot 应用程序无法在 IntelliJ 2017.1.5 中启动,只要我将 Spring Boot 开发人员工具添加到 Gradle 构建中,如下所示:

dependencies {
  compile("org.springframework.boot:spring-boot-starter-web")
  compile("org.apache.httpcomponents:httpclient:4.5.3")
  compile("com.fasterxml.jackson.core:jackson-databind:2.8.8.1")
  compile("commons-validator:commons-validator:1.6")

  // This line breaks IntelliJ compatibility:
  optional("org.springframework.boot:spring-boot-devtools")

  optional("org.springframework.boot:spring-boot-configuration-processor")
  testCompile("org.springframework.boot:spring-boot-starter-test")
  testCompile("junit:junit:4.12")
}

没有 spring-boot-devtools 一切都很好,只要我添加它们,启动应用程序(在 IntelliJ 中刷新 Gradle 项目后)失败并显示 NoClassDefFoundError:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/web/client/RestTemplateBuilder
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.web.client.RestTemplateBuilder
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

我很确定这是一个 IntelliJ 问题,因为同一个应用程序在启用 Spring 引导开发人员工具的 Eclipse 中运行良好。

有什么解决办法吗?

我能够通过将 spring-boot-devtools 的依赖关系更改为:

来解决此问题
runtime("org.springframework.boot:spring-boot-devtools")

而不是

optional("org.springframework.boot:spring-boot-devtools")