What is the difference between SpringBoot deployment packaged into jar and war?

Click to follow the official account, Java dry goodsDelivery in time

8ae8a88b6ca19886a118d77e0b3331ed.png

First, let me tell you about a strange problem we encountered:

  1. One of my springboot projects, packaged into a jar with mvn install, can be run directly with java -jar project name.jar on another machine with jdk, no problem, why can it run without tomcat here?

  2. Then I packaged it into war and put it into tomcat to run, and found that the port number changed to tomcat’s default 8080 (I set port 8090 in server.port). The project name must also be added.

That is to say, I am running in the IDEA of the original machine, the project interface address is ip:8090/listall, and the tomcat packaged into another machine becomes ip:8080/project name/listall. This is why?

  • Running through jar actually starts the built-in tomcat, so the port in the application configuration file is used

  • After directly deploying to tomcat, the built-in tomcat will not be enabled, so the relevant configuration is based on the installed tomcat, and has nothing to do with the application configuration file

Hey, basically no one who learns programming can teach history anymore, and no one is interested in delving into it.

Generally speaking, many years ago, when Sun was still alive, after passing through the wild period of writing Html parsers in C ++ in the early days, some of the earliest script programs entered the cgi era. At this time, Sun decided to Entering this field, in order to show the difference and look tall, so the servlet standard was developed and the earliest jsp was produced. And gave himself a tall title JavaEE (Java enterprise-level application standard, isn’t it just a bunch of servers that provide services with http, bragging).

Since it is an enterprise-level standard, it naturally has its own server standard. So the Servlet standard was born. The server implemented by this standard is called a Servle container server. Tomcat is one of the representatives. It was donated to the Apache Foundation by Sun. At that time, the Web server was still a lofty concept. It is the War package (actually a Zip package), and this is the origin of the War package.

Later, with the repeated evolution of the server field, people found out why we need such a cumbersome web server, but also implement a lot of management functions other than Servlet. Wouldn’t it be better to simplify and extract the core concept servlet? It seems that the first to do this is Jetty, a Servlet server that can be embedded appears.

A lot of non-core functions have been removed. Later, tomcat also followed up, and later, Jboss, a traditional JavaEE server that was originally very bulky, also set up an undertow to join in the fun. At this time, the concept of microservices emerged, “use Jar, not War”. Calls to phase out traditional Servlet servers arose.

The difference between jar package and war package

1. War is a web module, which needs to include WEB-INF, which is a WEB module that can be run directly; jar generally only includes some class files, which can be run with the java command after the Main_class is declared.

2. A war package is a web application, usually a website, packaged and deployed in a container; a jar package usually refers to a common class during development, and is packaged for easy storage and management.

3. war is a web application format proposed by Sun, and it is also a compressed package of many files. The files in this package are organized according to a certain directory structure; the classes directory contains compiled Servlet classes and other classes (such as JavaBean) that Jsp or Servlet depends on can be packaged into jars and placed in the lib directory under WEB-INF.

The JAR file format is based on the popular ZIP file format. Unlike ZIP files, JAR files are used not only for compression and distribution, but also for deployment and packaging of libraries, components, and plug-ins, and can be used directly by tools like compilers and the JVM.

[Format Features]:
  • Security The JAR file contents can be digitally signed. That way, a tool that recognizes the signature can selectively grant you software security privileges that other files cannot, and it can detect if the code has been tampered with.

  • Reduce download time If an applet is bundled into a JAR file, the browser can download the applet’s class files and associated resources in one HTTP transaction, rather than opening a new JAR file for each file. connect.

  • Compressed The JAR format allows you to compress files for storage efficiency.

  • Transport Platform Extensions The Java Extensions Framework (Java Extensions Framework) provides a means of adding functionality to the Java core platform, these extensions are packaged in JAR files (Java 3D and JavaMail are examples of extensions developed by Sun) .

A WAR file is a web application. To create a WAR file is to compress the entire web application (excluding the root directory of the web application hierarchy) and specify a war extension.

【Creation conditions】:
  • Need to establish the correct directory hierarchy for the web application.

  • Create a WEB-INF subdirectory, and create two subdirectories, classes and lib, under this directory.

  • Put the Servlet class file in the WEB-INF\classes directory, and put the Java class library file (that is, the JAR file) used by the Web application in the WEB-INF\lib code> directory.

  • Put JSP pages or static HTML pages under the context root path or its subdirectories.

  • Create a META-INF directory, and create a context.xml file in this directory.

Let me tell you how to package the springboot project into jar and war

It is very simple to package the SpringBoot project into jar, and it is also a common packaging format of SpringBoot; this blog records both ways of packaging SpringBoot into jar and war;

First introduce the way to package SpringBoot into a jar package: (the following example is demonstrated in idea)

1. Package into jar

1) First create a new Spring Starter Project

c54b5cd6d8ef900a4016d50094137a17.png

Note here that packaging defaults to jar and does not need to be modified.

2) After the creation is complete, the pom of the project is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.4.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example</groupId>
 <artifactId>demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>demo</name>
 <description>Demo project for Spring Boot</description>
 
 <properties>
  <java.version>1.8</java.version>
 </properties>
 
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
  </dependency>
 
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>
 
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
 
</project>

3) Make a jar package (via the maven command):

In the Terminal window, use the mvn clean package command to package:

3a0e947055248e28dd5719f84dcea8df.png

Then you can see the packaged jar package in the target directory

0503ded80b78112814d8e11797c56c04.png

Second, pack into war package format

1) You can make changes to the project you just created. First, you need a ServletInitializer class for packaging into war. The location of this class needs to be in the same file as the startup class.

a65d08de230c404122b44a5818eac0a8.png

If you choose the war package form at the beginning, this class will be created automatically

2) Modify pom.xml

Modify the war of pom.xml to change the original jar to war;

3) If our SpringBoot uses html as the front-end page development, there is no problem, but if we want to use jsp to develop, we need to configure some dependencies at this time: mainly to exclude SpringBoot’s built-in Tomcat, add javax.servlet-api and tomcat-servlet-api (SpringMVC also needs to configure the suffix);

The final pom.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.4.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example</groupId>
 <artifactId>demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <name>demo</name>
 <description>Demo project for Spring Boot</description>
 
 <properties>
  <java.version>1.8</java.version>
 </properties>
 
 <dependencies>
        <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>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
 
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-servlet-api</artifactId>
            <version>8.0.36</version>
            <scope>provided</scope>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
 
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>
 
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
 
</project>

Because SpringBoot recommends html by default instead of jsp; after the above modification, jsp can be used for development;

4) Package into war: use mvn clean package

as follows:

e6b9b3d40e093995a0cfe8f632c342d6.png

After the packaging is successful, you can put the war package under webapps under tomcat, and then run tomcat to start the project;

Record it and read it when you use it in the future ^_^;

Of course, when creating a project, directly select the package as war, and it can be directly packaged into a war package.

When war is selected as the packaging method to create a project, ServletInitializer is created directly by default

b1e6eaf90bada8faa51ad77cdd5f8745.png

At this point, the pom file is as follows

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.4.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example</groupId>
 <artifactId>demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <name>demo</name>
 <description>Demo project for Spring Boot</description>
 
 <properties>
  <java.version>1.8</java.version>
 </properties>
 
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
 
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>
 
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
 
</project>

Directly mvn clean package can be packaged successfully

398b8648963492262618f2ca198930b8.png

Thanks for reading, I hope it can help you 🙂

Source: https://blog.csdn.net/weixin_40910372/