Is resource filtering in Gradle possible without using tokens?

后端 未结 3 1229
走了就别回头了
走了就别回头了 2020-12-29 06:34

The recommended way to do resource filtering in Gradle is by having tokens in the properties file and then replacing them when processing.

Example

#          


        
相关标签:
3条回答
  • Can use thing like placeholder if you want.

    In config.properties file

    var1=${var1}
    var2=${var2}
    

    In gradle.properties file

    processResources {
        filesMatching('**config.properties') {
            expand(
                'var1': project.property('var1'),
                'var2': project.property('var2'),
            )
        }
    }
    
    0 讨论(0)
  • 2020-12-29 06:59

    The spring-boot approach

    project.version=X.X.X.X
    info.build.version=@project.version@
    

    http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-application-info-automatic-expansion

    0 讨论(0)
  • 2020-12-29 07:03

    You could use the following (not tested):

    processResources {
        filesMatching('**/config.properties') {
            filter {
                it.replace('localhost', project.property('myhost'))
            }
        }
    }
    

    Or you could have a default file, used during development in your IDE, and have another file containing tokens and replacing the development one when building using gradle. Something like this (not tested)

    processResources {
        exclude '**/config.properties'
        filesMatching('**/config-prod.properties') {
            setName 'config.properties'
            filter ReplaceTokens, tokens: [
                "myhost": project.property('myhost')
            ]
        }
    }
    
    0 讨论(0)
提交回复
热议问题