SpringBoot 打包(Jar 、war )

2019-08-16  本文已影响0人  MGary

War 打包方式

一 、 没有分模块的maven打包

1、找到主程序下的pox.xml文件并进行修改

<packaging>war</packaging>

2、新增Springboot spring-boot-starter-tomcat并将依赖设置为provided。

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

二、分模块maven打包

1、找到主程序下的pox.xml文件并进行修改

<packaging>war</packaging>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <warName>api</warName>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
</plugin>

2、 找到 Springboot spring-boot-starter-web模块排除Tomcat,并新增单独的Springboot Tomcat模块,设置在编译时使用。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>compile</scope>
</dependency>

以上两种方法都需要继承SpringBootServletInitializer,并且实现其中的方法,在其中指向你应用的主运行类。


@SpringBootApplication
public class ApplicationRunner extends SpringBootServletInitializer {
 
    public static void main(String[] args) {
        SpringApplication.run(ApplicationRunner.class, args);
    }
 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ApplicationRunner.class);
    }
 
}

JAR

一 、 没有分模块的maven打包

1、找到主程序下的pox.xml文件并进行修改

<packaging>jar</packaging>

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

二、分模块maven打包

1、找到主程序下的pox.xml文件并进行修改

<packaging>jar</packaging>

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>
上一篇 下一篇

猜你喜欢

热点阅读