好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

springboot application无法使用$获取pom变量的问题及解决

application无法使用$获取pom变量问题

在maven的pom文件中进行了多环境变量配置,引用了maven-resources-plugin,在application.properties文件中通过以下配置指定不同环境下的配置文件,

?

1

spring.profiles.active = ${profiles.active}

但是${profiles.active}无法从pom文件中获取变量值替换。

由于${}方式会被maven处理。如果你pom继承了spring-boot-starter-parent,Spring Boot已经将maven-resources-plugins默认的${}方式改为了@@方式,如@name@

如果还想继续使用${}占位符方式

只需要在pom文件中加上下面配置即可: 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

< build >

    < pluginManagement >

        < plugins >

            < plugin >

                < artifactId >maven-resources-plugin</ artifactId >

                < configuration >

                    < encoding >utf-8</ encoding >

                    < useDefaultDelimiters >true</ useDefaultDelimiters >

                </ configuration >

            </ plugin >

        </ plugins >

    </ pluginManagement >

</ build >

或者使用

?

1

2

3

4

5

6

< configuration >

                    < delimiters >

                        < delimiter >@</ delimiter >

                    </ delimiters >

                    < useDefaultDelimiters >false</ useDefaultDelimiters >

                </ configuration >

将<useDefaultDelimiters>false</useDefaultDelimiters>

改为<useDefaultDelimiters>true</useDefaultDelimiters>

application.yml无法使用@@读取pom.xml中标签值

在application.yml中使用了@@读取标签值,

报下面的错误

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 5, column 11:         
name: @artifactId@

解决办法

在模块的pom.xml文件下引入一下配置

?

1

2

3

4

5

6

7

8

9

< build >   

<!--如果不设置resource 会导致application.yml中的@@找不到pom文件中的配置-->    

    < resources >       

        < resource >            

            < directory >src/main/resources</ directory >

            < filtering >true</ filtering >        

        </ resource >    

    </ resources >

</ build >

然后重新启动,即可成功。

application.yml无法使用@@读取pom.xml中标签值

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/dany2017/article/details/83177691

查看更多关于springboot application无法使用$获取pom变量的问题及解决的详细内容...

  阅读:20次