springboot使用war部署(profile隔离版)

303 阅读1分钟

代码修改

修改pom文件

在profile添加war-build,同时把packaging设置为读取properties。

    <packaging>${packaging.method}</packaging>
    ······
    <profiles>
        <profile>
            <id>war-build</id>
            <properties>
                <packaging.method>war</packaging.method>
            </properties>
            <dependencies>
                <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>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>3.1.0</version>
                        <configuration>
                            <failOnMissingWebXml>false</failOnMissingWebXml>
                            <warName>${project.artifactId}</warName>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

添加ServletInitializer

public class MyInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MySpringApplication.class);
    }

}

打包发布

mvn打包时加上"-P war-build",示例如下:

mvn package -P war-build

war包打包完成后放到服务器对应目录即可

后记

springboot的war包部署只是为了兼容以前的servlet,生命周期的控制权主体在服务器,不建议使用这种方式部署。本文旨在提供一种尽量不影响开发的部署方式。