Spring Boot微服務(wù)WAR包模式部署

修改 pom.xml 文件

打包方式改為war

<packaging>war</packaging>

添加對(duì) servlet-api 的 Maven 依賴(如果沒有的話)

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>

添加 resource 插件的配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>default-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>target/classes</outputDirectory>
                <!--<useDefaultDelimiters>false</useDefaultDelimiters>-->
                <!--<delimiters>-->
                    <!--<delimiter>#</delimiter>-->
                <!--</delimiters>-->
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <excludes>
                            <exclude>application*.yml</exclude>
                        </excludes>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                        <!-- 是否替換#xx#表示的maven properties屬性值 -->
                        <filtering>true</filtering>
                        <includes>
                            <include>application.yml</include>
                            <include>application-${spring.profiles.active}.yml</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

添加多 profile 的配置

<profile>
    <id>dev</id>
    <properties>
        <spring.profiles.active>dev</spring.profiles.active>
    </properties>
</profile>
<profile>
    <id>test</id>
    <properties>
        <spring.profiles.active>test</spring.profiles.active>
    </properties>
</profile>
<profile>
    <id>demo</id>
    <properties>
        <spring.profiles.active>demo</spring.profiles.active>
    </properties>
</profile>

修改 application.yml 或 application.properties 文件

spring:
    profiles:
        active: '@spring.profiles.active@'

spring.profiles.active=@spring.profiles.active@

添加類文件

在 Application 啟動(dòng)類的同級(jí)添加一個(gè) ApplicationWebXml 類,其代碼如下(注意修改類頂部的 package 以及代碼行中啟動(dòng)類的類名):

package com.yonyou.occ.cmpt;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

/**
 * This is a helper Java class that provides an alternative to creating a web.xml.
 * This will be invoked only when the application is deployed to a Servlet container like Tomcat, JBoss etc.
 */
public class ApplicationWebXml extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

打包部署

打包

在命令行里運(yùn)行 mvn clean package -D maven.test.skip=true -P prod (-P 之后的參數(shù)為要打包的 profile),等待打包完成。

部署

  • 停止 Tomcat 服務(wù)。
  • 將生成的 WAR 包改名為 ROOT.war,拷貝到 Tomcat(建議使用8.5.x版本)的 webapps 文件夾下。
  • 拷貝之前,先刪除原有的 ROOT.war 和 ROOT 文件夾(如果有的話)。
  • 最后,啟動(dòng) Tomcat 服務(wù)。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容