Steps to package the Spring boot project as a war and run on Tomcat

Source: Internet
Author: User

Category: Spring boot (16)

Directory (?) [+]

Original address: http://m.blog.csdn.NET/article/details?id=52515226

Publish the Spring-boot project to the Tomcat container in the same way as usual Web projects next , modify the packaging form

Set <packaging>war</packaging> II in Pom.xml , remove embedded tomcat plugin

Find the Spring-boot-starter-web dependency node in Pom.xml and add the following code to the

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId> Spring-boot-starter-web</artifactid>
    <!--remove embedded Tomcat plugin-->
    <exclusions>
        < exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId> spring-boot-starter-tomcat</artifactid>
        </exclusion>
    </exclusions>
</ Dependency>
iii. adding Servlet-api dependencies

All of the following two ways can be selected, either

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactid>javax.servlet-api </artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId> tomcat-servlet-api</artifactid>
    <version>8.0.36</version>
    <scope>provided</ Scope>
</dependency>
Iv. Modify the startup class and override the initialization method

The way we normally start with the main method, there is a startup class for the app, and the code is as follows:

@SpringBootApplication public
class Application {public
    static void Main (string[] args) {
        Springapplication.run (Application.class, args);
    }

We need a configuration like Web.xml to start the spring context, add a springbootstartapplication class at the sibling of the application class with the following code:

/**
 * Modifies the startup class, inherits Springbootservletinitializer, and overrides the Configure method/public
class Springbootstartapplication Extends Springbootservletinitializer {

    @Override
    protected Springapplicationbuilder Configure ( Springapplicationbuilder builder) {
        //note here to point to the application start class return builder.sources that was previously performed with the Main method
        ( Application.class);
    }

Five, packaged deployment

In the project root (that is, the directory containing the Pom.xml), enter at the command line:
MVN Clean Package can wait for the package to complete, the [INFO] Build success is the package success.
The war package under the target directory is then placed under Tomcat's WebApps directory to launch Tomcat to automatically decompress the deployment.
Finally, enter in the browser

http://localhost:[Port number/packing project name]/

Publish Successful

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.