不知不觉都第十篇了,用了这么久,都是在本机运行,localhost 还是不爽,加上目前 jsp 页面已配置,可以做几个炫一些的页面,挂服务器上试试。那么问题来了,如何打包、部署呢?接下来开始尝试打包,Spirng Boot 默认配置是 jar 包,那首先来尝试一下 jar 包的打包、部署和测试。
在文章开始之前,首先删除配置文件的部分配置,如下:
<!-- Add Spring repositories -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
注释的意思是:
官网解释:
If you use a milestone or snapshot release, you also need to add the appropriate pluginRepository elements.
大概意思:如果使用里程碑或快照版本,则还需要添加相应的pluginRepository元素。
所以这里先把这部分配置删除, 因为目前项目用的是 2.0.2.RELEASE 版本。
一、打 jar 包。
如果不做任何的配置,直接打包:选中项目-》右键-》Run as -》4 Maven build -》弹出框 Goals 栏输入 -X package -》点击 Run 按钮,则打包开始。如果出现错误,可以尝试做下面两步操作:
第一步:选中项目-》右键 -》Maven -》Update Project。
第二步:选中项目-》菜单 -》Project -》Clean。
备注:上面 IDE 是英文环境,如果你的是中文,请简单翻译,对照操作。
如果打包成功,可以在 target 目录看到自己期盼已久的 jar 包,如图:

上传到 linux 服务器。可以看到这个包非常小,不到 14k:

运行试下效果,总觉得有点悬:
启动命令:java -jar test-springboot-1.0.jar

查看 jar 包,找到 test-springboot-1.0.jar\META-INF\MANIFEST.MF,内容如下:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: LangLang
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_172
确实少 main 方法,因为这是 Spring Boot 启动的入口,看来打包有问题,缺配置。
修改 pom.xml 配置文件:
- <build>
- <plugins>
- <!-- 这是Spring Boot Devtools Plugin的配置 -->
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <!-- 如果没有fork配置,可能devtools不会起作用,即不会restart -->
- <fork>true</fork>
- <mainClass>com.menglanglang.test.springboot.App</mainClass>
- </configuration>
- <executions>
- <execution>
- <goals>
- <goal>repackage</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
继续打包,打包后可以再看看 test-springboot-1.0.jar\META-INF\MANIFEST.MF 文件的内容:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: LangLang
Start-Class: com.menglanglang.test.springboot.App
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Version: 2.0.3.RELEASE
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_172
Main-Class: org.springframework.boot.loader.JarLauncher
内容明显比之前多很多,而且有了 Start-Class 等信息,而且 jar 包的大小也变为近 20M。
上传服务器,再次运行测试,结果如下:

看来打包部署算是成功了。
二、测试
打开浏览器,访问下前几篇测试的链接,看看结果是否一致。
访问:http://192.168.220.254:8080/cfg/outStr1

访问:http://192.168.220.254:8080/cfg/outStr4

访问:http://192.168.220.254:8080/jsp/testJspPage

打开 jar 包后发现,jar 包中并没有打入 jsp 页面部分,所以继续修改 pom.xml 配置,如下:
- <build>
- <plugins>
- <!-- 这是Spring Boot Devtools Plugin的配置 -->
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <!-- 如果没有fork配置,可能devtools不会起作用,即不会restart -->
- <fork>true</fork>
- <mainClass>com.menglanglang.test.springboot.App</mainClass>
- </configuration>
- <executions>
- <execution>
- <goals>
- <goal>repackage</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- <resources>
- <resource>
- <directory>${basedir}/src/main/webapp</directory>
- <targetPath>META-INF/resources</targetPath>
- <includes>
- <include>**/**</include>
- </includes>
- </resource>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>false</filtering>
- <includes>
- <include>**/**</include>
- </includes>
- </resource>
- <resource>
- <directory>src/main/webapp</directory>
- <filtering>false</filtering>
- <includes>
- <include>**/**</include>
- </includes>
- </resource>
- </resources>
- </build>
打包,部署,测试,发现还是不能访问页面。查其原因,官网说法:
JSP limitations
When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.
- With Jetty and Tomcat it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.
- Undertow does not support JSPs.
- Creating a custom error.jsp page won’t override the default view for error handling, custom error pages should be used instead.
大概意思:
JSP 限制
当运行使用嵌入 servlet 容器的 Spring Boot 应用程序(并打包为可执行包)时,JSP 支持存在一些限制。
- 使用 Jetty 和 Tomcat,如果使用 war 包,它应该可以工作。使用 java -jar 启动时,可执行的 war 也将起作用,并且还可以部署到任何标准容器。 使用可执行 jar 包时不支持 JSP。
- Undertow不支持JSP。
- 创建自定义 error.jsp 页面不会覆盖错误处理的默认视图,而应使用自定义错误页面。
看来是 Jar 包不能很好的支持访问 jsp 页面导致。
是不是打包的插件有问题,如果不设置版本,应该是最新版本,也就是 2.0.2,指定一个老版本再试试,比如 1.4.0。修改配置后如下:
<build>
<plugins>
<!-- 这是Spring Boot Devtools Plugin的配置 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.0.RELEASE</version>
<configuration>
<!-- 如果没有fork配置,可能devtools不会起作用,即不会restart -->
<!-- 。。。此处省略n行 -->
</build>
再次部署测试,居然能看到 jsp 页面内容,只是加载特别慢,如图:

但在 1.4.0 的官方文档中,也有 JSP limitations 部分说明。
顺便,又试了下 1.5.x 系列的版本,结果和 2.0.x 一样,所打包的 jar 都是不能访问 jsp 页面的,其它版本大家可以自行尝试。
猜测,Spring Boot 刚开始也是想支持 jsp 的,可能由于支持不是很好,所以在后续版本中,逐渐取消了 jsp 的支持。