Quantcast

Load properties from an arbitrary properties file

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Load properties from an arbitrary properties file

Kevin Stembridge
Hi,

I'm trying to migrate an Ant build to Gradle and I haven't been able to figure out how to migrate the following behaviour.

In my Ant build, each developer adds a property to their <user.home>/build.properties file that identifies which profile they are using. Each developer is assigned a unique profile.

e.g.
    profile.id=local01

The main build script reads the above property and uses it to load properties from a profile-specific properties file that is stored in a module of the checked out project.

e.g.

    project_root
            master
                    profiles
                            local01
                                    build.properties
                            local02
                                    build.properties


So in Gradle, is there a way to load an arbitrary properties file which will then become properties of the current project?

Regards,
Kevin










Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Load properties from an arbitrary properties file

Peter Niederwieser
Kevin Stembridge wrote
So in Gradle, is there a way to load an arbitrary properties file which will then become properties of the current project?
Gradle has a rich API and is based on a general-purpose programming language, so there is almost always a way. Put something like this into the root project's build script:

file("master/profiles/$profileId").withReader { reader ->
  def userProps = new Properties()
  userProps.load(reader)
  properties.putAll(userProps)
}

If you prefer 'profile.id' over 'profileId', '$profileId' would have to be replaced with '${properties['profile.id']}'. You could also consider to use a user's login name instead of a manually set up profileId property.

--
Peter Niederwieser 
Developer, Gradle
http://www.gradle.org
Trainer & Consultant, Gradleware
http://www.gradleware.com
Creator, Spock Framework
http://spockframework.org


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Load properties from an arbitrary properties file

Philippe Lhoste
On 04/03/2011 01:58, Peter Niederwieser wrote:
> Gradle has a rich API and is based on a general-purpose programming
> language, so there is almost always a way. Put something like this into the
> root project's build script:
>
> file("master/profiles/$profileId").withReader { reader ->
>    def userProps = new Properties()
>    userProps.load(reader)
>    properties.putAll(userProps)
> }

Interesting.
I also found (since I had the same problem some time ago) something like:

Properties props = new Properties()
props.load(new FileInputStream("$somePath/user.properties"))
project.setProperty('props', props)

I failed to find these tricks in the Gradle doc.
If they aren't in it, might I suggest to add them (or one of them)?
Don't forget that a number of Gradle users (at least, me...) just use Gradle as a tool (to
compile Java, Scala, whatever) without prior knowledge of Groovy and time (or will) to
learn it (I might give it a go, but it takes time, this scarce resource).
And it isn't the first time I see such request in this mailing list (which I started to
read recently...).

Thanks.

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Load properties from an arbitrary properties file

Kevin Stembridge
Thanks for the alternative Phillipe.

Rather than add this snippet to the documentation, I think this should be made a feature of the dsl. I imagine it would be super easy to do because there is already the functionality to load gradle.properties. You just need to make it more generic, allowing any arbitrary properties file to be passed in.

I've raised a jira for the feature request.

http://jira.codehaus.org/browse/GRADLE-1419



On 4 March 2011 10:28, Philippe Lhoste <[hidden email]> wrote:
On 04/03/2011 01:58, Peter Niederwieser wrote:
Gradle has a rich API and is based on a general-purpose programming
language, so there is almost always a way. Put something like this into the
root project's build script:

file("master/profiles/$profileId").withReader { reader ->
  def userProps = new Properties()
  userProps.load(reader)
  properties.putAll(userProps)
}

Interesting.
I also found (since I had the same problem some time ago) something like:

Properties props = new Properties()
props.load(new FileInputStream("$somePath/user.properties"))
project.setProperty('props', props)

I failed to find these tricks in the Gradle doc.
If they aren't in it, might I suggest to add them (or one of them)?
Don't forget that a number of Gradle users (at least, me...) just use Gradle as a tool (to compile Java, Scala, whatever) without prior knowledge of Groovy and time (or will) to learn it (I might give it a go, but it takes time, this scarce resource).
And it isn't the first time I see such request in this mailing list (which I started to read recently...).

Thanks.

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email



Loading...