Spring application使用@ 使用的問題:'@' that cannot start any token. (Do not use @ for indentation)

錯誤信息

在application配置文件中使用@出現異常:

Exception in thread "main" while scanning for the next token
found character '@' that cannot start any token. (Do not use @ for indentation)
 in 'reader', line 4, column 11:
        name: @project.artifactId@

代碼:

info:
  app:
    name: @project.artifactId@
    encoding: @project.build.sourceEncoding@
    java:
      source: @java.version@
      target: @java.version@

解決辦法

  1. 用單引號或雙引號將@@之間的內容包起來
info:
  app:
    name: "@project.artifactId@"
    encoding: '@project.build.sourceEncoding@'
    java:
      source: '@java.version@'
      target: '@java.version@'
  1. 或者添加maven依賴

使用Maven的資源過濾(resource filter)自動暴露來自Maven項目的屬性,如果使用spring-boot-starter-parent,你可以通過@…@佔位符引用Maven項目的屬性,例如:
[email protected]@
[email protected]@
注 如果啓用addResources標識,spring-boot:run可以將src/main/resources直接添加到classpath(出於熱加載目的),這就繞過了資源過濾和本特性。你可以使用exec:java目標進行替代,或自定義該插件的配置,具體查看插件使用頁面

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

<plugins>
	<plugin>
	    <groupId>org.apache.maven.plugins</groupId>
	    <artifactId>maven-resources-plugin</artifactId>
	    <version>2.7</version>
	    <configuration>
	        <delimiters>
	            <delimiter>@</delimiter>
	        </delimiters>
	        <useDefaultDelimiters>false</useDefaultDelimiters>
	    </configuration>
	</plugin>
<plugins/>

在這裏插入圖片描述注 如果你在配置中使用標準的Spring佔位符(比如${foo})且沒有將useDefaultDelimiters屬性設置爲false,那構建時這些屬性將被暴露出去

參考

1. application使用@符合問題:’@’ that cannot start any token. (Do not use @ for indentation)
2. SpringBoot中出現’@’ that cannot start any token. (Do not use @ for indentation)…

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章