How to expand property references in jar resources?

前端 未结 5 1943
挽巷
挽巷 2021-01-12 06:47

I\'m using Gradle to build a jar containing an xml file in META-INF. This file has a row like



        
5条回答
  •  情深已故
    2021-01-12 06:51

    I've had similar problems migrating from maven to gradle build. And so far the simplest/easiest solution was to simply do the filtering yourself such as:

    processResources {
      def buildProps = new Properties()
      buildProps.load(file('build.properties').newReader())
    
      filter { String line ->
        line.findAll(/\$\{([a-z,A-Z,0-9,\.]+)\}/).each {
            def key = it.replace("\${", "").replace("}", "")
            if (buildProps[key] != null)
            {
                line = line.replace(it, buildProps[key])
            }
        }
        line
      }
    }
    

    This will load all the properties from the specified properties file and filter all the "${some.property.here}" type placeholders. Fully supports dot-separated properties in the *.properties file.

    As an added bonus, it doesn't clash with $someVar type placeholders like expand() does. Also, if the placeholder could not be matched with a property, it's left untouched, thus reducing the possibility of property clashes from different sources.

提交回复
热议问题