Jar打包方式運行
類型為jar時
<packaging>jar</packaging>
1.使用命令mvn clean package 打包
2.使用java –jar 包名運行
// -server -Xms256m -Xmx256m 設置jvm參數 初始化堆內存值和最大值
例:java -server -Xms256m -Xmx256m -jar springboot_project.jar
War打包方式 外部Tomcat運行
類型為war時
<packaging>war</packaging>
1.pom.xml修改
<!-- springboot-web組件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 移除內置的tomcat --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- 或將內置的tomcat Scope改為provided --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
說明:scope作用范圍
1、compile設置編譯范圍內有效,在編譯和打包時都會將依賴項加載進去。
2、test設置測試范圍內有效,只在測試時使用,編譯和打包時都不會加載該項。
3、provided設置編譯和測試的范圍內有效,打包時不會加載該項。
4、runtime設置在運行時加載該依賴項,但是編譯時不依賴。
2.修改啟動類 繼承SpringBootServletInitializer,並實現configure方法
@SpringBootApplication public class ApplicationRun extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(this.getClass()); } public static void main(String[] args) { SpringApplication.run(ApplicationRun.class,args); } }
說明:外部tomcat啟動的時候配置文件中配置的端口號和context-path將失效,使用的是外部tomcat的端口號和tomcat webapps下war包 名稱
#修改端口號和contextPath server.port = 8888 server.servlet.context-path=/test
打包跳過本地的錯誤(本地環境和生產環境沖突):package -Dmaven.test.skip=true