• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

SpringBoot (八) --------- SpringBoot 打包

武飞扬头像
在森林中麋了鹿
帮助1


Spring Boot 可以打包为 war 或 jar 文件,以两种方式发布应用

一、SpringBoot 打包为 war

  • pom.xml

在 pom.xml 文件中配置内嵌 Tomcat 对 jsp 的解析包

<!--处理 jsp 的依赖--> 
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
  • 指定 jsp 文件编译目录
<resources>
   <resource>
       <directory>src/main/webapp</directory>
       <targetPath>META-INF/resources</targetPath>
       <includes>
           <include>**/*.*</include>
       </includes>
   </resource>
</resources>
  • 打包后的 war 文件名称
<!--指定打包后的 war 文件名称--> 
<finalName> myweb </finalName>
  • 完整的 build 标签内容
<build>
    <finalName> myweb </finalName>
    <resources>
        <!--mybatis 他的 xml 文件放置 src/main/java 目录-->
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>

        <!--指定 resources 下面的所有资源-->
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.*</include>
            </includes>
        </resource>
        
        <resource>
            <directory>src/main/webapp</directory>
            <targetPath>META-INF/resources</targetPath>
            <includes>
                <include>**/*.*</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.2.6.RELEASE</version>
        </plugin>
    </plugins>
</build>
学新通
  • 创建 webappp 目录

我们新建普通webapp目录,然后指定 webapp 是 web 的应用目录

学新通
学新通

  • 创建 jsp 文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>jsp</title>
</head>
<body>
    显示 controller 中的数据 ${data};
</body>
</html>
  • 创建 JspWarController
package com.example.customer.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class JspWarController {
     
    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("data", "SpringBoot web 应用打包为 war");
        mv.setViewName("index");
        return mv;
    }
}
学新通
  • 设置视图解析器

我们在 application.properties 中配置视图解析器

学新通

  • 启动主类,在浏览器访问地址 index

访问浏览器,地址 index

学新通

  • 主启动类继承 SpringBootServletInitializer

继承 SpringBootServletInitializer 可以使用外部 tomcat。
SpringBootServletInitializer 就是原有的 web.xml 文件的替代。
使用了嵌入式 servlet,默认是不支持 jsp。

package com.example.customer;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
@EnableDubbo
public class CustomerApplication extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(CustomerApplication.class);
    }
}
学新通
  • 指定项目 package 是 war

学新通

  • maven package 打包

学新通
学新通

  • 发布打包后的 war 到 tomcat

target 目录下的 war 文件拷贝到 tomcat 服务器 webapps 目录中,启动 tomcat
学新通
学新通
学新通

学新通

二、SpringBoot 打包为 jar

前面步骤几乎与打包为 war 相同,我们需要在 pom.xml 中更改打包类型即可

学新通

会出现以下错误,我们将 test 目录中代码注释掉即可
学新通

学新通

学新通

  • 执行 jar,启动内置的 tomcat

学新通

java –jar mybootjar.jar 启动 tomcat ,浏览器访问 web 应用

学新通

三、Spring Boot 部署和运行方式总结

➢ 在 IDEA 中直接运行 Spring Boot 程序的 main 方法(开发阶段)

➢ 用 maven 将 Spring Boot 安装为一个 jar 包,使用 Java 命令运行 java -jar springboot-xxx.jar

可以将该命令封装到一个 Linux 的一个 shell 脚本中(上线部署)
◼ 写一个 shell 脚本:

#!/bin/sh
java -jar xxx.jar

◼ 赋权限 chmod 777 run.sh

◼ 启动 shell 脚本: ./run.sh

➢ 使用 SpringBoot 的 maven 插件将 Springboot 程序打成 war 包,单独部署在 tomcat 中运行(上线部署常用)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgejfjf
系列文章
更多 icon
同类精品
更多 icon
继续加载