【问题标题】:Spring profiles with Gradle使用 Gradle 的弹簧配置文件
【发布时间】:2021-06-27 00:26:57
【问题描述】:

我正在运行一个包含三个不同属性文件(application.properties、application-prod.properties、application-uat.properties)的 Spring Boot 项目。我使用的是 Maven,如果我不指定任何参数,它将选择默认属性文件 application.properties。这是相关文件的外观。

application.properties

#Set active profile
spring.profiles.active=@activatedProperties@
    
#MongoDB configuration
spring.data.mongodb.host=${MONGODB_HOST:localhost}
spring.data.mongodb.port=${MONGODB_PORT:27017}
spring.data.mongodb.database=database
spring.data.mongodb.username=username
spring.data.mongodb.password=password

pom.xml

<profiles>
    <profile>
        <id>UAT</id>
        <properties>
            <activatedProperties>uat</activatedProperties>
        </properties>
    </profile>
    <profile>
        <id>PROD</id>
        <properties>
            <activatedProperties>prod</activatedProperties>
        </properties>
    </profile>
</profiles>

如何使用 Gradle 实现相同的目标?如果我不指定任何争论,我如何让 Gradle 选择默认值 (application.properties)?

【问题讨论】:

    标签: java spring-boot gradle


    【解决方案1】:

    我认为这个链接对你有用...

    https://docs.gradle.org/current/userguide/migrating_from_maven.html#migmvn:profiles_and_properties

    示例:

    if (!hasProperty('buildProfile')) ext.buildProfile = 'default'  
    
    apply from: "profile-${buildProfile}.gradle"  
    
    task greeting {
        doLast {
            println message  
        }
    }
    

    #profile-default.gradle
    ext.message = 'foobar' 
     
    #profile-test.gradle
    ext.message = 'testing 1 2 3'  
    
    #profile-prod.gradle
    ext.message = 'Hello, world!'  
    

    这个链接也很有用:

    https://www.baeldung.com/spring-profiles

    【讨论】:

    • 当你传递不同的参数时它如何选择不同的属性文件,如果你不指定任何参数,它如何选择默认的属性文件 application.properties?
    • 第一个问题:apply from: "profile-${buildProfile}.gradle" 第二个问题:if (!hasProperty('buildProfile')) ext.buildProfile = 'default'
    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 2018-11-18
    • 1970-01-01
    • 2014-10-14
    • 2012-11-02
    • 1970-01-01
    • 2016-07-28
    • 2020-03-24
    相关资源
    最近更新 更多