Gradle: Load property file dependent on profile

前端 未结 3 1531
终归单人心
终归单人心 2021-02-04 18:15

I want to write a gradle build script, which works in different environments (development, live). In each environment I have to load different property sets (target directories,

相关标签:
3条回答
  • 2021-02-04 18:44

    I had the same problem, but I use spring and configured it to read properties from classpath:application.properties

    In this case you can add this to gradle.build with java plugin

    if (project.hasProperty('env')) {
        println "Target environment: $env"
        sourceSets.main.resources.srcDir "src/main/environment/$env"
    }
    

    this for different folders based on environment

    So for adding resources based on environment 'dev' you should have 'src/main/environment/dev' folder (with properties file), and invoke gradle: gradle myTask -Penv=dev

    0 讨论(0)
  • Also have a look at gradle's equivalent of maven's profiles.

    0 讨论(0)
  • 2021-02-04 19:10

    You may want to check out the Gradle Properties Plugin.

    Include plugin:

    plugins {
      id 'net.saliman.properties' version '1.4.2'
    }
    

    Create property files:

    gradle-dev.properties
    

    or

    gradle-prod.properties
    

    Call gradle:

    gradle myTask -PenvironmentName=dev
    gradle myTask -PenvironmentName=prod
    
    0 讨论(0)
提交回复
热议问题