Maven Resource Filtering with Spring Boot: Could not resolve placeholder

前端 未结 1 1306
暖寄归人
暖寄归人 2020-11-29 07:20

Kind of new to Java and Maven, but I trying to get Maven to handle the database connection properties so I can have the maven build change them between dev/stage/prod enviro

相关标签:
1条回答
  • 2020-11-29 07:38

    First of all, you don't need to use a profile. The resources db.properties is a test resource so it should be located under src/test/resources and not under src/main/resources. Using profiles will complicate your build, you should only resort to them as a last condition.

    The reason you're having this problem is that Spring Boot redefines the token filter to be @ instead of the default ${*}. From the docs:

    If you are inheriting from the spring-boot-starter-parent POM, the default filter token of the maven-resources-plugins has been changed from ${*} to @ (i.e. @maven.token@ instead of ${maven.token}) to prevent conflicts with Spring-style placeholders. If you have enabled maven filtering for the application.properties directly, you may want to also change the default filter token to use other delimiters.

    This means that you should have instead:

    jdbc.url= @db.jdbcUrl@
    jdbc.username= @db.jdbcUn@
    jdbc.password= @db.jdbcPw@
    

    for the db.properties file.

    Then you need to remove your <resources> section and replace it with:

    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>db.properties</include>
            </includes>
        </testResource>
    </testResources>
    
    0 讨论(0)
提交回复
热议问题