1. 程式人生 > >springboot--部署springboot專案到tomcat容器

springboot--部署springboot專案到tomcat容器

開發好後的springboot通常會部署在外部容器,如tomcat。記錄下如何將springboot打包釋出到tomcat容器下。

1.先在pom.xml檔案中新增對tomcat依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

2.pom.xml修改打包方式

<groupId>com.example</groupId>
<artifactId>demo_configuration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

3.修改springboot的啟動類

@SpringBootApplication
public class DemoConfigurationApplication extends SpringBootServletInitializer implements WebApplicationInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application){

        return application.sources(DemoConfigurationApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoConfigurationApplication.class, args);
    }


}

4.build->build project重新生成專案

5.點選右側maven projects開啟maven面板,lifecycle->package,雙擊package打包專案,控制檯會輸出打包資訊,打包完成後提示build success,同時專案的target目錄下會出現 projectName.war名稱的war包,這個就是打包好的war包。

6.將打包好的war包放到tomcat的webapps目錄下面。

7.啟動tomcat就可以直接訪問專案,注意訪問時需要帶上專案名。