
在Spring Cloud项目中的YAML配置文件,可以使用 ${} 获取配置项的值,比如:
eureka:
instance:
instance-id: ${spring.application.name}:${server.port}
还可以使用 @@ 获取Maven的工程属性,比如:
info:
app:
name: ${spring.application.name}
company:
name: www.etoak.com
build:
artifactId: @project.artifactId@
version: @project.version@
但是有时候能够正常使用:

有的时候项目启动直接报错:
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 42, column 17:
artifactId: @project.artifactId@
^
没搞清楚原因,但是感觉与spring-boot-starter-parent依赖方式有关系,使用<parent>方式就正常:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
使用<dependencyManagement>方式就报错:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
谁能解释下?
================================================================================
后记:
增加以下内容即使使用<dependencyManagement>方式,也可以正常使用@@了
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
</resource>
</resources>
还有说需要配合使用maven-resources-plugin插件,实际上没有使用也可以的。
Apache Maven Resources Plugin
The Resources Plugin handles the copying of project resources to the output directory. There are two different kinds of resources: main resources and test resources. The difference is that the main resources are the resources associated with the main source code while the test resources are associated with the test source code. Thus, this allows the separation of resources for the main source code and its unit tests.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
本文探讨了SpringCloud项目中,在使用YAML配置文件时,如何正确引用Maven工程属性(如`@project.artifactId`),以及在不同依赖管理方式下(parent vs dependencyManagement)遇到的ScannerException。作者发现,`@`符号导致错误可能与依赖管理方式选择有关,并给出了解决办法,即配合`<resources>`标签和maven-resources-plugin配置。
2153

被折叠的 条评论
为什么被折叠?



