一、构建Jar包,命令行运行Spring Boot程序
创建项目的时候选择以jar的方式构建,pom.xml中会有<packaging>jar</packaging>
1.右键pom.xml->run as->Maven install;
2.刷新target目录->copy 项目名-0.0.1.jar的目录->cmd cd到 jar目录-> java -jar 项目名-0.0.1.jar启动项目;
二、构建war包,发布到Tomcat
1.将pom.xml中的<packaging>jar</packaging>换成<packaging>war</packaging>
2.在pom.xml添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency
3.项目上出现红叉,右键项目—>maven->update project ->ok->红叉消失;
4.让启动类xxxApplicaton继承 SpringBootServletInitializer;
5.重写父类方法
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
return builder.sources(xxxApplication.class);
}
6.右键pom.xml->Run as->Maven clean;
7.右键pom.xml->run as->Maven install;
8.将target生成的war包放到tomcat的webapps中,启动tomcat。
(注:访问的时候需要加上war包解压后的包名路径,eg:localhost:8080/文件名(项目名-0.0.1...)/)