简体   繁体   English

Spring 配置文件与 Gradle

[英]Spring profiles with Gradle

I'm running a spring boot project with three different property files (application.properties, application-prod.properties, application-uat.properties. I'm using Maven and if I don't specify any argument, it will pick the default property file which is application.properties. Here's how the related files look.我正在运行具有三个不同属性文件(application.properties、application-prod.properties、application-uat.properties)的 spring 引导项目。我正在使用 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 pom.xml

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

How can I achieve the same with Gradle?如何使用 Gradle 达到相同的效果? How can I let Gradle to pick the default (application.properties) if I don't specify any arguement?如果我没有指定任何争论,我如何让 Gradle 选择默认值(application.properties)?

I think this link will be useful for you...我认为此链接对您有用...

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

Sample:样本:

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!'  

This link can be useful too:此链接也很有用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM