web.xml 是否需要部署 spring 引导应用程序

Is web.xml required to deploy a spring boot application

我试图将 spring 启动应用程序打包为 war。根据this,我修改了我的申请class:

@SpringBootApplication
@EntityScan({"org.mdacc.rists.cghub.model"}) 
@EnableJpaRepositories(basePackages = {"org.mdacc.rists.cghub.ws.repository"})
public class Application extends SpringBootServletInitializer
{

    public static void main( String[] args )
    {
        SpringApplication.run(Application.class, args);
    }

    @Override
     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
         return application.sources(Application.class);
     }
}

还在我的pom.xml

中添加了以下内容
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

当我打包项目时,出现以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project cg-web: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

当我阅读 spring 启动应用程序时,我从未看到任何关于创建 web.xml 的内容。将 spring 启动应用程序部署为 war 是否需要 web.xml?

您实际上不需要 web.xml 文件来创建 WAR 准备好部署的工件。 以下是我如何使用 Gradle.

构建基于 Spring Boot 的工件

build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE"
    }
}

apply plugin: 'war'
apply plugin: 'spring-boot'

repositories {
    mavenCentral()
}

dependencies {
    compile "org.springframework.boot:spring-boot-starter-web:1.3.3.RELEASE"

    //Allows to run spring boot app on standalone Tomcat instance
    providedRuntime "org.springframework.boot:spring-boot-starter-tomcat:1.3.3.RELEASE"
}

为了构建 WAR,应该 运行:

gradle war

你有网络依赖吗

  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

如果您需要一些配置,您可以随时使用 web.xml,只需将文件放在 WEB-INF 中的正确文件夹中,这样 spring 就可以读取配置。也把包装改成

<packaging>war</packaging>

考虑将父 pom 用于 spring-boot 作为

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
    </parent>

这个配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

只告诉 maven 不要在 war 文件中包含 tomcat 依赖项,以避免干扰 servlet 提供程序。

根据 的回答,通过将以下代码段添加到您的 Maven 忽略 web.xml 缺席pom.xml:

<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <failOnMissingWebXml>false</failOnMissingWebXml>
  </configuration>
</plugin>

在 servlet 2.5 规范 (java EE 5) 中,web.xml 是强制性的,在 servlet 规范 3+ (java EE 6) 中,您可以删除 web.xml并改用注解配置