使用intellij idea打包並部署到外部的tomcat


1.使用intellij idea創建項目demotest

  File -> New -> Project-> Spring Initializr,根據提示一步步操作

  會生成一個帶有 main() 方法的類 DemotestApplication,用於啟動應用程序

2.新建class,HelloController

package com.example.demotest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello Spring Boot!";
    }
}

3.運行

在DemotestApplication這個類中,然后右鍵點擊運行

瀏覽器中輸入http://localhost:8080/hello

4.打war包

  (1)Run-> Edit Configurations

  

  OK

  (2)修改pom.xml

    war包

<packaging>war</packaging>

    添加一個spring-boot-maven-plugin打包插件,打包后的名字為packageTest

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>packageTest</finalName>
    </build>

  運行mvn就可以生成jar包

  

  輸出下面提示,打包成功

  

  在項目的target目錄可以看到生成的war包

   

  打開cmd,到war 包所在目錄 運行命令

java -jar  packageTest.war

  瀏覽器輸入http://localhost:8080/hello

  輸出hello信息

4.發布到外部的tomcat

  把webtest.war放到tomcat下的webapp下,重啟tomcat,

  

  解決當前這個在外部tomcat沒辦法運行起來並訪問的問題,設置啟動配置

  新建一個SpringBootStartApplication 繼承自 SpringBootServletInitializer

    說明:

     A.在外部容器部署的話,不能依賴於Application的main函數

       B.在啟動類中繼承SpringBootServletInitializer並實現configure方法,以類似於web.xml文件配置的方式來啟動Spring應用上下文

       C.新建的類與springboot的啟動類是同級的

package com.example.demotest;

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

public class SpringBootStartApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder application) {
        //DemotestApplication類為springboot 項目的啟動類
        return application.sources(DemotestApplication.class);
    }
}

   

重新打包

  把webtest.war放到tomcat下的webapp下,重啟tomcat

  

  發布成功

 

補充:

  如果,設置啟動配置后還是不行,需要讓springboot內嵌的tomcat在運行時不起作用,修改pom.xml

  因為SpringBoot默認的容器為Tomcat,依賴包在spring-boot-starter-web下

  使用如下方式:

    <!--web支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    或者

    <!--web支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    <!--聲明spring boot內嵌tomcat的作用范圍  在運行時不起作用-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM