To deploy a Springboot project as a war project

Source: Internet
Author: User
Tags tomcat server

Springboot with a built-in Tomcat server, you can package the project directly into a jar, but what if you need to make the project a war package. Here are some steps to deploying a Springboot project as a war project

1, first in Pom.xml package form jar modified to war

  <packaging>war</packaging>

2. To remove the Springboot built-in Tomcat dependencies, it should be noted that when using a different template engine, the dependencies that need to be removed are different, followed by the native dependencies and the Thymeleaf template engine's removal of dependencies (later packaged for discovery). Can not need to remove Tomcat dependencies directly add in dependencies)

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

It's okay.

Native Tomcat dependency Removal
  <dependency>
       <groupId>org.springframework.boot</groupId>
      <artifactId> spring-boot-starter-web</artifactid>
         <exclusions>
             <exclusion>
                   <groupId> Org.springframework.boot</groupid>
                   <artifactId>spring-boot-starter-tomcat</artifactId>
           </exclusion>
      </exclusions> 
      </dependency>
       <dependency>
     < Groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-tomcat</ artifactid>
      <scope>provided</scope>
  </dependency>
Thymeleaf Template engine Dependencies removal
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId> spring-boot-starter-thymeleaf</artifactid>
            <exclusions>
                <exclusion>
                    <groupid >org.springframework.boot</groupId>
                    <artifactid>spring-boot-starter-tomcat</artifactid >
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            < Groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-tomcat</ artifactid>
            <scope>provided</scope>
        </dependency>

3, after the Tomcat is removed, in order to ensure that the compilation is correct, you also need to add to the Servlet-api dependency (the start of the use of the need to rely on SERVLET-API, but found in the later use, you can not add the dependency)

    <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId> tomcat-servlet-api</artifactid>
            <version>7.0.42</version>
            <scope>provided</ Scope>
        </dependency>

4, set, package after the project access name, in the build node to set the filename (here is a special attention to the place, because the beginning is the use of a single module program, so you can name the project, but in the multi-module packaging found, if you add the project name this node.) An error occurs when a packaged program runs in Tomcat, and there is a problem with the directory format of the war package.

</plugins>
        <finalName> project name </finalName>
    </build>

5. Since we need to deploy the war package using an external Tomcat server, we cannot use the Application.java this main method of creating the project, and we need to rewrite a method similar to web.xml file configuration to start the Spring application context. At this point we need to declare a class that functions like a listener in web.xml that is responsible for initializing the Spring application context.

/**
 * Created by Sean on 2017/9/5.
 * * Public
class Servletinitializer extends Springbootservletinitializer {
    @Override
    protected Springapplicationbuilder Configure (Springapplicationbuilder application) {return
        application.sources ( Application name. Class);
    

This allows the project to be packaged into a war package and placed outside Tomcat to use.
However, it is important to note that all request paths in the project must be accompanied by the project name written in step 4 above. Otherwise, you will not be able to access the path.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.