Gradle
  1. Gradle
  2. GRADLE-1419

Add support for loading arbitrary properties files as per gradle.properties

    Details

    • Type: New Feature New Feature
    • Status: Open Open
    • Resolution: Unresolved
    • Affects Version/s: 1.0-milestone-1
    • Fix Version/s: None

      Description

      Gradle can currently load properties from a file called gradle.properties in the project directory. It would be great if the functionality that loads gradle.properties was made available as part of the dsl in order to load any arbitrary properties file as per the Ant <property> element.

        Issue Links

          Activity

          Hide
          Ido
          added a comment -

          Initially I used Blaine's plugin, but it uses a long-deprecated API (dynamic properties) and I got concerned that pretty soon I'd have to choose between the plugin and a Gradle upgrade (when Gradle finally removes the deprecated API in a future release).

          I then found a solution so simple that it makes using a plugin seem like overkill to begin with. Just slap this method into your project file:

          def loadProperties(String sourceFileName) {
              def config = new Properties()
              def propFile = new File(sourceFileName)
              if (propFile.canRead()) {
                  config.load(new FileInputStream(propFile))
                  for (Map.Entry property in config) {
                      ext[property.key] = property.value;
                  }
              }
          }
          

          Then, use it as follows:

          loadProperties 'build.properties'
          

          If the file with the given name doesn't exist, it fails silently.

          The properties are loaded as extra properties of your project, so when reading them you don't use the "ext." qualifier (read them just like dynamic properties), as opposed to when writing into them.

          Show
          Ido
          added a comment - Initially I used Blaine's plugin, but it uses a long-deprecated API (dynamic properties) and I got concerned that pretty soon I'd have to choose between the plugin and a Gradle upgrade (when Gradle finally removes the deprecated API in a future release). I then found a solution so simple that it makes using a plugin seem like overkill to begin with. Just slap this method into your project file: def loadProperties( String sourceFileName) { def config = new Properties() def propFile = new File(sourceFileName) if (propFile.canRead()) { config.load( new FileInputStream(propFile)) for (Map.Entry property in config) { ext[property.key] = property.value; } } } Then, use it as follows: loadProperties 'build.properties' If the file with the given name doesn't exist, it fails silently. The properties are loaded as extra properties of your project, so when reading them you don't use the "ext." qualifier (read them just like dynamic properties), as opposed to when writing into them.
          Hide
          Kevin Stembridge
          added a comment -

          Very nice Ido.

          I'd love to see the Gradle team make this available as a top-level method. They should also consider making it more flexible in how we specify the files to be loaded. e.g. classpath-relative, collections of files, etc.

          Show
          Kevin Stembridge
          added a comment - Very nice Ido. I'd love to see the Gradle team make this available as a top-level method. They should also consider making it more flexible in how we specify the files to be loaded. e.g. classpath-relative, collections of files, etc.
          Hide
          Jonas
          added a comment -

          You say "The properties are loaded as extra properties of your project, so when reading them you don't use the "ext." qualifier (read them just like dynamic properties), as opposed to when writing into them."

          Could you please give an exact example?

          Show
          Jonas
          added a comment - You say "The properties are loaded as extra properties of your project, so when reading them you don't use the "ext." qualifier (read them just like dynamic properties), as opposed to when writing into them." Could you please give an exact example?
          Hide
          Blaine Simpson
          added a comment - - edited

          I forgot to update this discussion last year. I updated my JavaPropFile plugin promptly. It supports the current API without warnings, and hasn't needed any modification since because of the rigorous unit tests. It will be updated aggresively whenever there is a need because it is used by very active open source projects. H

          Ido's snippet satisfies the most common use case. I often need more, like support for types other than strings, nesting of values, ability to reference sys properties (and set them), adding to arbitrary scope (not just the project), validation, default values, etc. See the feature list.

          Show
          Blaine Simpson
          added a comment - - edited I forgot to update this discussion last year. I updated my JavaPropFile plugin promptly. It supports the current API without warnings, and hasn't needed any modification since because of the rigorous unit tests. It will be updated aggresively whenever there is a need because it is used by very active open source projects. H Ido's snippet satisfies the most common use case. I often need more, like support for types other than strings, nesting of values, ability to reference sys properties (and set them), adding to arbitrary scope (not just the project), validation, default values, etc. See the feature list .
          Hide
          Ido
          added a comment -

          You say "The properties are loaded as extra properties of your project, so when reading them you don't use the "ext." qualifier (read them just like dynamic properties), as opposed to when writing into them."

          Could you please give an exact example?

          Sure:

          myprops.properties
          db-host-addr=100.100.100.100
          db-port=222
          db-name=myDB
          
          build.gradle
          ...
          loadProperties 'myprops.properties'
          ...
          println db-name // output: myDB
          ...
          
          Show
          Ido
          added a comment - You say "The properties are loaded as extra properties of your project, so when reading them you don't use the "ext." qualifier (read them just like dynamic properties), as opposed to when writing into them." Could you please give an exact example? Sure: myprops.properties db-host-addr=100.100.100.100 db-port=222 db-name=myDB build.gradle ... loadProperties 'myprops.properties' ... println db-name // output: myDB ...

            People

            • Assignee:
              Unassigned
              Reporter:
              Kevin Stembridge
            • Votes:
              12 Vote for this issue
              Watchers:
              15 Start watching this issue

              Dates

              • Created:
                Updated: