本文主要分享了对整合jsp的springboot项目打jar包,如何正确的配置maven pom。
一、项目结构
二、打包配置
1.pom.xml添加spring-boot-maven-plugin配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
如果只是普通的项目打包,上面的配置足够了,但是带src/main/webapp的打包,还不行,会发现src/main/webapp的内容都没有打进jar包
2.pom.xml添加配置resources配置
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
<!-- 注意此次必须要放在此目录下才能被访问到 -->
<targetPath>META-INF/resources</targetPath>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
这个配置可将src/main/webapp打包到jar包中,但是这个jar包还不能让jsp正常访问
注:如果没有配置<targetPath>META-INF/resources</targetPath>,src/main/webapp的内容会保存到BOOT-INF/ 目录下。经测试,jsp页面无法正常访问,需要指定到META-INF/resources才能正常访问
3.spring-boot-maven-plugin的版本指定为1.4.2.RELEASE
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 一定要是这个版本,其他版本访问不到页面 -->
<version>1.4.2.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 1.4.2.RELEASE导致多个main方法的情况下需要指定主类 -->
<mainClass>com.company.admin.AdminApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
springboot官方推荐的前端模板引擎是thymeleaf,所以对于jsp的支持可能存在兼容性。1.4.2.RELEASE是经过测试出来。具体原因未知,不过确实可用。
如果项目中没有多个main方法,mainClass可以不配置
4.最终版配置
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
<!-- 注意此次必须要放在此目录下才能被访问到 -->
<targetPath>META-INF/resources</targetPath>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 一定要是这个版本,其他版本访问不到页面 -->
<version>1.4.2.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 1.4.2.RELEASE导致多个main方法的情况下需要指定主类 -->
<mainClass>com.company.admin.AdminApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
怎么样?如果你觉得有用的话,还不快快收藏起来!!!
附:涉及的代码目录
gitee:springcloud-template: 一个基于springcloud netflix微服务框架,记录了关于微服务开发的一些最佳应用,欢迎大家学习指导。