Springboot同时出Jar和War

修改maven配置
为同时支持war / jar部署,主程序的pom.xml需作如下修改,该配置默认以jar方式打包;以war方式打包须使用命令 mvn package -Pwar

请移除原有maven-jar-plugin,并保证在项目里有默认application.properties文件(配置可使用本地配置)

${packaging.type} 
...

        
            jar
            
                true
            
            
                jar
            
            
                
                    org.springframework.boot
                    spring-boot-starter-web
                    
                        
                            org.springframework.boot
                            spring-boot-starter-tomcat
                        
                        
                            org.springframework.boot
                            spring-boot-starter-logging
                        
                    
                
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                    compile
                
            
            
                项目名
                
                    
                        org.apache.maven.plugins
                        maven-compiler-plugin
                        3.3
                        
                            1.8
                            1.8
                        
                    
                    
                        org.springframework.boot
                        spring-boot-maven-plugin
                        
                            ${project.name}
                            入口类
                            ZIP
                        
                        
                            
                                
                                    repackage
                                
                            
                        
                    
                    
                    org.apache.maven.plugins
                        maven-jar-plugin
                        2.4
                        
                        
                            **/application.yml
                        
                        
                    
                
            
        
        
            war
            
                war
            
            
                
                    org.springframework.boot
                    spring-boot-starter-web
                    
                        
                            org.springframework.boot
                            spring-boot-starter-tomcat
                        
                        
                            org.springframework.boot
                            spring-boot-starter-logging
                        
                    
                
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                    provided
                
            
            
                项目名
                
                    
                        org.apache.maven.plugins
                        maven-compiler-plugin
                        3.3
                        
                            1.8
                            1.8
                        
                    
                    
                        maven-war-plugin
                        
                            false
                        
                    
                
            
        
    
 

修改启动类
启动类须继承SpringBootServletInitializer

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

你可能感兴趣的:(Springboot同时出Jar和War)