1. 程式人生 > 其它 >解決 '@' that cannot start any token. (Do not use @ for indentation)

解決 '@' that cannot start any token. (Do not use @ for indentation)

在一個MavenSpringBoot專案中出現 '@' 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@
              ^

分析:

artifactIdmaven屬性,我們知道使用mavne屬性是使用${}的方式,為什麼上面使用@@的方式呢,這是因為為了避免與SpringBoot的變數衝突,在spring-boot-starter-parentmavne-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>