【发布时间】:2014-06-22 10:48:39
【问题描述】:
我尝试遵循 Spring Getting Started Guide for "Serving Web Content with Spring MVC",它使用 Spring Boot 和 Gradle 以及 Maven。 我在 Eclipse 中安装了 Gradle 插件。
我想在 Eclipse 中使用 Tomcat 服务器运行应用程序,因为我还遵循“将 Spring Boot JAR 应用程序转换为 WAR”指南并更改了指南中提到的“build.gradle”文件。基本上,我添加了行“应用插件:'eclipse-wtp'”、“应用插件:'war'”、配置 {providedRuntime} 和提供的运行时间(“org.springframework.boot:spring-boot-starter-tomcat”);并将 jar 设置更改为战争设置。这是 build.gradle 文件:
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-milestone" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
eclipse.project {
natures 'org.springsource.ide.eclipse.gradle.core.nature'
}
war {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-milestone" }
}
configurations {
providedRuntime
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
testCompile("junit:junit")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
我还添加了他们提到的 HelloWebXml 类。这是课程:
package hello;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
public class HelloWebXml extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
除此之外,我还需要稍微更改 pom.xml,因为它抱怨 Java SE 7 的特性。在 pom.xml 中添加以下行:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<showWarnings>true</showWarnings>
<compilerVersion>1.7</compilerVersion>
<fork>true</fork>
</configuration>
</plugin>
其余与入门指南相同。
为了建设,我跑
gradlew eclipseWtp
gradlew clean build
命令。在此之后,war 文件将在项目的 build/libs 文件夹中创建。如果我将 war 文件复制到本地 Tomcat 服务器并启动服务器,一切都会按预期工作。
如果我将项目拖到“服务器”选项卡下的 Tomcat 服务器(使用相同的本地 Tomcat 创建)并运行该服务器,则会引发 ClassCastException 并抱怨: “无法将 org.springframework.web.SpringServletContainerInitializer 转换为 javax.servlet.ServletContainerInitializer”。
我在两个部署位置检查了项目的文件夹结构。 在本地(非 Eclipse)部署位置,服务器启动后,会按预期创建一个名为 war 文件的文件夹。在 WEB-INF 文件夹中,有一个 lib-provided 目录。我检查了 Eclipse 的 Tomcat 的部署位置,它没有包含名为 lib-provided 的目录。我猜问题是这个目录没有被创建,但我找不到解决方案。
我已经在使用 Spring MVC,并且我知道如何使用 web.xml 创建 MVC 项目,但我是 Spring Boot 的新手。 Eclipse 的 Tomcat 服务器可以很好地运行我之前的 Spring MVC 项目。所以问题出在 Spring Boot 项目上。
我检查了几个相关的问题,但它们与我的不同。我找不到解决问题的方法。
我在这里错过了什么?
提前致谢。
【问题讨论】:
-
为什么你同时拥有
pom.xml和build.gradle。您现在有 2 个构建系统竞争控制权。要么使用 maven 要么使用 gradle 不要同时使用。 -
好吧,正如我所提到的,这是来自 Spring 的指南。我认为他们应该比我更了解如何构建 Spring 项目。我也更喜欢只使用 Maven,但我不想通过偏离指南所说的内容来增加可能出错的事情的数量。
标签: eclipse tomcat spring-mvc gradle spring-boot