zoukankan      html  css  js  c++  java
  • Spring Boot 简单的打包部署

      Spring Boot 支持使用 Jar 内嵌 Web 服务器(Tomcat)的方式发布,也支持生成 war 包放在外置的 web 服务器运行。

      1、使用 Jar 发布应用

      配置步骤:

      ① pom.xml 要显示加入插件 org.springframework.boot,否则无法产生 jar 清单文件,导致打出来的 jar 无法使用命令运行。

     1 <build>
     2         <plugins>
     3             <plugin>
     4                 <groupId>org.springframework.boot</groupId>
     5                 <artifactId>spring-boot-maven-plugin</artifactId>
     6                 <!-- 排除重新打包 -->
     7                 <executions>
     8                     <execution>
     9                         <goals>
    10                             <goal>repackage</goal>
    11                         </goals>
    12                     </execution>
    13                 </executions>
    14                 
    15             </plugin>
    16         </plugins>
    17     </build>
    18     

      ② 使用里面 package 打包

      ③ 打包成功,产生 spring-boot-demo-01.-1.0.jar 文件

      ④ 将 spring-boot-demo-01-1.0.jar 复制到一个文件夹下,在同一级目录编写一个 bat 文件

      内容格式:#java  -jar  <jar 名>,如下:

    java -jar spring-boot-demo-01-1.0.jar

      ⑤ 双击 bat 文件 startup.bat

      可以修改内嵌 Tomcat 的参数

      在 application.properties 设置相关参数即可,如:

    #设置Tomcat端口
    server.port=80
    #设置Tomcat路径编码
    server.tomcat.uri-encoding=UTF-8
    #设置超时时间
    server.connection-timeout=1000

    2、使用 war 发布应用

      配置流程:

      ① 修改 pom.xml 文件去掉嵌入式的 Tomcat 服务器,以及增加 serlvet-api 依赖包

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- 移除嵌入式tomcat服务器 -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <!-- 加入servlet-api的支持 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

      ② 修改 pom.xml 文件的打包方式为 war

      ③ 增加一个 web 程序的 WEB 入口类要和 Application 同一级目录

     1 package cn.mgy;
     2 
     3 import org.springframework.boot.builder.SpringApplicationBuilder;
     4 import org.springframework.boot.web.support.SpringBootServletInitializer;
     5 /**
     6  * 如果项目需要使用war发布,需要创建这个类,作为web程序的入口
     7  * @author ranger
     8  *
     9  */
    10 public class ServletInitializer extends SpringBootServletInitializer {
    11 
    12     @Override
    13     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    14         //表示获得web的请求时,调用Application类的实现
    15         return builder.sources(Application.class);
    16     }
    17 }

      ④ 打包生成 war 文件

      ⑤ 复制 spring-boot-demo-01-1.0.war 放在 Tomcat 的 webapps 下

      ⑥ 启动 Tomcat,Tomcat 控制台提示 SpringBoot 信息

      ⑦ 浏览器访问,成功

    http://localhost:8080/spring-boot-demo-01-1.0/index

      注意事项:

      ① web 网站入口类一定要继承 SpringBootServletInitializer 类,而且必须要给 SpringBoot 入口类 Application 统一级目录

      ② 

      1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      3     <modelVersion>4.0.0</modelVersion>
      4     <groupId>cn.gzsxt.platform</groupId>
      5     <artifactId>basic_platform_spring_boot</artifactId>
      6     <version>1.0</version>
      7     <packaging>war</packaging>
      8 
      9     <parent>
     10         <groupId>org.springframework.boot</groupId>
     11         <artifactId>spring-boot-starter-parent</artifactId>
     12         <version>1.5.13.RELEASE</version>
     13         <relativePath /> <!-- lookup parent from repository -->
     14     </parent>
     15 
     16 
     17 
     18     <!-- 设置自定义属性 -->
     19     <properties>
     20        
     21         <project.build.sourceEncoding>GBK</project.build.sourceEncoding>
     22         <project.reporting.outputEncoding>GBK</project.reporting.outputEncoding>
     23         
     24         <java.version>1.8</java.version>
     25         <servlet.version>3.0.1</servlet.version>
     26         <jsp.version>2.2.1</jsp.version>
     27     </properties>
     28 
     29 
     30     <!-- 依赖 -->
     31     <dependencies>
     32         <dependency>
     33             <groupId>org.springframework.boot</groupId>
     34             <artifactId>spring-boot-starter-tomcat</artifactId>
     35             <scope>provided</scope>
     36         </dependency>
     37         <dependency>
     38             <groupId>org.springframework.boot</groupId>
     39             <artifactId>spring-boot-starter-thymeleaf</artifactId>
     40             <!-- 排除掉内置的tomcat使用我们外置tomcat -->
     41             <exclusions>
     42                 <exclusion>
     43                     <groupId>org.springframework.boot</groupId>
     44                     <artifactId>spring-boot-starter-tomcat</artifactId>
     45                 </exclusion>
     46             </exclusions>
     47         </dependency>
     48 
     49         <dependency>
     50             <groupId>org.springframework.boot</groupId>
     51             <artifactId>spring-boot-starter-web</artifactId>
     52             <!-- 移除嵌入式tomcat服务器 -->
     53             <exclusions>
     54                 <exclusion>
     55                     <groupId>org.springframework.boot</groupId>
     56                     <artifactId>spring-boot-starter-tomcat</artifactId>
     57                 </exclusion>
     58             </exclusions>
     59         </dependency>
     60         <dependency>
     61             <groupId>org.springframework.boot</groupId>
     62             <artifactId>spring-boot-starter-jdbc</artifactId>
     63         </dependency>
     64 
     65         <dependency>
     66             <groupId>org.mybatis.spring.boot</groupId>
     67             <artifactId>mybatis-spring-boot-starter</artifactId>
     68             <version>1.3.2</version>
     69         </dependency>
     70 
     71         <dependency>
     72             <groupId>mysql</groupId>
     73             <artifactId>mysql-connector-java</artifactId>
     74             <scope>runtime</scope>
     75         </dependency>
     76         <dependency>
     77             <groupId>org.springframework.boot</groupId>
     78             <artifactId>spring-boot-starter-test</artifactId>
     79             <scope>test</scope>
     80         </dependency>
     81         <!-- servlet -->
     82         <dependency>
     83             <groupId>javax.servlet</groupId>
     84             <artifactId>javax.servlet-api</artifactId>
     85         </dependency>
     86         <!-- jsp -->
     87         <dependency>
     88             <groupId>javax.servlet.jsp</groupId>
     89             <artifactId>javax.servlet.jsp-api</artifactId>
     90             <version>2.2.1</version>
     91         </dependency>
     92 
     93         <dependency>
     94             <groupId>org.apache.commons</groupId>
     95             <artifactId>commons-dbcp2</artifactId>
     96         </dependency>
     97 
     98         <!-- jstl -->
     99 
    100 
    101         <dependency>
    102             <groupId>javax.servlet</groupId>
    103             <artifactId>jstl</artifactId>
    104 
    105         </dependency>
    106 
    107 
    108         <dependency>
    109             <groupId>log4j</groupId>
    110             <artifactId>log4j</artifactId>
    111             <version>1.2.17</version>
    112             
    113         </dependency>
    114         
    115     </dependencies>
    116 
    117     <!-- 构建项目使用Tomcat7插件 -->
    118     <build>
    119 
    120 
    121         <plugins>
    122 
    123 
    124             <!-- 配置isntall安装时,不要执行JUnit测试代码 -->
    125 
    126             <plugin>
    127                 <groupId>org.apache.maven.plugins</groupId>
    128                 <artifactId>maven-surefire-plugin</artifactId>
    129 
    130                 <configuration>
    131                     <!-- 跳过打包安装时自动执行Junit的测试代码 -->
    132                     <skipTests>true</skipTests>
    133                 </configuration>
    134             </plugin>
    135             <!-- 配置编译的时候使用1.8 的JDK -->
    136 
    137             <plugin>
    138                 <groupId>org.apache.maven.plugins</groupId>
    139                 <artifactId>maven-compiler-plugin</artifactId>
    140 
    141                 <configuration>
    142                     <!-- 源码检查时使用1.8的JDK -->
    143                     <source>1.8</source>
    144                     <!-- 源码打包成目标程序时,使用1.8JDK -->
    145                     <target>1.8</target>
    146                 </configuration>
    147             </plugin>
    148 
    149 
    150             <plugin>
    151                 <groupId>org.apache.maven.plugins</groupId>
    152                 <artifactId>maven-war-plugin</artifactId>
    153 
    154                 <configuration>
    155                     <failOnMissingWebXml>false</failOnMissingWebXml>
    156                 </configuration>
    157             </plugin>
    158         </plugins>
    159     </build>
    160 
    161 </project>

      ③ 将映射接口使用@Mapper

    @Mapper
    public interface ModularMapper 

      ④ 关闭 thymeleaf 模板引擎

      注意:关闭 thymeleaf 模板引擎

    #忽略默认的模板引擎
    spring.thymeleaf.cache=false
    spring.thymeleaf.enabled=false

      ⑤ 启动的是 WebApplicationInitializer 类

      所以直接启动页面就可以

     1 package cn.mgy.platform;
     2 
     3 import org.springframework.boot.builder.SpringApplicationBuilder;
     4 import org.springframework.boot.web.support.SpringBootServletInitializer;
     5 
     6 public class WebApplicationInitializer extends  SpringBootServletInitializer  {
     7 
     8     @Override
     9     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    10     
    11        return builder.sources(Application.class);
    12     }
    13 
    14 }
  • 相关阅读:
    将地址转换为链接的正则表达式(regex url href)
    [收藏]中国惠普前总裁孙振耀谈人生
    沪江技术团队寻觅新成员,下一位会是你吗?
    来点圣诞气氛
    让VisualSVN Server支持匿名访问
    11月25日博客园南京园友聚会
    [上海俱乐部活动]博文视点与博客园系列图书作者见面会暨.NET技术交流会
    比尔·盖茨在微软的最后一天
    真让人兴奋
    博客园新服务器已下订单
  • 原文地址:https://www.cnblogs.com/maigy/p/10884961.html
Copyright © 2011-2022 走看看