zoukankan      html  css  js  c++  java
  • 11. 将博客部署到tomcat上

    springboot项目既可以以jar运行,也可以做成war包放到服务器上,因为我的博客项目涉及到文件上传,所以按照jar的方式就不可行,需要部署到tomcat上,具体做法如下:
    1. 修改pom.xml
    1.1 将项目打包方式改为war:<packaging>war</packaging>
    1.2 在spring-boot-starter-web的依赖中去除tomcat的包修改如下:

     1 <dependency>  
     2     <groupId>org.springframework.boot</groupId>  
     3     <artifactId>spring-boot-starter-web</artifactId>  
     4     <exclusions>   
     5         <exclusion>
     6             <groupId>org.springframework.boot</groupId>
     7             <artifactId>spring-boot-starter-tomcat</artifactId>
     8         </exclusion>
     9     </exclusions> 
    10 </dependency>
    pom.xml

    1.3 添加对tomcat容器的依赖:

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

    1.4 移除spring-boot-maven-plugin插件,并增加maven-war-plugin插件:

     1 <build>  
     2     <plugins>  
     3         <!-- <plugin>  
     4             <groupId>org.springframework.boot</groupId>  
     5             <artifactId>spring-boot-maven-plugin</artifactId>  
     6         </plugin> -->  
     7         <plugin>
     8             <groupId>org.apache.maven.plugins</groupId>
     9             <artifactId>maven-compiler-plugin</artifactId>
    10             <configuration>
    11                 <source>1.8</source>
    12                 <target>1.8</target>
    13             </configuration>
    14         </plugin>
    15         <plugin>
    16             <groupId>org.apache.maven.plugins</groupId>
    17             <artifactId>maven-war-plugin</artifactId>
    18             <configuration>
    19                 <warName>blog</warName>
    20                 <failOnMissingWebXml>false</failOnMissingWebXml>
    21             </configuration>
    22         </plugin>
    23     </plugins> 
    24 </build>  
    pom.xml

    2. 修改App.java,使App继承自SpringBootServletInitializer,并重写SpringApplicationBuilder方法,代码如下:

     1 package com.lvniao.blog;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.boot.builder.SpringApplicationBuilder;
     6 import org.springframework.boot.context.web.SpringBootServletInitializer;
     7 import org.springframework.context.annotation.ComponentScan;
     8 import org.springframework.stereotype.Controller;
     9 import org.springframework.ui.Model;
    10 import org.springframework.web.bind.annotation.RequestMapping;
    11 import org.springframework.web.servlet.ModelAndView;
    12 
    13 @Controller
    14 @SpringBootApplication
    15 public class App extends SpringBootServletInitializer {
    16     @RequestMapping("/")
    17     public ModelAndView index(Model model) {
    18         model.addAttribute("hello", "Hello, World!");
    19         return new ModelAndView("redirect:/home/");
    20     }
    21     
    22      @Override
    23     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    24         return builder.sources(App.class);
    25     }
    26     
    27     public static void main( String[] args )
    28     {
    29            SpringApplication.run(App.class, args);
    30     }
    31 }
    App.java

    通过以上步骤后就可以通过maven来打包发布了。

  • 相关阅读:
    MSDN RSS Feeds (ZT)
    不錯,今天看到日历了.
    模糊:让你的代码远离偷窥之眼
    .NET中異常發布器的開發(1)(2)(3)
    How To Query Performance Monitor Counters Using a Web Page
    可選參數的Stored Procedure範例.
    Outlook GetCurrent Folder / GetSelectedItems / GetInspectors
    微軟的MS04007补丁有严重问题啊.
    Blog,流行有理由 (zt)
    从VB.Net到VB6.0要小心,关于使用IIF和log求对数函数(串联的小知识)
  • 原文地址:https://www.cnblogs.com/lvniao/p/9056397.html
Copyright © 2011-2022 走看看