Home > Java > javaTutorial > Detailed introduction of war package and jar package in springboot (code example)

Detailed introduction of war package and jar package in springboot (code example)

不言
Release: 2018-11-27 17:18:31
forward
3157 people have browsed it

This article brings you a detailed introduction (code example) about the war package and jar package in springboot. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What I share with you in this article is to use maven to create war packages and jar packages in springboot; generally speaking, war can be directly placed under tomcat's webapps after being generated. Tomcat is configured to automatically decompress war, and jar is generally generated through Command line deployment and startup;

First of all, let’s actually see how to generate a war package, which can be divided into three steps:

  • Program entrance transformation

  • Exclude springboot built-in tomcat

  • Configuration program entry in the spring-boot-maven-plugin plugin

Program entry For transformation, we need to block the main entrance of springboot, then inherit SpringBootServletInitializer, and rewrite the configure method. The specific code is as follows:

@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }
//    public static void main(String[] args){
//
//    }
}
Copy after login

Exclude springboot's built-in tomcat, By default, springboot is integrated with the built-in tomcat, because the war needs to be sent to the tomcat on our server. The built-in tomcat is not needed. You can configure the shielding in maven as follows:

<!--war包-排除内置tomcat-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
Copy after login

Here, the tomcat package is excluded by setting the scope to provided. The tomcat package is included in the springboot framework. The default role of scope is compile, compile, test, and run;

spring-boot-maven -Configure the program entrance in the plug-in. We have blocked the main entrance above, but tomcat still uses the startup class as the entrance, so you need to configure the startup mainClass:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <!--war包-执行程序入口 -->
    <configuration>
        <mainClass>com.platform.WebApplication</mainClass>
    </configuration>
</plugin>
Copy after login

Finally, specify the packaging of the entry project as the war type

1 <packaging>war</packaging>
Copy after login

As above, our preparations are completed. Now we only need to package the maven package. After generation Screenshot below:

The contents of the war package are the META-INF and WEB-INF parts. Let’s upload the war to the webapps of tomcat on Linux. Generally, tomcat will be configured. Automatically decompress the war package. I wrote an api interface here before. After successful operation, it will be displayed in the browser normally:

Come again, we start to open the jar package, the steps are similar to those of war, but the reverse is to remove the comment content:

  • Restore the main entry (main generated by the springboot template, without any modification)

  • Remove the configuration to exclude tomcat (there is no such configuration by default)

  • Use the maven-jar-plugin plug-in for packaging, specify the program entry and various inclusions | exclusions Item

Restore the main entry without annotating it, mainly for the main entry operation that was annotated just for the war. Generally, the springboot template is used to generate it by default:

public static void main(String[] args) throws ParseException, Exception {
    SpringApplication springApplication = new SpringApplication(WebApplication.class);
    springApplication.setBannerMode(Banner.Mode.OFF);
    springApplication.run(args);
}
Copy after login

Remove the configuration to exclude tomcat (there is no such configuration by default). This step also adds the configuration to exclude tomcat for war. Just delete it directly. Delete:

<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
<!--<scope>provided</scope>-->
<!--</dependency>-->
Copy after login

Use the maven-jar-plugin plug-in for packaging, specify the program entry and various inclusions | exclusions. Here, configure some items through the jar plug-in, and specify the configuration file and main entry:

<!--打jar包-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <manifest>
                <!--<addClasspath>true</addClasspath>-->
                <!--<classpathPrefix>lib/</classpathPrefix>-->
                <!--main入口-->
                <mainClass>com.platform.WebApplication</mainClass>
            </manifest>
        </archive>
        <!--包含的配置文件-->
        <!--<includes>-->
        <!--<include>*.yml</include>-->
        <!--<include>*.properties</include>-->
        <!--<include>templates/**</include>-->
        <!--<include>static/**</include>-->
        <!--<include>*.xml</include>-->
        <!--</includes>-->
    </configuration>
</plugin>
Copy after login

Finally, specify the packaging of the entry project as the war type:

1 <packaging>jar</packaging>
Copy after login

After completing the above steps, you can see the successful jar package, as shown in the figure:

Finally, start it through java -jar web-0.0.01.SN...jar on Linux.

The above is the detailed content of Detailed introduction of war package and jar package in springboot (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template