Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is web.xml required to deploy a spring boot application

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?

like image 521
Nasreddin Avatar asked Mar 30 '16 17:03

Nasreddin


People also ask

Is web xml necessary?

XML Still Needed Even, with all the features introduced in Servlet 3.0, there are some use cases where we'll still need a web. xml file, among them: We can't define the filter order with annotations – we still need the <filter-mapping> section if we have multiple filters that we need to be applied in a particular order.

What required for a spring boot application?

System Requirements Spring Boot 2.7. 5 requires Java 8 and is compatible up to and including Java 19. Spring Framework 5.3. 23 or above is also required.

Do we need web server for spring boot application?

One of the most popular uses is as a web server, using one of the many supported embedded servlet containers and template engines. However, Spring Boot has a number of uses that do not require a web server: console applications, job scheduling, batch or stream processing, serverless applications, and more.


2 Answers

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>
like image 184
Aliaxander Avatar answered Oct 12 '22 02:10

Aliaxander


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.

like image 45
Koitoer Avatar answered Oct 12 '22 02:10

Koitoer