在一個Maven
的SpringBoot
項目中出現 '@' that cannot start any token. (Do not use @ for indentation)
異常情況。
在配置文件 application.yml
中有如下代碼:
spring:
application:
name: @artifactId@
運行報如下的錯誤:
Caused by: 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 3, column 11:
name: @artifactId@
^
分析:
artifactId
是maven
屬性,我們知道使用mavne
屬性是使用${}
的方式,為什么上面使用@@
的方式呢,這是因為為了避免與SpringBoot
的變量沖突,在spring-boot-starter-parent
中mavne-resources-plugin
插件中maven
的變量被重新定義了,代碼如下:
<artifactId>spring-boot-starter-parent</artifactId>
<properties>
<resource.delimiter>@</resource.delimiter>
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimiter>${resource.delimiter}</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
到這里我們知道maven-resources-plugin
插件沒有替換掉application.yml
中的maven
變量。
解決方案,讓插件替換配置中的變量:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>