常用的profile中的properties子标签动态环境引用,项目组件artifactId引用
pom.xml
<profiles>
<profile>
<id>local</id>
<properties>
<environment>local</environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>dev</id>
<properties>
<environment>dev</environment>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<environment>test</environment>
</properties>
</profile>
<profile>
<id>uat</id>
<properties>
<environment>uat</environment>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<environment>prod</environment>
</properties>
</profile>
</profiles>
bootstrap.yml
spring:
profiles:
active: @environment@
如遇到ScannerException异常
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 26, column 13: active: @environment@
错误原因:
报了ScannerException异常,yml格式错误的时候抛出的异常,格式哪里有问题呢?active: @environment@解析错误,environment变量在pom中,说明没有加载到pom配置
解决方法:
在右侧的maven栏中重新reload的一下就行了
如果还是不行
添加resource到classpath
spring-boot:run可以将src/main/resources直接添加到classpath
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<includes>
<include>bootstrap-${environment}.yml</include>
<include>bootstrap.yml</include>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
或者自定义该插件的配置
<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/>