11. 将博客部署到tomcat上

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

 <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>

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插件:

 <build>
<plugins>
<!-- <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>blog</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>

pom.xml

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

 package com.lvniao.blog;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@SpringBootApplication
public class App extends SpringBootServletInitializer {
@RequestMapping("/")
public ModelAndView index(Model model) {
model.addAttribute("hello", "Hello, World!");
return new ModelAndView("redirect:/home/");
} @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(App.class);
} public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}

App.java

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

上一篇:动态创建ImageView


下一篇:【MySQL】探究之常用SQL