升级Maven 3.8.6后Spring Boot项目Thymeleaf模板路径警告求助
解决Maven 3.8.6升级后Spring Boot Thymeleaf模板路径警告问题
问题重现
升级Maven到3.8.6后,启动Spring Boot项目出现以下警告:
DefaultTemplateResolverConfiguration Cannot find template location: classpath:/templates/ (please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)
新建项目可正常运行,仅升级Maven构建后出现该问题。
解决方案
1. 检查Maven资源过滤配置
Maven 3.8.x对资源过滤的处理更严格,需确保src/main/resources下的templates目录被正确包含到构建产物中。在pom.xml的<build>节点里添加或确认资源配置:
<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
如果开启了资源过滤(filtering=true),要避免Thymeleaf模板文件(如.html)被错误过滤,可拆分配置:
<resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> <filtering>true</filtering> <excludes> <exclude>templates/**/*.html</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>templates/**/*.html</include> </includes> <filtering>false</filtering> </resource>
2. 确认模板目录或关闭检查
- 检查项目中是否存在
src/main/resources/templates目录,且目录下有至少一个Thymeleaf模板文件(如index.html),若缺失则补充。 - 如果项目确实不需要Thymeleaf模板,可直接关闭路径检查:
在application.properties中添加:
spring.thymeleaf.check-template-location=false
或application.yml中配置:
spring: thymeleaf: check-template-location: false
3. 清理Maven缓存并重新构建
Maven升级后可能存在缓存冲突,执行以下命令清理并强制更新依赖:
mvn clean install -U
4. 检查Spring Boot Maven插件兼容性
确保Spring Boot Maven插件版本与Maven 3.8.6兼容,推荐使用2.7.x及以上版本,在pom.xml中确认:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.7.15</version> <!-- 或更高兼容版本 --> </plugin> </plugins> </build>
内容的提问来源于stack exchange,提问作者Zuhaib Khan
相关产品推荐
相关产品推荐

