【SpringBoot启动异常】解决@profileActive@相关异常问题
一、异常信息
org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token found character '@' that cannot start any token. (Do not use @ for indentation) in 'reader', line 55, column 13: active: @profileActive@ ^ at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:439) at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:248) at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingValue.produce(ParserImpl.java:633) at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:165) at org.yaml.snakeyaml.comments.CommentEventsCollector$1.peek(CommentEventsCollector.java:59) at org.yaml.snakeyaml.comments.CommentEventsCollector$1.peek(CommentEventsCollector.java:45) at org.yaml.snakeyaml.comments.CommentEventsCollector.collectEvents(CommentEventsCollector.java:140) at org.yaml.snakeyaml.comments.CommentEventsCollector.collectEvents(CommentEventsCollector.java:119) at org.yaml.snakeyaml.composer.Composer.composeScalarNode(Composer.java:221) at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:191)
二、问题分析
目前我的项目中application.yml、application-dev.yml、application-sit.yml、application-prod.yml
application.yml中部分信息如下:
# Spring配置
spring:# 资源信息messages:# 国际化资源文件路径basename: i18n/messagesprofiles:active: @profileActive@# 文件上传servlet:multipart:# 单个文件大小max-file-size: 10MB# 设置总上传的文件大小max-request-size: 20MB# 服务模块devtools:restart:# 热部署开关enabled: true
Pom.xml中配置如下:
<profiles><profile><!-- 开发环境 --><id>dev</id><properties><profileActive>dev</profileActive></properties><!-- 默认激活的环境 --><activation><activeByDefault>true</activeByDefault></activation></profile><profile><!-- 测试环境 --><id>sit</id><properties><profileActive>sit</profileActive></properties></profile><profile><!-- 生产环境 --><id>prod</id><properties><profileActive>prd</profileActive></properties></profile>
</profiles>
通过上述配置在 profiles. Active已经配置了@profileActive@为何还不生效呢?
在网上查询一番资料,有提示pom中缺少
<resources><resource><directory>src/main/resources</directory><filtering>true</filtering></resource>
</resources>
于是尝试修改如下:
<build><resources><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>2.5.15</version><configuration><fork>true</fork> <!-- 如果没有该配置,devtools不会生效 --></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin><plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.1.0</version> <configuration><failOnMissingWebXml>false</failOnMissingWebXml><warName>${project.artifactId}</warName></configuration> </plugin> </plugins><finalName>${project.artifactId}</finalName>
</build>
Maven clean后,然后启动应用,结果真的启动成功了.
三、解决方案
参考上述配置,在build中增加:
<resources><resource><directory>src/main/resources</directory><filtering>true</filtering></resource></resources>