zl程序教程

您现在的位置是:首页 >  工具

当前栏目

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

TomcatIDEA打包部署 Intellij 外部 使用
2023-09-14 09:00:01 时间

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>