Spring Boot微服务WAR包模式部署

修改 pom.xml 文件

打包方式改为war

war

添加对 servlet-api 的 Maven 依赖(如果没有的话)


    javax.servlet
    javax.servlet-api
    provided

添加 resource 插件的配置


    org.apache.maven.plugins
    maven-resources-plugin
    
        
            default-resources
            validate
            
                copy-resources
            
            
                target/classes
                
                
                    
                
                
                    
                        src/main/resources
                        
                            application*.yml
                        
                    
                    
                        src/main/resources
                        
                        true
                        
                            application.yml
                            application-${spring.profiles.active}.yml
                        
                    
                
            
        
    

添加多 profile 的配置


    dev
    
        dev
    


    test
    
        test
    


    demo
    
        demo
    

修改 application.yml 或 application.properties 文件

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

[email protected]@

添加类文件

在 Application 启动类的同级添加一个 ApplicationWebXml 类,其代码如下(注意修改类顶部的 package 以及代码行中启动类的类名):

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

打包部署

打包

在命令行里运行 mvn clean package -D maven.test.skip=true -P prod (-P 之后的参数为要打包的 profile),等待打包完成。

部署

  • 停止 Tomcat 服务。
  • 将生成的 WAR 包改名为 ROOT.war,拷贝到 Tomcat(建议使用8.5.x版本)的 webapps 文件夹下。
  • 拷贝之前,先删除原有的 ROOT.war 和 ROOT 文件夹(如果有的话)。
  • 最后,启动 Tomcat 服务。

你可能感兴趣的:(Spring Boot微服务WAR包模式部署)