Springboot发布war包部署
项目发布上线的时候,我们需要将项目打包成war包的形式,放到Tomcat的webapps下,然后运行Tomcat发布项目。那么在IDEA中SpringBoot项目怎么打包成war包的形式呢?
首先新建一个SpringBoot项目,配置打包方式为war
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jiuyue</groupId>
<artifactId>springboot-war</artifactId>
<version>1.0.0</version>
<!--打包方式-->
<packaging>war</packaging>
这是一个web项目,前端页面我们使用JSP,则需要的依赖有:
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入springBoot 内嵌的Tomcat对JSP的解析包-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!--jstl-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
在application.properties配置文件中配置前后缀
server.port=8088
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
控制器类,提供访问接口
@Controller
public class WarController {
@RequestMapping("war/json")
@ResponseBody
public Object json( ){
Map<String,Object> map = new HashMap<String,Object>();
map.put("name","九月json");
return map;
}
@RequestMapping("war/jsp")
public String home(Model model){
System.out.println("jsp");
model.addAttribute("name","九月jsp");
return "home";
}
}
需要Maven插件,以及需要配置webapp下面的资源文件信息
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
在启动类需要继承SpringBootServletInitializer并重写它的configure方法。
/**
* Create bySeptember
* 2019/3/3
* 14:56
*/
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
在Maven Projects的项目找到该项目===》打开项目===》Lifecycle===》install。
则在maven仓库中生成了项目的war包。
把war包复制到你的Tomcat的webapps下运行Tomcat的就完成了发布war包部署