How to make a springboot easily into a war package

Source: Internet
Author: User
Tags tomcat server

Why did you make Springboot a war bag?

Under normal circumstances, the Springboot item is in the form of a jar package, via the command line:

java -jar demo.jar

To run, and Springboot is an embedded tomcat server, so every reboot is a new tomcat server. For this reason, there is also a problem:
Files uploaded to the project, if saved in the project, will be lost after the reboot. For example, when we uploaded an Avatar and restarted the project, the Avatar was gone. If you save the file on a local disk, there is no way to get the tags in the HTML. Therefore, we need to make the Springboot project into a war package and put it into Tomcat to run.

Modify method

Add the following dependencies in the Pom.xml file:

<!--因配置外部TOMCAT 而配置--><dependency>            <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

Also, change the jar of the Pom.xml file header to war

    <groupId>com.star</groupId>    <artifactId>yiyong</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <!--<packaging>jar</packaging>-->
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

Finally, start the class from the

@SpringBootApplicationpublic class YiyongApplication {    public static void main(String[] args) { SpringApplication.run(YiyongApplication.class, args); }}

Revision changed to

 @SpringBootAppli Cationpublic class yiyongapplication extends  springbootservletinitializer{ @Override protected Springapplicationbuilder configure (Springapplicationbuilder builder) {return builder.sources (Yiyongapplication.class); } public static void main (string[] args) {Springapplication.run (yiyongapplication.class, args);}}     

Note: This class inherits the Springbootservletinitializer and overrides the Configure method, which is the key.

Packaged deployment

On the right of idea maven bar double-click the package to wait for build success.

Then put the war package in the target directory under Tomcat's WebApps directory and launch Tomcat to automatically unzip the deployment.
Finally, enter in the browser

http://localhost:[端口号]/[打包项目名]/

Publishing success

How to make a springboot easily into a war package (go)

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.