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

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

不言
不言forward
2018-11-27 17:18:313043browse

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){
//
//    }
}

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:

b86990c77ec45bfad4c402ad0f859e24
b4b38e33757a6497aa8690936b905cc1
    05a8acc5c31084a4f61ade01873802caorg.springframework.boot192ca2f7b8c770b01c8f81e6bdd5b947
    9bc4cbb67f66e148869423c0d27e5f90spring-boot-starter-tomcatb68fb17cb904a46b73e6272850323873
    06db57cb000bdd2564c5b32a302b10e2provided03b1008234ba0cf6ad3c873aea327e8a
09a0e22e5aaafd848ae04665be625b91

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>

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

1 <packaging>war</packaging>

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);
}

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>-->

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>

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

1 <packaging>jar</packaging>

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete