旅鸟

导航

< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

统计

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来打包发布了。

posted on   旅鸟  阅读(145)  评论(0编辑  收藏  举报

努力加载评论中...
点击右上角即可分享
微信分享提示