spring_boot 发布成war包 ,部署到外部的tomcat

由jar变成war

<packaging>jarpackaging>

如果是上面的打包方式,启动方式则为

 mvn package
 java -jar target/mymodule-0.0.1-SNAPSHOT.jar

改变成war


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <packaging>warpackaging>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-tomcatartifactId>
            <scope>providedscope>
        dependency>
        
    dependencies>
project>

只需要把tomcat的范围改成provided

如果要发布到外部的tomcat同时需要改变启动方式

新增ServletInitializer类

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

public class ServletInitializer extends SpringBootServletInitializer {  

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

}  

Application.class 为标注有@SpringBootApplication的主启动类

注意的问题

此时打成的包的名称应该和application.properties的
server.context-path=/spring-boot
保持一致

<build>
    <finalName>spring-bootfinalName>
build>

如果不一样发布到tomcat的webapps下上下文会变化

你可能感兴趣的:(java,web,spring)