org.springframework.boot.web.support不存在

我正在将构建系统从maven更改为gradle以进行spring boot项目。 我得到了这个堆栈跟踪

19:03:08: Executing external task 'bootRun'... /home/dac/proj/spring-boot-master/spring-boot-samples/spring-boot-sample-jetty-jsp/src/main/java/sample/jetty/jsp/SampleJettyJspApplication.java:22: error: package org.springframework.boot.web.support does not exist import org.springframework.boot.web.support.SpringBootServletInitializer; ^ /home/dac/proj/spring-boot-master/spring-boot-samples/spring-boot-sample-jetty-jsp/src/main/java/sample/jetty/jsp/SampleJettyJspApplication.java:25: error: cannot find symbol public class SampleJettyJspApplication extends SpringBootServletInitializer { ^ symbol: class SpringBootServletInitializer /home/dac/proj/spring-boot-master/spring-boot-samples/spring-boot-sample-jetty-jsp/src/main/java/sample/jetty/jsp/SampleJettyJspApplication.java:27: error: method does not override or implement a method from a supertype @Override ^ 3 errors :compileJava FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':compileJava'. > Compilation failed; see the compiler error output for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED Total time: 2.334 secs Compilation failed; see the compiler error output for details. 19:03:10: External task execution finished 'bootRun'. 

我的build.gradle

 buildscript { repositories { mavenCentral() } dependencies { classpath "org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE" classpath 'org.springframework:springloaded:1.2.5.RELEASE' } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'spring-boot' apply plugin: 'rebel' buildscript { repositories { mavenCentral() } dependencies { classpath group: 'org.zeroturnaround', name: 'gradle-jrebel-plugin', version: '1.1.3' } } jar { baseName = 'gs-spring-boot' version = '0.1.0' } repositories { mavenCentral() } sourceCompatibility = 1.8 targetCompatibility = 1.8 dependencies { // tag::jetty[] compile("org.springframework.boot:spring-boot-starter-web") { exclude module: "spring-boot-starter-tomcat" } compile("org.springframework.boot:spring-boot-starter-jetty") // end::jetty[] // tag::actuator[] compile("org.springframework.boot:spring-boot-starter-actuator") testCompile("org.springframework.boot:spring-boot-starter-test") testCompile("junit:junit") } // change default IntelliJ output directory for compiling classes idea { module { inheritOutputDirs = false outputDir = file("$buildDir/classes/main/") } } task wrapper(type: Wrapper) { gradleVersion = '2.3' } 

我的pom.xml

   4.0.0   org.springframework.boot spring-boot-samples 1.4.0.BUILD-SNAPSHOT  spring-boot-sample-jetty-jsp war Spring Boot Jetty JSP Sample Spring Boot Jetty JSP Sample http://projects.spring.io/spring-boot/  Pivotal Software, Inc. http://www.spring.io   ${basedir}/../.. /    org.springframework.boot spring-boot-starter-web   org.springframework.boot spring-boot-starter-tomcat     org.springframework.boot spring-boot-starter-validation   org.apache.tomcat.embed tomcat-embed-el     org.springframework.boot spring-boot-starter-jetty <!--provided-->   javax.servlet jstl   org.eclipse.jetty apache-jsp <!--provided-->   org.springframework.boot spring-boot-starter-test test      org.springframework.boot spring-boot-maven-plugin  true    org.apache.maven.plugins maven-surefire-plugin  false      

我应该如何使包装可用于gradle?

这可能是源代码中的一个导入问题 – 您的Gradle构建脚本使用Spring Boot 1.3.6.RELEASE,其中SpringBootServletInitializer具有以下完全限定名称:

 org.springframework.boot.context.web.SpringBootServletInitializer 

但是,您的Maven pom.xml使用Spring Boot 1.4.0.BUILD-SNAPSHOT,其中包名更改为:

 org.springframework.boot.web.support.SpringBootServletInitializer 

因此,如果您转到SampleJettyJspApplication并将导入更改为

 import org.springframework.boot.context.web.SpringBootServletInitializer; 

一切都应该没问题。

或者,您可以更改Gradle构建脚本以导入1.4.0.BUILD-SNAPSHOT,但这需要添加Spring的快照存储库:

 buildscript { repositories { maven.url "http://repo.spring.io/snapshot" mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.BUILD-SNAPSHOT") } } 

您正在使用org.springframework.boot.context.web.SpringBootServletInitializer这是不推荐使用的。 代替:

使用

org.springframework.boot.web.support.SpringBootServletInitializer

对于SpringBoot 2.0

org.springframework.boot.web.servlet.support.SpringBootServletInitializer

您应该使用与maven相同的Spring引导版本1.4.0.BUILD-SNAPSHOT。 自1.4.0以来引入了org.springframework.boot.web.support.SpringBootServletInitializer ,这就是为什么gradle找不到它。