springboot 打包与启动02. 启动方式

462 阅读7分钟

1. 开发时启动

在开发时启动springboot应用,指的是直接运行源码,如在开发时在ide中运行启动类的main()方法。

1.1 在ide中执行启动类的 main() 方法

自从有了springboot后,web项目就不必再放到web容器中运行了,直接运行项目的main()方法就行了:

启动日志如下:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2020-01-07 21:11:16.365  INFO 84046 --- [           main] com.gitee.funcy.maven.jar.Main           : Starting Main on l with PID 84046 (/Users/funcy/IdeaProjects/myproject/springboot-demo/springboot-maven/springboot-jar/target/classes started by funcy in /Users/funcy/IdeaProjects/myproject/springboot-demo)
2020-01-07 21:11:16.368  INFO 84046 --- [           main] com.gitee.funcy.maven.jar.Main           : No active profile set, falling back to default profiles: default
2020-01-07 21:11:17.468  INFO 84046 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-01-07 21:11:17.497  INFO 84046 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-01-07 21:11:17.497  INFO 84046 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2020-01-07 21:11:17.513  INFO 84046 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/funcy/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2020-01-07 21:11:17.605  INFO 84046 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-07 21:11:17.605  INFO 84046 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1206 ms
2020-01-07 21:11:17.861  INFO 84046 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-07 21:11:18.096  INFO 84046 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-07 21:11:18.100  INFO 84046 --- [           main] com.gitee.funcy.maven.jar.Main           : Started Main in 1.988 seconds (JVM running for 2.34)
2020-01-07 21:11:32.155  INFO 84046 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-01-07 21:11:32.155  INFO 84046 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-01-07 21:11:32.223  INFO 84046 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 68 ms

访问http://localhost:8080/,结果如下:

$ curl http://localhost:8080
hello world

以上启动方式war与jar打包方式都支持。

1.2 mvn spring-boot:run启动

这种方式也是源码启动,在命令行界面进入项目对应的源码目录下,然后执行mvn spring-boot:run命令:

springboot-parent-war $ mvn spring-boot:run
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------< com.gitee.funcy:springboot-parent-war >----------------
[INFO] Building springboot parent war打包方式 1.0.0
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:2.1.1.RELEASE:run (default-cli) > test-compile @ springboot-parent-war >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ springboot-parent-war ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ springboot-parent-war ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/funcy/IdeaProjects/myproject/springboot-demo/springboot-maven/springboot-parent-war/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ springboot-parent-war ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/funcy/IdeaProjects/myproject/springboot-demo/springboot-maven/springboot-parent-war/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ springboot-parent-war ---
[INFO] No sources to compile
[INFO] 
[INFO] <<< spring-boot-maven-plugin:2.1.1.RELEASE:run (default-cli) < test-compile @ springboot-parent-war <<<
[INFO] 
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.1.1.RELEASE:run (default-cli) @ springboot-parent-war ---

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2020-01-07 21:40:50.577  INFO 84448 --- [           main] com.gitee.funcy.mavenparent.war.Main     : Starting Main on funcydeMacBook-Pro.local with PID 84448 (/Users/funcy/IdeaProjects/myproject/springboot-demo/springboot-maven/springboot-parent-war/target/classes started by funcy in /Users/funcy/IdeaProjects/myproject/springboot-demo/springboot-maven/springboot-parent-war)
2020-01-07 21:40:50.579  INFO 84448 --- [           main] com.gitee.funcy.mavenparent.war.Main     : No active profile set, falling back to default profiles: default
2020-01-07 21:40:51.311  INFO 84448 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-01-07 21:40:51.336  INFO 84448 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-01-07 21:40:51.337  INFO 84448 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2020-01-07 21:40:51.347  INFO 84448 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/funcy/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2020-01-07 21:40:51.406  INFO 84448 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-07 21:40:51.406  INFO 84448 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 800 ms
2020-01-07 21:40:51.582  INFO 84448 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-07 21:40:51.736  INFO 84448 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-07 21:40:51.739  INFO 84448 --- [           main] com.gitee.funcy.mavenparent.war.Main     : Started Main in 1.39 seconds (JVM running for 3.943)
2020-01-07 21:41:04.068  INFO 84448 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-01-07 21:41:04.069  INFO 84448 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-01-07 21:41:04.076  INFO 84448 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 7 ms

可以看到,项目启动成功,请求http://localhost:8080,也能获得结果:

 $ curl http://localhost:8080
hello world

以上启动方式war与jar打包方式都支持。

2. jar包启动

2.1 java -jar方式启动

对于打成jar包springboot项目,使用java -jar xxx.jar命令即可启动:

:target $ java -jar springboot-jar-1.0.0.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2020-01-07 21:47:47.075  INFO 85080 --- [           main] com.gitee.funcy.maven.jar.Main           : Starting Main on funcydeMacBook-Pro.local with PID 85080 (/Users/funcy/IdeaProjects/myproject/springboot-demo/springboot-maven/springboot-jar/target/springboot-jar-1.0.0.jar started by funcy in /Users/funcy/IdeaProjects/myproject/springboot-demo/springboot-maven/springboot-jar/target)
2020-01-07 21:47:47.077  INFO 85080 --- [           main] com.gitee.funcy.maven.jar.Main           : No active profile set, falling back to default profiles: default
2020-01-07 21:47:48.152  INFO 85080 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-01-07 21:47:48.186  INFO 85080 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-01-07 21:47:48.186  INFO 85080 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2020-01-07 21:47:48.202  INFO 85080 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/funcy/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2020-01-07 21:47:48.303  INFO 85080 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-07 21:47:48.303  INFO 85080 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1177 ms
2020-01-07 21:47:48.502  INFO 85080 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-07 21:47:48.677  INFO 85080 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-07 21:47:48.680  INFO 85080 --- [           main] com.gitee.funcy.maven.jar.Main           : Started Main in 1.977 seconds (JVM running for 2.398)

访问http://localhost:8080,同样也能获得结果.

2.2 java org.springframework.boot.loader.JarLauncher方式启动

这种启动方式就魔幻了:好好的一个jar,要先解压,然后直接运行里面的类,操作如下:

target $ unzip -d ./tmp springboot-jar-1.0.0.jar
Archive:  springboot-jar-1.0.0.jar
   creating: ./tmp/META-INF/
  inflating: ./tmp/META-INF/MANIFEST.MF  
   creating: ./tmp/org/
   creating: ./tmp/org/springframework/
   creating: ./tmp/org/springframework/boot/
··· 省略其他内容
target $ cd tmp/
tmp $ java org.springframework.boot.loader.JarLauncher

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2020-01-07 21:56:00.472  INFO 85431 --- [           main] com.gitee.funcy.maven.jar.Main           : Starting Main on funcydeMacBook-Pro.local with PID 85431 (/Users/funcy/IdeaProjects/myproject/springboot-demo/springboot-maven/springboot-jar/target/tmp/BOOT-INF/classes started by funcy in /Users/funcy/IdeaProjects/myproject/springboot-demo/springboot-maven/springboot-jar/target/tmp)
2020-01-07 21:56:00.475  INFO 85431 --- [           main] com.gitee.funcy.maven.jar.Main           : No active profile set, falling back to default profiles: default
2020-01-07 21:56:01.589  INFO 85431 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-01-07 21:56:01.619  INFO 85431 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-01-07 21:56:01.619  INFO 85431 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2020-01-07 21:56:01.634  INFO 85431 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/funcy/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2020-01-07 21:56:01.722  INFO 85431 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-07 21:56:01.722  INFO 85431 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1203 ms
2020-01-07 21:56:01.931  INFO 85431 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-07 21:56:02.154  INFO 85431 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-07 21:56:02.157  INFO 85431 --- [           main] com.gitee.funcy.maven.jar.Main           : Started Main in 2.025 seconds (JVM running for 2.472)

总结下,步骤如下:

  1. 进入项目target/目录
  2. 解压jar包tmp目录:unzip -d ./tmp springboot-jar-1.0.0.jar
  3. 进入tmp目录cd tmp/
  4. 运行:java org.springframework.boot.loader.JarLauncher

访问http://localhost:8080,也能得到正确结果。

注:这种神奇的启动方式在什么情况下会使用呢?我曾经见过一些项目组,为了安全会把生产的配置文件放在服务器上,在部署项目的时候,先解压jar包,然后替换相应的配置文件,再运行。这种解压jar包、替换配置文件的方式就可以用此启动方式了。当然,这些解压、替换、启动等操作都会写进shell脚本里,自动化运行。

