繁体   English   中英

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

[英]Is web.xml required to deploy a spring boot application

我试图将弹簧启动应用程序打包为战争。 根据这个 ,我修改了应用程序类:

@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 boot应用程序时,我从未见过有关创建web.xml的任何内容。 将Spring启动应用程序部署为战争需要web.xml吗?

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

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

你有网络依赖项吗?

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

如果你需要一些配置,你可以随时拥有你的web.xml,只需将文件放在WEB-INF中的正确文件夹中,这样春天就可以读取配置。 还要将包装更改为

<packaging>war</packaging>

考虑使用spring-boot的父pom作为

   <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提供程序。

您实际上并不需要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

在servlet 2.5规范(java EE 5)中,web.xml是必需的,在servlet规范3+(java EE 6)中,你可以删除web.xml并使用注释配置代替

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM