SpringBoot War与Jar打包的 异同 !

1.Pom.xml

war : <packaging>war</packaging>

jar : <packaging>jar</packaging>

dependencies : war,多一条依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!– 移除嵌入式tomcat插件 –>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
</dependency>

2.项目启动

war :

需要继承SpringBootServletInitializer

public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(BootApplication.class);
    }
}

@SpringBootApplication
public class BootApplication {

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

Jar :

@SpringBootApplication
public class Boot1Application {

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

3.运行方式

俩者都支持 mvn spring-boot:run

war : 可以用tomcat运行

jar : 可以用java -jar *.jar 运行

 

voidm

在黑暗中支撑和平的无名者

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