Springboot playing jar and war packages

Source: Internet
Author: User

One: Hit Jar bag

Add the following dependencies in the Pom.xml of the project

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

Our current project is using the packaging method of the jar package command, jar package.

we went into this directory to open this jar package with compression software , where we found a folder called lib , open Lib Folder Discovery This folder is full of project-dependent jar packages, and even Tomcat. This jar package contains a jar package , which we call the Fatjar ( fat jar Package )

since Fatjar itself includes Tomcat, We do not need to deploy it, and we can start our application directly at the command line, in the command line, into the jar The directory where the package resides, we can execute the jar package with the following Java–jar command . The startup information appears in the console and the browser accesses the program

II: Fight the War bag

Spring-boot provides inline tomcat by default , so packaging generates jar packages directly and can be started with the Java-jar command. However, sometimes we prefer a tomcat to manage multiple projects, in which case the project is a war -format package rather than a jar Format of the package.

We follow the steps below to complete the transformation of the project

(1) modify pom.xml

To change the packaging method to War

<packaging>war</packaging>

Add dependency

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

<scope>provided</scope>

</dependency>

Spring-boot-starter-tomcat is the dependency that was passed over, and by default it hits the packet, so we can introduce this dependency again and specify a dependency range of provided, so The Tomcat -related jar will not be packaged in the war .

(2) add servletinitializer

Import Org.springframework.boot.builder.SpringApplicationBuilder;

Import Org.springframework.boot.context.web.SpringBootServletInitializer;

public class Servletinitializer extends Springbootservletinitializer {

@Override

Protected Springapplicationbuilder Configure (springapplicationbuilderapplication) {

Return application.sources (Application.class);

}

}

since we are using the web3.0 specification, there is no Web. Xml , and this class has the same effect as Web. XML .

(3) run the Package Packaging command to generate a war packet

after the build , put the war package into Tomcat, start Tomcat, and test whether the completed feature is ready to use.

Springboot playing jar and war packages

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.