Replace properties in a file

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Replace properties in a file

Ignacio Coloma
Is there any way of replacing properties in a file while it's being copied? I have a file that contains

docBase="${project.projectDir}/src/web"

And I want to copy it and replace the properties automatically. With ant, I would have used a filterchain (expandproperties) but with gradle I'm a little lost:

ant.copy(
        file: "fromfile.xml",
        tofile: "tofile.xml"
        //chain:   << magic happens here
        )

Note that I'm not using any wildcards here, so a pure Groovy solution may also solve my problem.

Reply | Threaded
Open this post in threaded view
|

Re: Replace properties in a file

hans_d
Administrator
Hi Ignacio,

there are a couple of ways to deal with this.

On Sep 19, 2008, at 3:43 PM, Ignacio Coloma wrote:

> Is there any way of replacing properties in a file while it's being  
> copied? I have a file that contains
>
> docBase="${project.projectDir}/src/web"
>
> And I want to copy it and replace the properties automatically.  
> With ant, I would have used a filterchain (expandproperties) but  
> with gradle I'm a little lost:
>
> ant.copy(
>         file: "fromfile.xml",
>         tofile: "tofile.xml"
>         //chain:   << magic happens here
>         )

The ant way:

ant.copy(file: "fromfile.xml", tofile: "tofile.xml") {
    filterset(begintoken: ...) {
       filter(token: ... value: ...)
       filter(token: ... value ....)
    }
}

Ant on stereoids goes like this:

Map filters = [a: 'b', c: 'd', ...]
ant.copy(file: "fromfile.xml", tofile: "tofile.xml") {
    filterset(begintoken: ...) {
       filters.each { key, value ->
          filter(token: key value: value)
       }
    }
}

You can bundle multiple ant statements in one context:

ant {
    copy(...)
    someOtherCommand(...)
}

Build-in:

The resources task has build in functionality for this. See the  
(rather thin documentation) in: http://www.gradle.org/api/0.4/org/ 
gradle/api/tasks/Resources.html
Under the hood it delegates to Ant. The only limitation right now is  
that it uses the default Ant begin and end tokens (@). You can't  
specify other ones.  If you would need this behavior, please file a  
Jira.

Groovy:

http://groovy.codehaus.org/Groovy+Templates

- Hans

--
Hans Dockter
Gradle Project lead
http://www.gradle.org





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

    http://xircles.codehaus.org/manage_email