3. war包启动

3.1 java -jar方式启动

项目都打成war包了,还能使用java -jar启动?是的,springboot就是这么方便:

target $ java -jar springboot-war-1.0.0.war

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2020-01-07 22:11:54.284  INFO 85638 --- [           main] com.gitee.funcy.maven.war.Main           : Starting Main on funcydeMacBook-Pro.local with PID 85638 (/Users/funcy/IdeaProjects/myproject/springboot-demo/springboot-maven/springboot-war/target/springboot-war-1.0.0.war started by funcy in /Users/funcy/IdeaProjects/myproject/springboot-demo/springboot-maven/springboot-war/target)
2020-01-07 22:11:54.287  INFO 85638 --- [           main] com.gitee.funcy.maven.war.Main           : No active profile set, falling back to default profiles: default
2020-01-07 22:11:55.257  INFO 85638 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-01-07 22:11:55.286  INFO 85638 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-01-07 22:11:55.287  INFO 85638 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2020-01-07 22:11:55.299  INFO 85638 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/funcy/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2020-01-07 22:11:55.711  INFO 85638 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-07 22:11:55.711  INFO 85638 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1379 ms
2020-01-07 22:11:55.873  INFO 85638 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-07 22:11:56.031  INFO 85638 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-07 22:11:56.034  INFO 85638 --- [           main] com.gitee.funcy.maven.war.Main           : Started Main in 2.066 seconds (JVM running for 2.469)
2020-01-07 22:12:01.189  INFO 85638 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-01-07 22:12:01.190  INFO 85638 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-01-07 22:12:01.195  INFO 85638 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms

看,项目真的跑起来了!

3.2 java org.springframework.boot.loader.WarLauncher方式启动

springbootjar包 可以解压,然后运行某个类来启动,war包竟然也有这种方法!jar包的启动类是org.springframework.boot.loader.JarLauncher,相应的war包启动类是org.springframework.boot.loader.WarLauncher,步骤如下:

  1. 进入项目target/目录
  2. 解压war包tmp目录:unzip -d ./tmp springboot-war-1.0.0.war
  3. 进入tmp目录cd tmp/
  4. 运行:java org.springframework.boot.loader.WarLauncher

过程如下:

target $ unzip -d ./tmp springboot-war-1.0.0.war
Archive:  springboot-war-1.0.0.war
   creating: ./tmp/META-INF/
  inflating: ./tmp/META-INF/MANIFEST.MF  
   creating: ./tmp/org/
   creating: ./tmp/org/springframework/
   creating: ./tmp/org/springframework/boot/
··· 省略其他
target $ cd tmp/
tmp $ java org.springframework.boot.loader.WarLauncher

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2020-01-07 22:17:09.637  INFO 85782 --- [           main] com.gitee.funcy.maven.war.Main           : Starting Main on funcydeMacBook-Pro.local with PID 85782 (/Users/funcy/IdeaProjects/myproject/springboot-demo/springboot-maven/springboot-war/target/tmp/WEB-INF/classes started by funcy in /Users/funcy/IdeaProjects/myproject/springboot-demo/springboot-maven/springboot-war/target/tmp)
2020-01-07 22:17:09.640  INFO 85782 --- [           main] com.gitee.funcy.maven.war.Main           : No active profile set, falling back to default profiles: default
2020-01-07 22:17:10.576  INFO 85782 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-01-07 22:17:10.603  INFO 85782 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-01-07 22:17:10.604  INFO 85782 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2020-01-07 22:17:10.616  INFO 85782 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/funcy/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2020-01-07 22:17:10.725  INFO 85782 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-07 22:17:10.725  INFO 85782 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1046 ms
2020-01-07 22:17:10.942  INFO 85782 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-07 22:17:11.137  INFO 85782 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-07 22:17:11.140  INFO 85782 --- [           main] com.gitee.funcy.maven.war.Main           : Started Main in 1.817 seconds (JVM running for 2.183)
2020-01-07 22:17:15.024  INFO 85782 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-01-07 22:17:15.024  INFO 85782 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-01-07 22:17:15.029  INFO 85782 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms

可以看到,项目也启动成功了!

3.3 传统方式启动:使用tomcat容器

最初的war包就是放在tomcat等容器中运行的,我们也来试试war包在tomcat容器中运行情况如何。这里说的tomcat容器是指在tomcat官网下载的容器,非springboot内置容器。这里我下载的是apache-tomcat-8.5.47,过程如下:

... 省略tomcat日志输出
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2020-01-07 22:28:23.519  INFO 85904 --- [ost-startStop-1] c.g.funcy.maven.war.StartApplication     : Starting StartApplication on funcydeMacBook-Pro.local with PID 85904 (/Users/funcy/Applications/Tomcat/apache-tomcat-8.5.47/webapps/springboot-war-1.0.0/WEB-INF/classes started by funcy in /Users/funcy/Applications/Tomcat/apache-tomcat-8.5.47)
2020-01-07 22:28:23.523  INFO 85904 --- [ost-startStop-1] c.g.funcy.maven.war.StartApplication     : No active profile set, falling back to default profiles: default
2020-01-07 22:28:24.256  INFO 85904 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 676 ms
2020-01-07 22:28:24.655  INFO 85904 --- [ost-startStop-1] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-07 22:28:24.920  INFO 85904 --- [ost-startStop-1] c.g.funcy.maven.war.StartApplication     : Started StartApplication in 1.86 seconds (JVM running for 3.98)
07-Jan-2020 22:28:24.974 信息 [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars 至少有一个JAR被扫描用于TLD但尚未包含TLD。 为此记录器启用调试日志记录,以获取已扫描但未在其中找到TLD的完整JAR列表。 在扫描期间跳过不需要的JAR可以缩短启动时间和JSP编译时间。
07-Jan-2020 22:28:24.999 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/Users/funcy/Applications/Tomcat/apache-tomcat-8.5.47/webapps/springboot-war-1.0.0.war] has finished in [3,468] ms
07-Jan-2020 22:28:25.000 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectoryweb 应用程序部署到目录 [/Users/funcy/Applications/Tomcat/apache-tomcat-8.5.47/webapps/docs]
07-Jan-2020 22:28:25.010 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/Users/funcy/Applications/Tomcat/apache-tomcat-8.5.47/webapps/docs] has finished in [10] ms
07-Jan-2020 22:28:25.010 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectoryweb 应用程序部署到目录 [/Users/funcy/Applications/Tomcat/apache-tomcat-8.5.47/webapps/manager]
07-Jan-2020 22:28:25.027 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/Users/funcy/Applications/Tomcat/apache-tomcat-8.5.47/webapps/manager] has finished in [17] ms
07-Jan-2020 22:28:25.027 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectoryweb 应用程序部署到目录 [/Users/funcy/Applications/Tomcat/apache-tomcat-8.5.47/webapps/examples]
07-Jan-2020 22:28:25.181 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/Users/funcy/Applications/Tomcat/apache-tomcat-8.5.47/webapps/examples] has finished in [154] ms
07-Jan-2020 22:28:25.181 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectoryweb 应用程序部署到目录 [/Users/funcy/Applications/Tomcat/apache-tomcat-8.5.47/webapps/ROOT]
07-Jan-2020 22:28:25.191 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/Users/funcy/Applications/Tomcat/apache-tomcat-8.5.47/webapps/ROOT] has finished in [10] ms
07-Jan-2020 22:28:25.191 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectoryweb 应用程序部署到目录 [/Users/funcy/Applications/Tomcat/apache-tomcat-8.5.47/webapps/host-manager]
07-Jan-2020 22:28:25.202 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/Users/funcy/Applications/Tomcat/apache-tomcat-8.5.47/webapps/host-manager] has finished in [11] ms
07-Jan-2020 22:28:25.206 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["http-nio-8080"]
07-Jan-2020 22:28:25.212 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["ajp-nio-8009"]
07-Jan-2020 22:28:25.213 信息 [main] org.apache.catalina.startup.Catalina.start Server startup in 3717 ms
2020-01-07 22:29:30.754  INFO 85904 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-01-07 22:29:30.767  INFO 85904 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 12 ms

请求http://localhost:8080/springboot-war-1.0.0/,结果如下:

$ curl 'http://localhost:8080/springboot-war-1.0.0/'
hello world

可以看到,已经部署成功了。

4. 总结

main()方法mvn命令java -jarjava xxx.WarLauncherjava xxx.JarLauncher外置容器
war支持支持支持支持不支持支持
jar支持支持支持不支持支持不支持