Is web.xml required to deploy a spring boot application

34,501

Solution 1

According to this answer make Maven ignore the web.xml absence by adding the following snippet to your pom.xml:

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

Solution 2

Do you have the web dependencies.

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

You can always have your web.xml if you need some configuration on it, just place the file in the correct folder within WEB-INF so spring can take it an read the configurations. Also change the packaging to

<packaging>war</packaging>

Consider as well use the parent pom for spring-boot as

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

This configuration

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

Only tell maven not to include tomcat dependencies in the war file, to avoid interference with the servlet provider.

Solution 3

In servlet 2.5 specs (java EE 5) , web.xml is mandatory, in servlet specs 3+ (java EE 6) , you can remove web.xml and use annotation configuration instead

Solution 4

You don't actually need web.xml file to create WAR artifact ready to be deployed. Here is how I build Spring Boot-based artifacts using Gradle.

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"
}

In order to build WAR, one should run:

gradle war

Share:
34,501
Nasreddin
Author by

Nasreddin

Updated on July 09, 2022

Comments

  • Nasreddin
    Nasreddin almost 2 years

    I was trying to package a spring boot application as a war. According to this, I modified my Application 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);
         }
    }
    

    Also added the following in my pom.xml

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

    When I package the project though, I got the following error:

    [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]
    

    As I was reading upon spring boot application, I never saw anything about creating a web.xml. Is web.xml required in deploying spring boot application as war?

  • Nasreddin
    Nasreddin over 8 years
    I am not familiar with gradle. How do I package it with maven?
  • Yogen Rai
    Yogen Rai over 7 years
    @Nasreddin with maven, use mvn package